Class Inheritance
Definition
The mechanism of deriving a new classes from old one
is called inheritance
There are five forms
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
The main idea behind inheritance is to reuse, enhance
and increase the reliability of the ready existing code
Forms of inheritance
Base
Base
Derived1
Derived
Derived 2
Derived n
Forms of inheritance
Base 1 Base 2 Base n
Derived
Base
Derived 1 Derived 2 Derived n
Forms of inheritance
Base
Derived 1 Derived 2
Derived
Defining the derived class
Class
Is a keyword
Derived class
Name of the derived class
:
Shows the derivation from the base class
Visibility mode
Specifies the type of derivations
Public
Private
Protected
Base class
Name of the base class
Types of derivation
Private
Public
Protected
Defining the derived class
Private
Student :base class
Private Int roll no;
Char name;
Public Void getdata();
Void putdata();
Test: derived class
Private Int marks
Void getdata();
Void putdata();
Public Void input();
Void output();
Private derivation
The public members of the base class are privately
inherited by a derived class
The private members of the base class are not
inherited by the derived class
The protected members of the base class becomes
private members to the derived class
Since the public members of the base class are now
private members in the derived class, they can be
accessed by the member function of the derived class
Defining the derived class
Public
Student :base class
Private Int roll no;
Char name;
Public Void getdata();
Void putdata();
Test: derived class
Private Int marks
Public
Void getdata();
Void putdata();
Void input();
Void output();
Public derivation
The public members of the base class become public
to the derived class
Again the private members of the base class cannot be
inherited to the derived class
The protected members of the base class stay
protected in the derived class
Defining the derived class
Protected inheritance
The public members of the base class become
protected to the derived class
Again the private members of the base class cannot be
inherited to the derived class
The protected members of the base class stay
protected in the derived class
Total summary
Base class Derived class visibility
visibility
Private inheritance Public inheritance Protected inheritance
Private Not inherited Not inherited Not inherited
Public Private Public Protected
Protected Private Protected Protected
Example single inheritance
#include <iostream.h>
#include <conio.h>
class father
{
private: char name[25];
int age;
public: char color[15];
char sex;
int IQ;
void getData();
void display();
};
class son : public father //derived class
{
private: char qualification[25];
float salary;
public: void read();
void print();
};
void father::getData()
{
cout<<"Enter the name : ";
cin >>name;
cout<<"Enter the age : ";
cin >>age;
cout<<"Enter the color: ";
cin >>color;
cout<<"Enter the sex : ";
cin >>sex;
cout<<"Enter the IQ : ";
cin >>IQ;
}
void father::display()
{
cout<<"Name = "<<name<<endl;
cout<<"Age = "<<age<<endl;
cout<<"Color = "<<color<<endl;
cout<<"Sex = "<<sex<<endl;
cout<<"IQ = "<<IQ<<endl;
}
void son :: read(void)
{
father::getData(); //accessable to the derived class
cout<<"Enter the qualification : ";
cin >>qualification;
cout<<"Enter the salary : ";
cin >>salary;
}
void son :: print(void)
{
father::display(); //accessable to the derived class
cout<<"Qualification = "<<qualification<<endl;
cout<<"Salary = "<<salary<<endl;
}
void main()
{
son s1;
cout<<"Enter son's information..."<<endl;
s1.read();
cout<<"Son's information is as follows..."<<endl;
s1.print();
}
Example multilevel inheritance
Grand father
Father
son
#include <iostream.h>
#include <conio.h>
class grandFather
{
private: char name[25];
int age;
public: char color[15];
void getData();
void display();
};
class father : public grandFather //derived class: level 1
{
private: char qualification[25];
float salary;
public: int IQ;
void read();
void print();
};
class son : public father //derived class: level 2
{
private: char hobby[20];
public : void input();
void output();
};
void grandFather::getData()
{
cout<<"Enter the name : ";
cin >>name;
cout<<"Enter the age : ";
cin >>age;
cout<<"Enter the color: ";
cin >>color;
}
void grandFather::display()
{
cout<<"Name = "<<name<<endl;
cout<<"Age = "<<age<<endl;
cout<<"Color = "<<color<<endl;
}
void father :: read(void)
{
grandFather::getData();
cout<<"Enter the qualification : ";
cin >>qualification;
cout<<"Enter the salary : ";
cin >>salary;
cout<<"Enter the IQ : ";
cin >>IQ;
}
void father :: print(void)
{
grandFather::display(); //accessable to the derived class
cout<<"Qualification = "<<qualification<<endl;
cout<<"Salary = "<<salary<<endl;
cout<<"IQ = "<<IQ<<endl;
}
void son :: input(void)
{
father::read(); //accessable to the derived class
cout<<"Enter the Hobby : ";
cin >>hobby;
}
void son :: output(void)
{
father::print(); //accessable to the derived class
cout<<"Hobby = "<<hobby<<endl;
}
void main()
{
son s1;
cout<<"Enter son's information..."<<endl;
s1.input();
cout<<"\nSon's information is as follows..."<<endl;
s1.output();
}
Multiple inheritance
It is the mechanism in which derived from several base
classes
The syntax is
King
Queen
Prince
#include <iostream.h>
#include <conio.h>
class king
{
protected: char name[25];
int age;
float weight;
public: int IQ;
void getData();
void display();
};
class queen
{
protected: char color[25];
float height;
public: void read(void);
void print(void);
};
class prince : public king, public queen //derived class
{
protected : char skill[20];
public : void getSkill(void);
void putSkill(void);
};
void king :: getData()
{
cout<<"Enter the name : ";
cin >>name;
cout<<"Enter the age : ";
cin >>age;
cout<<"Enter the weight : ";
cin >>weight;
cout<<"Enter the IQ : ";
cin >>IQ;
void king :: display()
{
cout<<"Name = "<<name<<endl;
cout<<"Age = "<<age<<endl;
}
void queen :: read(void)
{
cout<<"Enter the color : ";
cin >>color;
cout<<"Enter the height : ";
cin >>height;
}
void queen :: print(void)
{
cout<<"Color = "<<color <<endl;
cout<<"Height = "<<height<<endl;
}
void prince :: getSkill(void)
{
king ::getData(); //accessable to the derived class
queen::read(); //accessable to the derived class
cout<<"Enter the Skill : ";
cin >>skill;
}
void prince :: putSkill(void)
{
king ::display(); //accessable to the derived class
queen::print(); //accessable to the derived class
cout<<"Skill = "<<skill<<endl;
}
void main()
{
prince p;
cout<<"Enter prince's information..."<<endl;
p.getSkill();
cout<<"\nPrince's information is as follows..."<<endl;
p.putSkill();
}
Class n
Class m {
{ public:
public: void display()
void display() {
{ cout<<“Class n”;
cout<<“Class m”; }
} };
};
Class p: public m, public n
{
public: Void main()
void display() {
{ p p1;
m :: display(); p1.display();
} }
};
Ambiguity means to tell the compiler that which
function should call
We are telling the compiler to call otherwise the
compiler will not knew where to go
It may occur in single inheritance
Class p: public a
{
Class a public:
{ void display()
public: {
void display() cout<<“b”;
{ }
cout<<“Class a”; };
}
};
Void main()
{
p b;
b.display();
b.a::display();
b.b::display();
}
Use of constructor in base and derived classes
How the flow of constructors in base and derived class
Class base Class derived :public base
{
{
int x;
base() int y;
{
deriver()
x=10;
cout<<x; {
}
y=5;
};
cout<<y;
}
};
Void main()
{
derived d;
}
#include <iostream.h>
#include <conio.h>
#include <string.h>
class BC1
{
private: int i;
public: BC1(int x)
{
i=x;
cout<<"Constructor of BC1"<<endl;
}
void print_i()
{
cout<<"i = "<<i<<endl;
}
};
class BC2
{
private: float j,k;
public: BC2(float a, float b)
{
j=a;
k=b;
cout<<"Constructor of BC2"<<endl;
}
void print_jk()
{
cout<<"j = "<<j<<endl;
cout<<"k = "<<k<<endl;
}
};
class DC1 : public BC1, public BC2
{
private: char ch1, str[25];
public: DC1(int ival, float fval1, float fval2, char c1, char c2[]):
BC1(ival),
BC2(fval1, fval2)
{
ch1=c1;
strcpy(str, c2);
cout<<"Constructor of DC1"<<endl;
}
void print_Str()
{
cout<<"ch1 = "<<ch1<<endl;
cout<<"String = "<<str<<endl;
}
};
void main()
{
DC1 d(10, 12.25, 56.79, 'p', "Bangalore");
cout<<"\n";
d.print_i();
d.print_jk();
d.print_Str();
}
Points
The two points while using hierarchy
If the base class constructor does not take any
arguments, then the derived class need not contain the
constructor
If the base class constructor take any arguments, then
the derived class would pass the arguments to the base
class
Use of destructors
It’s the reverse order of the constructors
#include <iostream.h>
#include <conio.h>
class BC1
{
protected: int i;
public: BC1()
{
i=10;
cout<<"Constructor of BC1: i = "<<i<<endl;
}
~BC1() //destructor
{
cout<<"Destructor of BC1"<<endl;
}
};
class DC1 : public BC1
{
protected: int j;
public: DC1()
{
j=20;
cout<<"Constructor of DC1: j = "<<j<<endl;
}
~DC1() //destructor
{
cout<<"Destructor of DC1"<<endl;
}
};
class DC2 : public DC1
{
protected: int k;
public: DC2()
{
k=30;
cout<<"Constructor of DC2: k = "<<k<<endl;
}
~DC2() //destructor
{
cout<<"Destructor of DC2"<<endl;
}
};
void main()
{
DC2 d;
cout<<"\n";
}
Abstract base classes
An abstract base class is one which is used only for
deriving purpose
It cannot be used declaring any objects of its own
The main objective of an abstract base class is to
provide some traits to the derived class
This will help full in achieving the run-time
polymorphism