3. INDEX
INTRODUCTION OF INHERITANCE
ACCESS CONTROL IN INHERITANCE
TYPES OF INHERITANCE
VIRTUAL BASE CLASS & ABSTRACT CLASS
CONSTRUCTOR IN INHERITANCE
4. INHERITANCERITANCE
Inheritance is the mechanism as the capability of class to inherit properties from
another class
BASE CLASSRITANCE
It is the class whose properties are inherited by
another class it is also called super class
DERIVED CLASS
It is the class that inherit properties from base
class , it also called sub class
5. what is inheritance ?
mammal
Dog is a mammal it has all features of
mammals in addition to its own
unique features
cat is a mammal it has all features of
mammals in addition to its own
unique features
All mammals have certain
characteristics
dog cat
7. ACCESS CONTROL IN INHERITANCE
PRIVATE INHERITANCE PROTECTED INHERITANCE PUBLIC INHERITANCE
8. TANCE
PRIVATE PROTECTED PUBLIC
PRIVATE NOT INHERITED BECOME PRIVATE OF
CHILD CLASS
BECOME PRIVATE OF
CHILD CLASS
PROTECTED NOT INHERITED BECOME PROTECTED
MEMBER OF CHILD CLASS
BECOME PROTECTED OF
CHILD CLASSS
PUBLIC NOT INHERITED BECOME PROTECTED OF
CHILD CLASS
BECOME PUBLIC MEMBER
OF CHILD CLASS
MEMBER OF
BASE CLASS
MEMBER OF
BASE CLASS
MEMBER OF
BASE CLASS
INHERITANCE
TYPE
INHERITANCE
TYPE
INHERITANCE
TYPE
9. class Base
{
private
int a;
public:
Int b;
};
class Derived: private Base
{
int y;
public:
Int z;
};
In private inheritance protected
and public members of the base
class becomes the private
members of the derived class
PRIVATE INHERITANCE
10. class Base
{
private
int a;
public:
Int b;
};
class Derived: protected Base
{
int y;
public:
Int z;
};
In protected inheritance
protected and public members
of the base class becomes the
protected members of the
derived class
PROTECTED INHERITANCE
11. class Base
{
private
int a;
public:
Int b;
};
class Derived: public Base
{
int y;
public:
Int z;
}
In public inheritance,
protected members of the base
class becomes the protected
member of derived class
and public members of the base
class becomes the public
members of the derived class
PUBLIC INHERITANCE
13. SINGLE INHERITANCE
Single inheritance is the one here have a single base class and a
derived class
Base class
Derived class
EVOLUTION
INHERITANCE
14. SINGLE INHERITANCE
EXAMPLE
class Tire
{
public:
Int pressure = 14;
};
class car : public Tire
{
public:
Int TirePressure( )
{
return pressure;
}
};
main()
{
Car c1;
Cout<<“ pressure in tire is “<<c1.TirePressure()<<endl;
}
#include<iostream>
using namespace std;
Output : -
Pressure in tire is 14
15. MULTIPLE INHERITANCE
in multiple inheritance , a derived class inherits from multiple
base classes it has properties of all the base classes
Base class1 base class2 Base class3
Derived class
16. int main(void)
{
Phone p1;
return 0;
}
class camera
{
public:
camera () {
cout << " I am camera "
<<endl;
}
};
class calculator{
public:
calculator () {
cout << " i am calculator “ <<
endl;
}
};
I am calculator
I am camera
I am phone
Output : -
MULTIPLE INHERITANCE
EXAMPLE
class phone : public camera,
public calculator
{
public:
phone () {
cout << " i am phone " <<
endl;
}};
#include<iostream>
using namespace std;
17. MULTILEVEL INHERITANCE
in multilevel inheritance , a sub class inherits from a class
that it self inherits from another class
Tortilla
Flour
WHEAT
T
CLASS A
CLASS B
CLASS C
BASE CLASS
DERIVED
SECOND
CLASS
DERIVED
FIRST CLASS
18. MULTILEVEL INHERITANCE EXAMPLE
class country
{
public:
string countryN = " india ";
};
class state: public country
{
public:
string stateN= " madhaya pradesh
";
};
class district : public state
{
public: string districtN= " bhopal";
district(){
cout<<countryN<<","<<stateN<<","<
<districtN<<endl;
}};
int main()
{
district d1;
return 0;
}
Output : -
india , madhaya pradesh , bhopal
#include<iostream>
using namespace std;
19. HIERARCHICAL INHERITANCE
If a number of classes are derived from a single class . It is
called hierarchical inheritance
Civil eng.
computer
eng.
Mech. Eng.
ENGINEER
Derived1 class
Base class
Derived3 class
Derived2 class
20. HIERARCHICAL INHERITANCE EXAMPLE
class engineer
{
public:
string eng = "engineer";
};
class computer: public engineer
{
public:
string cs =" computer " +eng;
computer(){
cout<<cs<<endl;
}
};
class civil: public engineer
{
public:
string civ = "civil " + eng;
civil(){ cout<<civ<<endl;
}
};
class mech: public engineer
{
public:
string mec = " mecchanical " +
eng;
mech(){
cout<<mec<<endl;
}
};
int main()
{
civil c1;
computer c2;
mech c3;
}
Output : -
Civil engineer
computer engineer
mechanical engineer
#include<iostream>
using namespace std;
21. HYBRID INHERITANCE
it combine two or more types of inheritance . in this type of
inheritance we can have a mixture of different kind of
inheritances
low price
High price
low price
WHOLESALER
RETAILER CONSUMER
MULTILEVEL INHERITANCE
MULTIPLE
INHERITANCE
22. HYBRID INHERITANCE EXAMPLE
class wholesaler
{
public:
int price =10;
};
class consumer: virtual public retailer,
virtual public wholesaler
{
public:
consumer(){
cout<<"prices"<<NewPrice<<","<<p
rice<<endl;
}
};
class retailer: virtual public
wholesaler
{
public:
int commission = 2;
int NewPrice = price +
commission;
};
int main()
{
consumer c1;
}
Output : -
prices 12, 10
#include<iostream>
using namespace std;
23. NCE
VIRTUAL BASE CLASS IN
INHERITANCE
When classes are derived in the form of
hybrid inheritance there can be a problem by
which multiple copies of the base class
members come in the lowest level derived
class through the various intermediate
subclass , here the virtual base class come
for rescue.
24. Class base
{
public:
Int a;
};
Class derived1: public base
{
public:
Int b;
};
Class derived2: public base
{
public:
Int c;
};
Class derived3 : public derived1,
public derive2
{
public:
Int d;
};
Class derived1 contains
a and b
Class derived2 contains
a and c
Class derive3 contains
a , b
a, c and d
Here the class derived3
contains three copy data
member a
Because of intermediate
class
Before using virtual base class
25. Class base
{
public:
Int a;
};
Class derived1: virtual public base
{
public:
Int b;
};
Class derived2: virtual public base
{
public:
Int c;
};
Class derived3 : virtual public
derived1, public derive2
{
public:
Int d;
};
Class derived1 contains
a and b
Class derived2 contains
a and c
Class derived3 only
contains
a , b
c and d
Here the class derived3
contains three copy of
data member a
Because of intermediate
class
After using virtual base class
26. ABSTRACT CLASS IN INHERITANCE
it is only designed to be inherited
it provide a framework , upon which other classes
can be built
It is that has no instance and is not designed to create
objects
27. int main(void)
{
Derived d;
d.fun();
return 0;
}
class Derived: public Base
{
public:
void fun() { cout << "fun() called"; }
};
class Base
{
int x=10;
public:
virtual void fun() = 0;
int getX() { return x; }
};
Fun() called
Output : -
ABSTRACT CLASS EXAMPLE
#include<iostream>
using namespace std;
28. CONSTRUCTOR IN INHERITANCE
If a base class has a parametrized constructor it is
the duty child class to pass the parameters for the
class at the time of creating the object as the
derived class inherit the base class
When an object of a derived class is created the constructor
of the base class is executed first and later the constructor of
derived class
29. class base {
protected:
int i;
public: base(int x) {
i = x;
cout << "Constructing
base"<<endl;
}
} ;
class derived: public base {
int j;
public:
derived(int x, int y): base(y)
{
j = x;
cout << "Constructing derivedn “ ; }
void show() { cout << i << " " << j << "n"; }
};
#include<iostream>
using namespace std; Example of constructor in
inheritance
Output : -
int main()
{
derived ob(12, 99);
ob.show();
}
99 12
30. ADVANTAGE OF INHERITANCE
It saves the memory space and time
It increases the reliability and readability of the code
It saves the developing and testing effort