Unit 5
Unit 5
Inheritance
Inheritance
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 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{ O
} utput:
bool tail = true; Legs=4
public: Tail=1
void display2(){
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
class Student :public };
Person
{
//content of Student
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 class
A B
then it is called multiple inheritance.
Here, class C is derived from two classes, class A
C and class B.
Example:
class Liquid class Petrol: public
{ Liquid, public Fuel
//content of Liquid {
class //content of
}; Petrol class
class Fuel };
{
//content of Fuel class
};
class Liquid{ int main()
public: {
void display1(){ Liquid l;
cout<<"\nLiquid class"; Fuel f;
} Petrol p;
};
class Fuel{ l.display1();
public:
void display2(){ f.display2();
cout<<"\nFuel class";
} p.display3();
};
class Petrol:public Liquid,public p.display2();
Fuel{ Output:
public: Liquid class
p.display1();
void display3(){ } Fuel class
cout<<"\nPetrol class"; Petrol class
} Fuel class
}; Liquid class
4. Hierarchical Inheritance
If one or more classes are derived from
A
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
public: Animal{
void display1(){ public:
cout<<"\nAnimal Class"; void display3(){
} cout<<"\nHorse
}; class";
class Elephant:public Animal{ }
public: };
void display2(){ class Cow:public Animal{
cout<<"\nElephant class"; public:
} void display4(){
}; cout<<"\nCow class";
}
int main(){ Output:
};
Animal a; Elephant e; Horse h; Cow Animal Class
c; Elephant class
a.display1(); Animal Class
e.display2(); e.display1(); Horse class
h.display3(); h.display1(); Animal Class
Cow class
c.display4(); c.display1();
Animal Class
}
5. Hybrid Inheritance
A
B C
D
It is a combination of any other inheritance types. That is either
multiple or multilevel or hierarchical or any other combination.
Here, class B and class C are derived from class A and class D is
derived from class B and class C.
class A, class B and class C is example of Hierarchical Inheritance
and class B, class C and class D is example of Multiple Inheritance
so this hybrid inheritance is combination of Hierarchical and
Multiple Inheritance.
Hybrid Inheritance (Cont…)
class Car
{
//content of class Car
};
class FuelCar:public Car
{
//content of class FuelCar
};
Class ElectricCar:public Car
{
//content of class ElectricCar
};
Class HybridCar:public FuelCar, public
ElectricCar
{
//content of classHybridCar
};
class Car{ class ElecCar:public Car{
public: public:
void display1(){ void display3(){
cout<<"\nCar class"; cout<<"\nElecCar
} class";
}; }
class FuelCar:public Car{ };
public: class HybridCar:public
void display2(){ FuelCar, public ElecCar{
cout<<"\nFuelCar public:
class"; void display4(){
} cout<<"\nHybridCar
}; class";
int main(){ }
Car c; FuelCar f; ElecCar }; Output:
e; HybridCar class
HybridCar h; ElecCar class
h.display4(); FuelCar class
h.display3();
h.display2();
GTU Program
Create a class student that stores roll_no, name. Create a class
test that stores marks obtained in five subjects. Class result
derived from student and test contains the total marks and
percentage obtained in test. Input and display information of a
student. class student Class test
Attributes:
Attributes:
roll_no
marks[5]
name
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
public: ABC{
void setProtMemb(int i){ public:
m_protMemb = i; } void useProtfunc(){
void Display(){ Protfunc(); }
cout<<m_protMemb<<endl;} };
protected:
int m_protMemb;
void Protfunc(){
cout<<"\nAccess allowed\
n";}
}; main() {
int
ABC a; XYZ x;
a.m_protMemb; //error, m_protMemb is
protected public access
a.setProtMemb(0);//OK,uses
a.Display(); function
a.Protfunc(); //error, Protfunc() is
protected public access
x.setProtMemb(5);//OK,uses
x.Display(); function
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
private: base
int z; {
public: // x is private
int x; // y is private
protected: // z is not accessible from
privateDerived
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
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: {
int i; public:
}; int sum;
class B:virtual public A };
{ int main()
public: {
int j; D ob1;
}; ob1.i=10;
class C: public virtual ob1.j=20;
A ob1.k=30;
{ ob1.sum=ob1.i+ob1.j+ob1.
public: k;
int k; cout<<ob1.sum;
}; }
Derived class constructor
class Base{
int x;
public:
Base() { cout << "Base default constrictors"; }
};
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
Base(int i){ d(10,20) ;
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();
};