Introduction To C++
Introduction To C++
Introduction To C++
C++ Headers
Class Definition
Main function
}
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
{
Array Structure
Functions Union
Pointers Class
References Enumeration
doubl
int char float
e
Datatypes in c++
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;