OOPS in C++
OOPS in C++
OOPS in C++
Class: It is a user defined data type, which hold it’s own data members functions,
which can be accessed and used by creating an instance of that class.
Abstraction: Abstraction means displaying only essential information and hiding the
details.
• Abstraction using classes
• Abstraction using header files (math.h → pow())
Access Modifier:
• Public= It can be accessed by any class.
• Private= It can be accessed only by a function in a class (inaccessible outside of
a class).
• Protected= It is also inaccessible outside but can be accessed by subclass at
that class.
Note: If we do not specify any access modifier inside the class then by default the
access modifier for the member will be private.
Friend Class: A friend class can be access private and protected members of
other class in which it is declared as friend.
Ex: Friend class A;
Inheritance: Class subclass : accessmode baseclass
3. Multilevel Inheritance:
1. Single Inheritance: A B
A
2. Multiple Inheritance: A B
B
C C
4. Hierarchical Inheritance: A
B C
Inline function:
• Inline is a request, not command.
• It is function that is expanded in line when it is called. When the infinite function is
called, whole code get inserted or substituted at the point of inline function call.
Function overloading: It is a feature in C++ where two or more functions can have
same name but different parameters.
void print (int i)
cout << “Here is the integer “ << i <<
endl;
void print (float i)
cout << “Here is the float “ << i <<
endl;
int main()
print (10);
print (10.12);
return 0;
Difference between C and C++
C C++
1. C supports procedural programming. 1. C++ is known as hybrid language
because it supports both procedural
and object oriented programming.
7. C does not support exception handling. 7. C++ supports exception handling using
try and catch.
• Static function in a class: static member functions are allowed to access only
the static data members or other static member functions
Constructors:
➢ Constructor is an special member function of the class. It is automatically
invoked when an object is created.
➢ It has no return type.
➢ Constructor has the same name as class itself.
➢ If we do not specify, then C++ compiler generates a constructor for us.
Constructor
Default Parameterized Copy
Class_name() Class_name (parameters) Class_name (
constant class_name
&obj)
update () update ( int x, int y) update (constant
{ { update &P2)
A= 10; A= x; {
B= 20; B= y; A= P2 . a;
} } B= P2 . b;
}
Compiler generates two constructors by itself:
❑ Default Constructor
❑ Copy Constructor
But is any of the constructor of created by user, then default constructor will not
be created by compiler.
Constructor Overloading can be done just like function overloading.
Deep Copy is possible only with user defined constructors. In user defined copy
constructor, we make sure that pointers of copied object points to new memory
location.
Obj 1 Obj 2
Deep Copy
Destructor
➢ Destructor is a member function which destructs or delete an object.
➢ Destructor don’t take any argument and don’t have any return type.
➢ Only one destructor is possible.
➢ Destructor can not be static.
➢ Actually destructor doesn’t destroy object, it is the last function that invoked
before object destroy.
object resource
Destructor is used, so that before deletion of object, we can free space allocated
for this resource. B/C if object gets deletion then space allocated foe object will
be free but resource doesn’t.
Operating Overloading:
C++ have the ability to provide special meaning to the operator.
Ex:
Class Complex
{…
Complex operator + ( Complex & C1)
{ Complex res ;
res . a = C1 . a ;
res . b = C2 . b ;
}
}
int main ()
{
C = C1 + C2
}
As ‘+’ can’t add complex numbers directly, we can define a function with name +,
but we need to write operator keyword before it. So, we can use all operator like
this.
Friend Class
A friend class access the private and protected members of other class in which it
is declared as friend.
There can be friend class and friend function.
Ex:
Class Complex
{
Private:
double width;
Public:
friend void Printwidth(Complex complex);
void Setwidth(double wid);
}
void Complex : : Setwidth(double wid)
{ width = wid ; }
Types of Inheritance :
Single Inheritance: A
Class B : Public A
{
}; B
Multiple Inheritance:
Class A1 Class A2
{ { A1 A2
}; };
Class B : Public A1 , Public A2
{
}; B
Multilevel Inheritance:
Class B : Public A
A
{
};
Class C : Public B B
{
};
C
Hierarchical Inheritance :
Class B1 : Public A
A
{
};
Class B2 : Public A
{ B1 B2
};
Visibility mode
Private
A – base class Protected
B – sub class Public
Private
Protected
Public
Private
Class A : Public B
{
}
If visibility mode is private then both protected and public member of A will be
private member of B.
Constructor executed
Child Parent
object
B() : A() Constructor Call
{ Child constructor
}
Parent constructor
While in case of destructor first child
Complete parent
destructor executed, then parent
destructor executed.
Complete child
this Pointer
Every object in C++ has access to it’s own address through an important pointer
called this pointer.
Friend function doesn’t have a this pointer, b/c friends are not member of a class.
Only member function have this pointer.
Class Box
{ Private:
int l, b, h;
Public:
void set( int l, int b, int h )
{
this → l = l;
this → b = b;
this → h = h;
}
};
int main()
{ --- Box b;
b . set ( 5, 10, 4);
return 0;
}
Method Overriding ( achieved at run time )
It is redefinition of base class function in it’s derived class, with same return type
and same parameters.
While method overloading is achieved at compile time.
Ex:
Class Car
{ private:
int gear;
public:
void change_gear( int gear )
{
gear ++ ;
}
};
Shape draw ()
A pure virtual function in C++ is a virtual function for which we don’t have any
implementation, we only declare it.
// Abstract Class
1. A class is abstract if it has at least one pure virtual function. We can not
declare object of abstract class. Ex: Test t; // will show error
2. We can have pointer or reference of abstract class.
3. We can access the other functions except virtual by object of it’s derived class.
4. If we don’t override the pure virtual function is derived class then it become
abstract.
5. An abstract class can have constructors. ( Read from GFG )
Template in C++ :
template < class x > check a, xb
{
if ( a > b ){
return a;
}else {
return b;
}
}
It just help in data type. So that we can write generic function that can be used
for different data type.
Dynamic Constructor
When allocation of memory is done dynamically using dynamic memory allocator
‘new’ in constructor.
Class Geeks
{ Public :
void fun ()
{
char *P = new char ( b ) ;
}
};
int main ( )
{ Geeks g = Geeks ( );
}