Professional Documents
Culture Documents
• The members (data and functions) declared in the private mode can be
accessed only from within the class, while the members declared in the
public mode can be accessed from outside the class also.
• The private data member and the private member functions can only
be accessed by the member functions of the class.
public:
Data member2
Accessible from
outside the class
Member
function2
Notations used:
Indicates that the member (data or function) is not accessible
Indicates that the member (data or function) is accessible
Example:
#include<iostream.h>
class Rectangle
{
float length, breadth ; // Data members within the class rectangle,
// which are private by default
public:
void enterdata(void) ; // Member function declarations within the
void disparea(void) ; // class rectangle, which are declared as public
};
//******************Member Function Definitions**************
void Rectangle :: enterdata(void)
{
cout << "\n Enter the length of the rectangle: " ;
cin >> length ;
cout << "\n Enter the breadth of the rectangle: " ;
cin >> breadth ;
}
void Rectangle :: disparea(void)
{
float area = length * breadth ;
cout << "\n The area of the rectangle is = " << area << endl ;
}
Cont.
//*****************Main Function*****************************
void main(void)
{
Rectangle r1, r2 ; // a1 and a2 are objects of the
// user-defined type rectangle
cout << "\n First rectangle-->\n" ;
r1.enterdata( ) ; // Call Member Functions
r1.disparea( ) ;
• For example, in the previous program, the member functions are defined outside
the class as:
return-type function-name(arguments)
{
Function body ;
}
Example:
# include<iostream.h>
class Rectangle // Name of the class
{
float length, breadth ; // Data members which are private by default
class Rectangle
{
private:
int length, breadth ;
public:
void enterdata( ) ;
void disparea( ) ;
} a1, a2 ;
a1.enterdata( ) ;
a1.disparea( ) ;
Cont.
• Note that the objects of a class can access only those
members of the class that are declared in public mode.
}; };
a1.disparea( ) ;
a2.disparea( ) ;
}
Constructor
• A constructor is a special kind of member function that allows one to
set up values while defining the objects, without need to make a
separate call to a member function.
• Moreover, no return type is used for the constructor (not even void).
Since, the constructor is called automatically when an object is created;
returning a value would make no sense.
• The constructor that does not accept any parameter is called the
default constructor.
class class_name
{
private:
data_member ;
public:
class_name(argument_list) ; //
Constructor
};
Example
void main(void)
{
Rectangle r1 ; // Invokes the first constructor without any argument
cout << "\n First rectangle-------->\n" ;
r1.Enter_lb( );
r1.Display_area( );
cout << "\n\n Second rectangle------->\n" ;
rectangle r2(5.6, 3.5); //Invokes the second constructor with two
// arguments
r2.Display_area( );
}
Overloaded constructors
For example,
class Rectangle
{
………
………
public:
Rectangle(Rectangle r) ; // This is not valid
};
r1.Enter_lb( );
r1.Disp_area( );
Rectangle r2(7.5, 3.6); // Invokes the second constructor with two arguments
r2.Disp_area( );
r3.Disp_area( );
}
Destructor
• A constructor initializes an object. In other words, it creates the
environment in which the member functions operate. Sometimes,
creating that environment involves acquiring a resource- such as a file,
a lock, or some memory that must be released after use. Thus, some
classes need a function that is guaranteed to be invoked when an object
is destroyed in a manner similar to the way a constructor is guaranteed
to be invoked when an object is created. Such functions are called
destructors.
Output:
First complex number
The complex number is: -5.3+3.2 i
Second complex number
return temp ;
}
void display_data(void)
{
if(imaginary >=0)
cout << "\n\t The complex number is: " << real << "+"
<< imaginary << " i" << endl ;
else
cout << "\n\t The complex number is: " << real
<< imaginary << " i" << endl ;
}
}; // End of the class definition
Cont.
void main()
{
Complex c1, c2(4.5, -3.2), c3 ;
c1.enter_data( );
cout << "\n First complex number***********" ;
c1.display_data( );
cout << "\n Second complex number**********" ;
c2.display_data( );
c3 = c1.add_comp(c2);
cout << "\n The sum of the above two complex numbers********";
c3.display_data( );
}
Output:
Enter the real part of the complex number: 2.5
Enter the imaginary part of the complex number: 3.8
First complex number***********
The complex number is: 2.5+3.8 i
Second complex number*********
The complex number is: 4.5-3.2 i
The sum of above two complex numbers**********
The complex number is: 7+0.6 i
Passing of objects by reference:
// This program illustrates the passing of the object arguments by reference.
#include<iostream.h>
class Complex
{
private:
float real, imaginary ;
public:
Complex( )
{
real = 0.0 ;
imaginary = 0.0 ;
}
Complex(float r, float i)
{
real = r ;
imaginary = i ;
}
void enter_data(void)
{
cout << "\n Enter the real number: " ;
cin >> real ;
cout << "\n Enter the imaginary number: " ;
cin >> imaginary ;
}
Cont.
com2.enter_data( );
cout << "\n The second complex number is: " ;
com2.display_data( );
Complex com3 ;
com3.add_comp(com1, com2);
cout << "\n The sum of the two complex numbers is: " ;
com3.display_data( );
}
Output:
The first complex number is: 3.5+5.6i
Enter the real number: 4.5
Enter the imaginary number: 7.5
The second complex number is: 4.5-7.5i
The sum of two complex numbers is: 8-1.9i
Constructors with default arguments
void main(void)
{
S_Interest s(5995.75, 5);
s.Display_si( );
}
Output:
generates a jump to the function. At the end of the function, it jumps back
to the instruction following the call. While this sequence of events may save
memory space, it takes some extra time.
void main()
{ void display()
display( );
…………. {
if (imaginary >= 0)
……….....
cout << “\n complex number is: “ << cout << real << “+” << imaginary <<
display( ); “i” ;
else
…………..
cout << “\n complex number is: “ <<cout << real << imaginary << “i” ;
………….. }
display( );
…………..
………….. Fig: Repeated code placed in function
}
Cont.
• To save the execution time in short functions, you may select to put
the code in the function body directly in line with the code in the
calling program.
• In other words, each time there is a function call in the source file,
the actual code from the function is inserted instead of a jump to the
function.
int x, y ;
cout << “\n Enter the values of x and y:” ;
cin >> x >> y;
sum(x, y);
}
Example:
// This program illustrates the use of the inline functions
#include<iostream.h>
class Rectangle
{
private:
float length ;
float breadth ;
public:
Rectangle( ) // Default constructor, without any argument
{ length =0; breadth = 0; }
Rectangle(float l, float b) // Constructor with two arguments
{ length = l ; breadth = b ; }
void Enter_lb(void)
{
cout << "\n\t Enter the length of the rectangle: " ;
cin >> length ;
cout << "\t Enter the breadth of the rectangle: " ;
cin >> breadth ;
}
void Display_area(void)
{
cout << "\n\t The area of the rectangle = " <<
length*breadth ;
}
} ; // End of the class definition
Eg: Defining the inline functions outside the class
#include<iostream.h>
class Rectangle
{
private:
float length ;
float breadth ;
public:
Rectangle( ) ; // Default constructor, without any argument
void Enter_lb(void) ;
void Display_area(void) ;
A static member variable is available only within the class, but it continues to live
till the time program execution does not come to an end.
Note that the type and scope of each static member variable must be defined
outside the class definition. This is necessary because the static data members
are stored separately rather than as a part of an object.
They are also known as class variables, as they are associated with the class
itself rather than with any class objects.
Static member variables are normally used to maintain the values common to the
entire class. For example, there may be a situation when an object is required to
know how many other objects of its class are in existence.
Example
// This program illustrates the use of the static member variable in a class
#include<iostream.h>
class Objcount
{
private:
static int count ; // Member Variable declared as static
public:
Objcount( ) // Constructor
{
count++ ;
}
void display(void)
{
cout << "\n cout = " << count ;
}
};
//*********Definition of the Static Member Variable count***************
int Objcount :: count ; // count is initialized to 0 by default
void main( )
{
StaticSample s1
cout << “\nAfter defining first object of StaticSample class---->\n”;
s1.display();
StaticSample s2 ;
cout<<"\nAfter defining second objects of StaticSample class---->\n";
s1.display();
s2.display();
StaticSample s3;
cout << "\n\n After defining third object of StaticSample class---->\n";
s1.display();
s2.display();
s3.display();
}
Cont.
Output:
After defining one object of StaticSample class---->
count = 1
staticcount = 1
count = 1
staticcount = 2
count = 1
staticcount = 3
count = 1
staticcount = 3
staticcount=3
s1 s2 s3
count=1 count =1 count =1
Static member function:
• Like static member variables, the member functions can
also be declared as static.
class Two
{
int t;
public:
Two( )
{ t = 10; }
friend void sum(One, Two); //Friend function
};
Cont.
void sum(One one, Two two) // Friend function definition
{
int s = one.o + two.t ;
cout << “\n The sum = “ << s;
}
void main()
{
One o1;
Two t2;
sum(o1, t2);
}
Output:
The sum = 15
The const keyword
• To prevent the normal variables from
being modified.