Classes Object PDF
Classes Object PDF
2019
Inheritance
Inheritance is the process of Acquiring the Properties of the class into Another class is
called Inheritance or we can say , we can Add additional features to an existing class.
Existing class is called “Super” class. New class is called
“Sub” class.
Polymorphism
Polymorphism Means the ability to take more than one form. In Polymorphism every
object React different things using same methods or we can say using one interface we
can perform different operation on different type of Objects.
Dynamic linking(Binding)
Calling of Polymorphic Methods at Runtime is called Dynamic Linking.
Linking means calling of methods.
Advantages of oop’s
Using inheritance, we can eliminate code and reuse of existing
code.
Using data hiding Programmer can secure programs.
Easier communication between Objects by message sending.
S/W Complexity can be easily managed.
Oop system can be update to large system.
Object:-
Any real time entity which has state and behavior is called “Object”.
Or
The instances (variable) of any class is called object.
OBJECT
State
(Data Member)
Behavior
(Member Function)
Example:-
Fruit Mango, Pineapple, Orange;
int x,y,z;
The difference between private and protected is that where Protected part can be
inherited from one class to another and Private part cannot be inherited (in any type of
the inheritance).
Inherited––> Protected
Inherited––> Public
Syntax:-
class <user-defined name >
{
private:
data members;
member functions;
protected:
data members;
member functions;
public:
data members;
member functions;
};
Note:- We cannot declare more than one data member with same name (neither in
same path , nor in different path),otherwise result will be error.
class first
{ int x , x; //Error
};
class first
{ private:
int x;
public:
int x;
};
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 4
AIM POINT INFOSYSTEM PVT. LTD. 2019
#WAP to show the accessibility of private data members outside the class:
#include<iostream.h>
class first void main()
{ private: {
int x,y; first f1,f2;
public: f1.get();
void get( ) f2.get();
{ f1.show();
cout<<"\n Enter x and y:"; f2.show();
cin>>x>>y; int a,b;
}
void show() a = f1 . retx( ) ;
{ b = f2 . rety( );
cout<<x<<" "<<y;
} cout<<"\n"<<a<<" "<<b;
int retx( ) }
{ return x ; }
int rety( )
{ return y ;}
};
Note:- For small function (without control statement) inline method is better than offline
method and for large function (with control statement)offline method is better than inline
method.
ARRAY OF OBJECTS
void main( )
{
#include<iostream.h>
record r[10];
class record
cout<<sizeof(r)<<" "<<sizeof(r[10]);
{
for(int i=0;i<10;i++)
int rno;
r[ i ].get();
char rname[20];
for(i=0;i<10;i++)
float price;
r[ i ].show();
public:
}
void get()
{
cout<<"\n Enter rno,rname,price";
cin>>rno>>rname>>price;
}
void show()
{
cout<<"\n"<<rno<<" "<<rname<<""<<price;
}
};
#include<iostream.h>
void main( )
class complex
{
{
complex c1,c2,c3;
private:
c1.get();
int x,y;
c2.get();
public:
c1.show();
void get( )
c2.show();
{
c3 = c1 . sum (c2 ) ;
cout<<"\n Enter x and y:";
c3.show();
cin>>x>>y;
}
}
void show( )
{ if (y<0)
cout<<"\n"<<x<<y<<"j";
else
cout<<"\n"<<x<<"+"<<y<<"j";
}
complex sum(complex c)
{
complex t;
t.x = x + c.x;
t.y = y + c.y;
return t;
}
};
FRIEND FUNCTION
For friendship the prototype of this function must be declared inside the class
with keyword friend.
FRIEND CLASS
If there are two classes named “first” & “second” and if we grant friendship from class
“first” to class “second”, then every member function of class “second” can access
private and protected members of class “first”. But in this case no member function of
class “first” can access private and protected members of “second” class.
Two classes can grant its friendship to each other and in this case every member
function of both classes can access private and protected member of another class.
#include<iostream.h>
class first void main( )
{ {
private: first f1;
int x,y; second s1;
friend class second; s1.getfirst(f1);
}; s1.showfirst(f1);
class second }
{
public:
void getfirst(first &f)
{
cout<<"\n Enter x and y of class first:";
cin>>f.x>>f.y;
}
void showfirst(first &f)
{
cout<<"\n"<<f.x<<" "<<f.y;
}
};
NESTED CLASS
Example 1:-
#include<iostream.h> void main( )
class out {
{ int x, y; out o1;
class in cout<<sizeof(o1);
{ int l, m; o1.i1.get( );
public: o1.get( );
void get( ) o1.i1.show( );
{ o1.show( );
cout<<"\n Enter l and m:"; }
cin>>l>>m;
}
void show( )
{
cout<<"\n"<<l<<" "<<m;
}
};
public:
in i1;
void get( )
{
cout<<"\n Enter x and y";
cin>>x>>y;
}
void show( )
{
cout<<"\n"<<x<<" "<<y;
}
};
1. It uses “static data members” and local variables means it does not use “non-
static data members” (otherwise result will error).
2. It does not need to call by invoking object because it is call by the “class name”
only.
3. It can be defined by Inline/Offline method.
4. It is also associated with class not with object.
Example:-
class constant
{
const int x, y;
};
void main( )
{
constant c1, c2;//Error
}
1. In “const” member function we cannot modify the value of any data member,
otherwise result will be error.
2. It can be inline/offline.
Example:-
#include<iostream.h> void main( )
class constant {
{ int x, y; constant c1;
public: c1.get();
void get() c1.show();
{ cout<<"\n Enter x and y"; c1.call();
cin>>x>>y; c1.show();
} }
void show()
{ cout<<"\n"<<x<<" "<<y;
}
void call( ) const
{
int a, b;
a=x;
b=y;
x=10;//Error
y=20;//Error
}
};
“this” POINTER
“this” pointer points to the invoked object in member function or we can say “this “
pointer hold the address of invoking object in member function. It is very useful concept
whenever we want to refer invoking object in member function .