Class Inheritance
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
Derived
Base
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();
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();
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();
};
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;
}
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 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