CPP OOPS Module 5
CPP OOPS Module 5
Concept of Inheritance
class Doctor class Footballer class Businessman
Attributes: Attributes: Attributes:
Age, Height, Weight Age, Height, Weight Age, Height, Weight
Methods: Methods: Methods:
Talk() Talk() Talk()
Walk() Walk() Walk()
Eat() Eat() Eat()
Diagnose() Playfootball() Runbusiness()
All of the classes have common attributes (Age, Height, Weight)
and methods (Walk, Talk, Eat).
However, they have some special skills like Diagnose, Playfootball
and Runbusiness.
In each of the classes, you would be copying the same code for
Walk, Talk and Eat for each character.
Concept of Inheritance(Cont…)
class Person class Person
is called
Attributes:
Base class
Age, Height, Weight
Methods:
Talk() , Walk(), Eat()
These classes
are called
Derived class
Concept of Inheritance(Cont…)
class Vehicle
Attributes:
Engineno, color
Methods:
applybreaks()
Example:
class person{
//body of class Base Class
};
Access Mode
public
class doctor: protected
private person
{ Derived Class
//body of class
} By default access mode is
private
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance (also known as Virtual
Inheritance)
1. Single Inheritance
A • If a class is derived from a single class
then it is called single inheritance.
• Class B is derived from class A
B
Example:
class Animal
{ public:
int legs = 4;
};
class Dog : public Animal
{ public:
int tail = 1;
};
Single Inheritance Program
class Animal{ int main()
int legs=4; {
public: Animal a1;
void display1(){ Dog d1;
cout<<"\nLegs="<<legs; d1.display1();
} d1.display2();
}; }
class Dog : public Animal{
bool tail = true; Output:
public: Legs=4
void display2(){ Tail=1
cout<<"\nTail="<<tail;
}
};
2. Multilevel Inheritance
A • Any class is derived from a class which is derived
from another class then it is called multilevel
inheritance.
B
• Here, class C is derived from class B and class B
is derived from class A, so it is called multilevel
C inheritance.
Example:
class Person class ITStudent :public
{ Student
//content of class person {
}; //content of ITStudent
class Student :public Person class
{ };
//content of Student class
};
class Person{ int main()
public: {
void display1(){ Person p;
cout<<"\nPerson class"; Student s;
} ITStudent i;
}; p.display1();
class Student:public Person{ s.display2();
public: s.display1();
void display2(){ i.display3();
cout<<"\nStudent class"; i.display2();
} i.display1();
}; }
Output:
class ITStudent:public Student{ Person class
public: Student class
void display3(){ Person class
cout<<"\nITStudent class"; ITStudent class
} Student class
}; Person class
3. Multiple Inheritance
• If a class is derived from more than one
A B
class then it is called multiple inheritance.
• Here, class C is derived from two classes,
C class A and class B.
Example:
class Liquid class Petrol: public
{ Liquid, public Fuel
//content of Liquid class {
}; //content of Petrol
class Fuel class
{ };
//content of Fuel class
};
class Liquid{ int main()
public: {
void display1(){ Liquid l;
cout<<"\nLiquid class"; Fuel f;
} Petrol p;
}; l.display1();
class Fuel{ f.display2();
public: p.display3();
void display2(){ p.display2();
cout<<"\nFuel class"; p.display1();
} }
};
class Petrol:public Liquid,public
Fuel{ Output:
public: Liquid class
void display3(){ Fuel class
cout<<"\nPetrol class"; Petrol class
} Fuel class
}; Liquid class
4. Hierarchical Inheritance
• If one or more classes are derived
A
from one class then it is called
hierarchical inheritance.
• Here, class B, class C and class D are
B C D derived from class A.
Example:
class Animal class Horse :public Animal
{ {
//content of class Animal //content of class Horse
}; };
class Elephant :public Animal class Cow :public Animal
{ {
//content of class Elephant //content of class Cow
}; };
class Animal{ class Horse:public Animal{
public: public:
void display1(){ void display3(){
cout<<"\nAnimal Class"; cout<<"\nHorse class";
} }
}; };
class Elephant:public Animal{ class Cow:public Animal{
public: public:
void display2(){ void display4(){
cout<<"\nElephant class"; cout<<"\nCow class";
} }
}; };
int main(){ Output:
Animal a; Elephant e; Horse h; Cow c; Animal Class
a.display1(); Elephant class
e.display2(); e.display1(); Animal Class
h.display3(); h.display1(); Horse class
c.display4(); c.display1(); Animal Class
Cow class
}
Animal Class
5. Hybrid Inheritance
A
B C
class result
Attributes:
totalmarks
percentage
Protected access modifier
• Protected access modifier plays a key role in inheritance.
• Protected members of the class can be accessed within the
class and from derived class but cannot be accessed from any
other class or program.
• It works like public for derived class and private for other class.
class ABC { class XYZ : public ABC{
public: public:
void setProtMemb(int i){ void useProtfunc(){
m_protMemb = i; } Protfunc(); }
void Display(){ };
cout<<m_protMemb<<endl;}
protected:
int m_protMemb;
void Protfunc(){
cout<<"\nAccess allowed\n";}
};
int main() {
ABC a; XYZ x;
a.m_protMemb; //error, m_protMemb is protected
a.setProtMemb(0); //OK,uses public access function
a.Display();
a.Protfunc(); //error, Protfunc() is protected
x.setProtMemb(5); //OK,uses public access function
x.Display();
x.useProtfunc();} // OK, uses public access function
Class access modifiers
• public – Public members are visible to all classes.
• private – Private members are visible only to the class
to which they belong.
• protected – Protected members are visible only to
the class to which they belong, and derived classes.
Access modifiers
Base class How inherited base class
members
members appear in derived class
private: x x is inaccessible
public
protected: y protected: y
base class
public: z public: z
private: x x is inaccessible
private
protected:y private: y
base class
public: z private: z
private: x x is inaccessible
protected protected: y
protected:y
base class protected: z
public: z
class base{ class privateDerived: private base
private: {
int z; // x is private
public: // y is private
int x; // z is not accessible from
privateDerived
protected:
int y; };
};
class publicDerived: public base{
// x is public
// y is protected
// z is not accessible from publicDerived
};
class protectedDerived: protected base{
// x is protected
// y is protected
// z is not accessible from
protectedDerived
};
Inheritance using Public Access
class Grade class Test : public Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions;
When Test class inherits float pointsEach;
int numMissed;
from Grade class using public members:
public class access, it Test(int, int);
looks like this: void setScore(float);
float getScore();
char getLetter();
Inheritance using Private Access
class Grade class Test : private Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
float pointsEach;
When Test class inherits int numMissed;
from Grade class using void setScore(float);
private class access, it float getScore();
looks like this: float getLetter();
public members:
Test(int, int);
Inheritance using Protected Access
class Grade class Test : protected Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter(); private members:
int numQuestions:
float pointsEach;
When Test class inherits int numMissed;
public members:
from Grade class using Test(int, int);
protected class access, it protected members:
looks like this: void setScore(float);
float getScore();
float getLetter();
Visibility of inherited members
Derived class visibility
Base class
visibility
Public Private Protected
derivation derivation derivation
D
memberA Multiple copies of member A
Virtual base class (Cont…)
• Virtual base class is used to prevent the
duplication/ambiguity.
• In hybrid inheritance child class has two direct parents
which themselves have a common base class.
• So, the child class inherits the grandparent via two
separate paths. it is also called as indirect parent class.
• All the public and protected member of grandparent
are inherited twice into child.
• We can stop this duplication by making base class
virtual.
class A class D:public B, public C
{ {
public: public:
int i; int sum;
}; };
class B:virtual public A int main()
{ {
public: D ob1;
int j; ob1.i=10;
}; ob1.j=20;
class C: public virtual A ob1.k=30;
{ ob1.sum=ob1.i+ob1.j+ob1.k;
public: cout<<ob1.sum;
int k; }
};
Derived class constructor
class Base{
int x;
public:
Base() { cout << "Base default constructor"; }
};
class Derived : public Base
{ int y;
public:
Derived() { cout<<"Derived default constructor"; }
Derived(int i) { cout<<"Derived parameterized constructor";}
};
int main(){
Base b;
Derived d1;
Derived d2(10);
}
Derived class constructor (Cont…)
class Base int main()
{ int x; {
public: Derived d(10,20) ;
Base(int i){ }
x = i; cout << "x="<<x;
}
};
class Derived : public Base {
int y;
public:
Derived(int i,int j) : Base(j)
{ y = i; cout << "y="<<y;
}
};
Execution of base class constructor
Method of inheritance Order of execution
class Derived: public Base Base();
{ Derived();
};