0% found this document useful (0 votes)
106 views

Classes Object PDF

The document discusses key concepts of object-oriented programming in C++ including classes, objects, data abstraction, encapsulation, inheritance, polymorphism, and advantages of OOP. It provides examples of classes with data members and member functions. The differences between private, protected, and public access specifiers are explained. Array of objects and defining member functions as inline and offline are also covered.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Classes Object PDF

The document discusses key concepts of object-oriented programming in C++ including classes, objects, data abstraction, encapsulation, inheritance, polymorphism, and advantages of OOP. It provides examples of classes with data members and member functions. The differences between private, protected, and public access specifiers are explained. Array of objects and defining member functions as inline and offline are also covered.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

AIM POINT INFOSYSTEM PVT. LTD.

2019

Oop’s concepts In C++


Oop’s concepts
Class and Object
Object :- Object is the basic Runtime Entities in the object oriented system which may
be refer to a person place Etc or any item.
Object
State
Behaviour

Object is the instance of class or variable of class

Class:-Class is the classification of object ,The logical Definition of object is called


class. Class is also referred to “user defined data type‟ ,Which defined data
(attributes) and Function (Methods) to manipulate data.
Example Fruit , Mango ,Pineapple,

Data Abstractions And Encapsulation

The Wrapping (Binding) of data And Methods in to a single units is called


encapsulations And this data is not accessible from out side the class ,Means
accessible only by Methods those are defined inside the class and It is also called data
hiding .

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.

Compile time Polymorphism: calling of polymorphic methods of compile time


Runtime Polymorphism : calling of Polymorphic methods at Run time.

Dynamic linking(Binding)
Calling of Polymorphic Methods at Runtime is called Dynamic Linking.
Linking means calling of methods.

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 1


AIM POINT INFOSYSTEM PVT. LTD. 2019

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.

Examples of Object Oriented programming Language are C++ , Java , .Net


,AutoCAD, PL/SQL, etc.
Object Based Language does not support inheritance and dynamic linking. Example:-
ADA

Topic – 1 (CLASSES AND OBJECTS)

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;

Classes:-The logical description or definition of any object is called “Class”. In C++,


class is a user-defined data type (Same as the Structure). It has three access specifies :
private, protected and public. Only public members are accessible from outside the
class, means private and protected members are not accessible from outside the
class.

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 2


AIM POINT INFOSYSTEM PVT. LTD. 2019

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).

CLASS Not Inherited Private

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:-Every part of class is an optional part.


#WAP to demonstrate the accessibility of different members:
#include<iostream.h>
class first void main()
{ private: { cout<<sizeof(first);
int x; first f1;
protected: cout<<sizeof(f1);
int y; f1.x=10;
public: f1.y=20;
int z; f1.z=30;
}; }

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 3


AIM POINT INFOSYSTEM PVT. LTD. 2019

#WAP to demonstrate data member and member function:


#include<iostream.h>
class first void main()
{ private: {
int x,y; //Data member first f1,f2;
public: f1.get();
void get( ) //Member function f1.show( );
{ cout<<"\n Enter x and y: "; f2.get( );
cin>>x>>y; f2.show( );
} }
void show( )
{
cout<<"\n"<<x<<" "<<y;
}
};

#WAP to demonstrate of same local and data members:


#include<iostream.h>
class first void main( )
{ {
private: first f1;
int x,y; //Data Member cout<<sizeof(f1);
public: f1.get();
void get() f1.show();
{ int x,y; //Local }
cout<<"\n Enter the local x and y";
cin>>x>>y;
cout<<"\n Enter the Data member x and y:";
cin>>first::x>>first::y;
}
void show()
{
cout<<"\n"<<x<<" "<<y;
}
};

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 ;}
};

Methods Of Defining Member Function:- int first::retx()


{ return x;
 Inline member function. }
 Offline member function. int first::rety()
#Example Of The Offline Member Function:- { return y;
#include<iostream.h> }
class first };
{ private:
int x,y;
public:
void get();
void show( ); void main()
int retx( ); { first f1,f2;
int rety( ); f1.get();
}; f2.get();
void first::get( ) f1.show();
{ cout<<"\n Enter x and y:"; f2.show();
cin>>x>>y; int a,b;
} a=f1.x;//error
void first::show( ) b=f2.y;//error
{ a=f1.retx();
cout<<"\n"<<x<<" "<<y; b=f2.rety();
} cout<<"\n"<<a<<" "<<b;
}

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 5


AIM POINT INFOSYSTEM PVT. LTD. 2019

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;
}
};

POINTER WITH OBJECT


void main( )
#include<iostream.h> {
class record record r1,*p;
{ p=&r1;
int rno; //cin>>p->rno>>p->rname>>p->price ;
char rname[20]; p->get( );
float price; p->show( );
public: }
void get( )
{
cout<<"\n Enter rno ,rname , price";
cin>>rno>>rname>>price;
}
void show( )
{
cout<<"\n"<<rno<<" "<<rname<<" "<<price;
}
};

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 6


AIM POINT INFOSYSTEM PVT. LTD. 2019

ADDITION OF TWO OBJECTS

#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;
}
};

SWAPPING OF TWO OBJECTS

#include<iostream.h> void main( )


class complex {
{ complex c1,c2,t;
private: c1.get( );
int x,y; c2.get( );
public: c1.show( );
void get() c2.show( );
{ -------------- t=c1;
} c1=c2;
void show() c2=t;
{ cout<<"\n after swapping";
--------------- c1.show(); c2.show();
--------------- }
}
};

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 7


AIM POINT INFOSYSTEM PVT. LTD. 2019

FRIEND FUNCTION

“friend” is a keyword, it is a function which can be non-member function or can be the


member function of another class. By default no nonmember function can access
private and protected member of any class but if any class grant its friendship to this
nonmember function then that nonmember function can also access private and
protected members of this class and this function is called “friend” Function.

For friendship the prototype of this function must be declared inside the class
with keyword friend.

Case 1:- Friendship to the nonmember function


#include<iostream.h>
#include<string.h>
class record
{ void main( )
int rno; {
char rname[20]; record r1;
float price; r1.get ( );
public: r1 . show ( ) ;
void get( ) modify( r1);
{ r1.show( );
cout<<"\n Enter rno ,rname , price"; }
cin>>rno>>rname>>price;
}
void show( )
{
cout<<"\n"<<rno<<" "<<rname<<""<<price;
}
friend void modify(record &);
};
void modify(record &r)
{
r.show( );
r.rno=205;
strcpy(r.rname,"KB");
r.price=880;
r.show( );
}

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 8


AIM POINT INFOSYSTEM PVT. LTD. 2019

Case 2:- Friendship to the nonmember function


#include<iostream.h>
class second;//Forward Declaration first sum(first f, second s)
class first {
{ int x,y; first t;
public: t.x=f.x+s.l;
void get() t.y=f.y+s.m;
{ return t;
cout<<"\n Enter x and y"; }
cin>>x>>y; void main()
} {
void show() first f1,f2;
{ second s1;
cout<<"\n"<<x<<" "<<y; f1.get();
} s1.get();
friend first sum(first,second); f1.show();
}; s1.show();
class second f2=sum(f1, s1);
{ f2.show();
int l, m; }
public:
void get()
{
cout<<"\n Enter l, m";
cin>>l>>m;
}
void show()
{
cout<<"\n"<<l<<" "<<m;
}
friend first sum(first,second);
};

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 9


AIM POINT INFOSYSTEM PVT. LTD. 2019

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.

It does not support transitive relationship.


A–––––>B––––––>C

#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;
}
};

Note:-Friendship can be granted from any part of the class.

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 10


AIM POINT INFOSYSTEM PVT. LTD. 2019

NESTED CLASS

Class inside of class is called “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;
}
};

STATIC DATA MEMBER

1. It is automatically initialized by zero but can be initialized by another value also.


2. Its memory does not count in the memory of its object because only one copy of
“Static” data member is created by the compiler and all objects shares it.
3. It must be declared inside the class and define outside the class (memory
allocated).
4. It can be modified.
5. It is allocated with class not with object.
6. Used to generate automatically counter.

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 11


AIM POINT INFOSYSTEM PVT. LTD. 2019

Example of Static Data Member:- void main( )


#include<iostream.h> {
class stat stat s1, s2, s3;
{ static int y ; //Declare
public: cout<<sizeof(s1)<<sizeof(s2)<<
void call( ) sizeof(s3);
{ s1.call( );
y++; s2.call( );
cout<<"\n"<<y; s3.call( );
} s3.call( );
}; s2.call( );
int stat::y; //Definition (Memory Created) s1.call( );
or }
int stat::y=50;//Definition with Initialization.

STATIC MEMBER FUNCTION

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 of Inline “Static Member Function”:-


#include<iostream.h>
class stat void main( )
{ {
int x; stat s1, s2, s3;
static int y; stat :: call( );
public: stat :: call( );
static void call() stat :: call( );
{ s3.call( );
int z=10;//Local s2.call( );
x++; //Non static s1.call( );
y++;//Static }
cout<<" "<<y;
}
};
int stat :: y;

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 12


AIM POINT INFOSYSTEM PVT. LTD. 2019

“CONST” DATA MEMBER

1. It must be initialized otherwise result will be error.


2. During execution, it cannot be modified.

Example:-
class constant
{
const int x, y;
};
void main( )
{
constant c1, c2;//Error
}

“CONST” MEMBER FUNCTION

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
}
};

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 13


AIM POINT INFOSYSTEM PVT. LTD. 2019

“this” POINTER

“this” is a keyword. It is a special type of pointer of C++ language which is accessed


only in member function means we cannot use “this” pointer outside the member
function (otherwise result will be error).

“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 .

Example of “this pointer”:-


#include<iostream.h>
class demo void main( )
{ { demo d1, d2;
int x, y; d1.get();
public: d1.show();
void get( ) d2=d1.retobj();
{ d2.show();
int x, y; cout<<this;//Error
cout<<"\n Enter local x and y:"; }
cin>>x>>y;
cout<<"\n Enter D.M. x and y";
cin>>this->x>>this->y;
}
void show()
{
cout<<"\n"<<x<<" "<<y;
}
demo retobj( )
{
return *this;
}
};

FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 14

You might also like