Introduction To C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Introduction to C++

• During the 1980s and into the 1990s Object-Oriented


Programming(OOP) began to replace the more traditional
structured programming techniques.
• This explosion led to the development of many new languages
including C++ which support programming with objects.
• Evolution of C++
• C++ was developed and implemented by Bjarne Stroustrup at
AT&AT Bell Laboratories in the early 1980s.
• C++(pronounced “see plus plus”) is a high-level programming
language having low-level facilities.
• It is not an entirely new language.
• This language is based on two existing languages namely. C
and Simula.
• C++ includes the flexibility and efficiency of C and the object-
oriented programming features of Simula.
Applications of C++
• Bjarne Stroustrup has given a list of systems, applications, and
libraries that are completely or mostly written in C++. Some
of them are given here.

1 Adobe systems like Photoshop & ImageReady, Acrobat, etc.


2 CAD Systems
3 E-commerce software such as Amazon.com
4 Firefox browser, i.e., Mozilla
5 K Desktop environment from linux
6 Macintosh systems
7 Medimage products
8 MySQL Server
9 Winamp
10 Web search engines like Google.
• C++ is extensively used for developing applications such as
editors , compilers, databases, word processing programs,
telecommunication, banking, insurance, trading and other
complex real-time application systems.
The Structure of a C++ Program
• Programs are a sequence of instructions or statements.
• These statements form the structure of a C++ program.
• C++ program structure is divided into various sections,
namely, headers, class definition, member functions
definitions and main function

C++ Headers

Class Definition

Member functions definitions

Main function

Structure of a C++ program


• It is essential to remember that C++ offers the flexibility
of writing a program with or without a class and its
member functions definitions.
• A simple C++ struct program (without a class) will
include comments, namespace, headers, main() and
input/output statements.
1. Comments (Documentation section):
• This section comes first and is used to document the logic
of the program that the programmer going to code.
• It can be also used to write for purpose of the program.
• Whatever written in the documentation section is the
comment and is not compiled by the compiler.
• Documentation Section is optional since the program can
execute without them.
•  supports two comment styles-
• Single line comment :
– / / An example to demonstrate single line comment
• Multiline comment
– /* An example to demonstrate multiline comment */
2. Header Files:
• Generally, a program includes various programming
elements like built-in functions, classes, keywords, 
constants, operators, etc. that are already defined in the
standard C++ library.
• In order to use such pre-defined elements in a program, an
appropriate header must be included in the program.
• Eg:
• #include <iostream>
• Standard headers are specified in a program through
the preprocessor directive #include. Here iostream header
is used. When the compiler processes the
instruction #include<iostream>, it includes the contents of
the stream in the program.
3. Global Definition section (class definition)
• Here, the variables and the class definitions which are
going to be used in the program are declared to make
them global.
• The scope of the variable declared in this section lasts
until the entire program terminates.
• Eg:
#include<iostream>
class sum
{

}
4. Member function definition Section:
• It contains all the functions which our main functions need.
• Usually, this section contains the User-defined functions.
• #include <iostream>
class Room
{

int length , breadth;


int calculateArea()
{
return length * breadth;
}
};
5. Main function.
• The main function tells the compiler where to start the
execution of the program. The execution of the program
starts with the main function.
• All the statements that are to be executed are written in
the main function.
• The compiler executes all the instructions which are
written in the curly braces {} which encloses the body of
the main function.
Input/Output Streams
• The input stream uses cin object to read data and the output
stream uses cout object to display data on the screen.

>> Variables MEMORY Variables <<

>> Extraction Operator


cin cout
<< Insertion Operator

Input devices Output devices


(keyboard , mouse) (Monitor)

Working of cin and cout statements


Input Stream
• The input stream does read operation through keyboard.
• It uses cin as object.
• The cin statement uses >> (extraction operator) before
variable name.
• The cin statement is used to read data through the input
device.
Syntax :
• Cin >> variable
• The operator >> accepts the data and assigns it to the memory
location of the variables.
Eg:
int weight;
cin>>weight; // Reads integer value.
Output Stream
• The output stream displays contents of variables on the screen.
• It uses cout as object.
• The cout statement uses << (insertion operator) before
variable name.
• The cout statement is used to write data on the screen.
Syntax :
• cout << variable
Eg:
int weight;
cout<<weight; // Displays integer value.
Write a program to accept string through the keyboard and display it on
the screen. Use cin and cout statements
#include <iostream.h>
#include <conio.h>
main()
{
char name[15];
clrscr();
cout << “Enter Your Name :”;
cin >>name;
cout<<“Your name is” <<name;
return 0;
}
Output
Enter Your Name : Amit
Your name is Amit
• Explanation :
in the above program, cout statement displays given string on
the screen. It is similar to printf() statement. The cin statement
reads data through the keyboard. It is similar to scanf()
statement
Write a program to read two integers and display them. Use cin
and cout statements
#include <iostream.h>
#include <conio.h>
main()
{
int num, num1;
clrscr();
cout << “Enter two numbers: “;
cin>>num>>num1;
cout<<“Entered numbers are: ”;
cout <<num<<“\t”<<num1;
return 0; output
} Enter two numbers : 8 9
Entered Numbers are : 8 9
Write a program to input integer, float, char, and string using cin
statement and display using cout statements
#include <iostream.h>
#include<conio.h>
Void main()
{
clrscr();
int x;
float y;
char z;
char city[15];
cout<<“\n Enter integer, float and char “;
cin >> x>>y>>z;
cout <<“\n Enter a string : “;
cin >> city;
cout<<“x=“<<x<<“ y=“<<y <<“z = “ <<z << endl;
cout<<“city=“<<city;
}
output
Enter integer , float, and char : 12 1.2 H
Enter a string : cochin
x =12 y = 1.2 z=H
city = cochin
Data types and Declarations
C++ datatypes
C++ DataTypes

Derived Data Type Basic Data Type User Defined Data


Type

Array Structure
Functions Union
Pointers Class
References Enumeration

Integral Float Void

doubl
int char float
e
Datatypes in c++

• Data is a collection of characters, digits, symbols etc.


• it is used to represent information.
• A data type determines the type and the operations that can be
performed on the data.
• C++ data types can be classified in the following categories
1) Basic data type
2) Derived type
3) User- defined type
4) Void data type
1) Basic (Built-in) Data Types :
the basic (built-in) data types provided by C++ are integral ,
floating point and void data type
a) Integral data type :
is used to store both integers and character
• char : characters refer to the alphabet , numbers and
other characters (such as { , @ , # , etc.) defined in the
ASCII character set.
• In C++ , the char data type is also treated as an integer
data type as the characters are internally stored as
integers that range in value from -128 to 127 .
• The char data type occupies 1 byte of memory
• Syntax :
char variable;
• int : Numbers without the fractional part represent
integer data.
• The int data type is used to store integers such as 4, 43,
4563 , -32 , -745.
Syntax
int variable;

b) Floating point data type : a floating point data type is


used to store real numbers such as 3.28 , 64.755, etc..

c) Void : the void data type is used for specifying an empty


parameter list to a function and return type for a function.
2) Derived Data types
– Data types that are derived from the built-in data types are
known as derived data types
– The various derived data types provided by C++ are arrays,
functions , references and pointers
Array:
• An Array is a collection of elements of similar data
types in which each element is located in separate
memory location.
• Eg:
Int b[4];
• The above statement declares an array b[ ] which can
hold 4 integer values.
Function :
• A function is a self-contained program segment that
carries out a specific well-defined task.
• In C++, every program contains one or more functions
which can be invoked from other parts of a program, if
required.
main() funA()
{ {
funA( ); -----
return;
---------
}

funB( ); funB()
---------- {
} -----
return;
}
– Pointer (*)
• A pointer is a variable that can store the memory address of another
variable
• It is always denoted by ‘*’ operator
• Eg: Int *x;

• 2nd Eg:
Int x=2 , *p ;
p = &x;

Variable x 4096 2

Pointer p
variable

– Reference : (&)
• A reference is an alternative name for a variable. i.e., a reference is
an alias for a variable in a program.
3) User-defined data type:
– C++ provides various user-defined data types such as
structures, unions, enumerations and classes.
Structures
Keyword struct
The struct is a keyword and used to combine variables of
different data types into a single record.
Syntax :
struct struct name
{

}
Union
A union is same as compared to a struct , the only difference is
that it lets the user to declare variables that share same memory
space.
Syntax:
union <union name>
{

}
• Enumerated Data type:
– The enum is a keyword.
– It is used for declaring enumeration data types.
– The programmer can declare new data type and define the
variables of these data types that can hold.
– Eg:
– The user can define the material as new data type. It’s
variable may be solid, liquid or gas.
– Thus three values are restricted for this new data type.
– Eg:
– enum country {US , UN , India , China};
– Note that these enumerators represent integer values, i.e.,
– In C++ enumerations are treated as integers internally
Variable declarations
• A variable is an identifier that refers to the data item stored at
a particular memory location.
• This data item can be accessed in the program simply by using
the variable name.
• The value of a variable can be changed by assigning different
values t it at various places in program.
Declaration of variables
• Variables must be declared in a program before they are used.
• The declaration of a variable informs the compiler, the specific
data type to which a variable is associated and allocates
sufficient memory for it.
• Syntax:
• Data_type variable_name;
• For eg: a variable of type int can be declared using this
statement.
• int a;
• At the time of the variable declaration, more than one variable
of the same data type can be declared in a single statement.
• For eg:
• int x , y , z ;
Initialisation of variables
Declaration of variables allocates sufficient memory for
variables. However, it does not store any data in the variables.
however, it does not store any data in the variables.
To store data in variables, they need to be initialised at the
time of declaration. Eg:
• int i = 10.
• in this statement, a variable I of the integer type is declared
and initialised with a value do 10
• Variable can be initialised at the compile-time or at the run-time.
• Initialistion at the compile-time using constant is known as
static initialisation.
• Variables can also be initialised at the run-time using
expressions. Initialisation of variables at the run-time is known
as dynamic initialisation.
• Eg of static and dynamic initialisation
• #include<iostream>
void main()
{
int num1=5 , num2 = 6; // static initialisation using constant
int num3 = num1 + num2; // Dynamic initialisation using
expression
cout<<num3;
}
• Constant variable:
– When the qualifier const precedes a data type in the declaration of a
variable, it implies that the value of the variable cannot be changed
throughout the program. Eg:
– const float pi_val = 3.14;

Write also reference variable…….

You might also like