Inheritance
Is a method by which a class can inherit or adopt some or all the
properties of another class is called inheritance. Inheritance is a
process of deriving a class from another class. The one who
inherits the properties is called the derived class, subclass or
child class and the class whose properties are inherited is called
the base class, super class or parent class. When the derived
class inherits it gets everything of the parent class. The total
class the derived becomes is the union of both derived and
parent. Thus we create a new class based on other class. This is
also called reusability of the code.
Simple Inheritance
Program - Create a two classes name as student and student
marks. Student class inherits the student marks class and use
the data member of these class in the student marks class and
display it.
Constructor in both classes:
If the constructors in both base and derived classes. when we
create an object of the derived class because the derived class
contains the base class, the constructor of the base class is first
automatically invoked and then the constructor of the derived
class is invoked.
Program
#include<iostream>
#include<string.h>
using namespace std;
class Cylinder
{
private:
char name[15];
public:
Cylinder(char N[15])
strcpy(name,N);
void show()
cout<<"Name is "<<name<<endl;
};
class mycylinder:public Cylinder
private:
float baseRad,height;
public:
mycylinder(float B,float H,char C[15]):Cylinder(C)
baseRad=B;
height=H;
}
void showArea()
float area=3.142f*baseRad*baseRad*height;
show();
cout<<"Area is "<<area<<endl;
};
int main()
mycylinder c(3.0,4.0,"Cylinder 1");
c.showArea();
Private derivation further terminates the inheritance
#include<iostream>
#include<string.h>
using namespace std;
class base
public:
int base_public;
void show()
{
cout<<"Public variables in base =
"<<base_public<<endl;
};
class der:private base
public:
void show1()
base_public=5;
show();
};
int main()
der D;
D.show1();
Friend of friend is not a friend.
Program
#include<iostream>
#include<string.h>
using namespace std;
class base
int base_private;
friend der;
};
class der:private base
public:
void show()
base_private=5;
cout<<"base_private is = "<<base_private<<endl;
friend der1;
};
class der1:public der
void show2()
{
base_private=5; //error
int main()
der D;
D.show();
Types of Inheritance
1) Single Inheritance - in the single inheritance there is only one
super class and one sub class. In single inheritance, the class
hierarchy contains only two classes one is a base class and
another is a derived class.
2) Multilevel inheritance - in multilevel inheritance there will be
one super class and a derived class which is further derived to
any level. the multilevel inheritance will have more than two
levels of inheritance in the form of a chain of classes.
3) Hierarchical inheritance - in hierarchical inheritance many
sub classes exist for a single super class. in hierarchical
inheritance the top most base class contains all the features
common to the derived classes.
4) Multiple inheritance - in multiple inheritance a single
subclass can derive from many super classes. in multiple
inheritance a class can inherit behaviours and features from
more than one base classes.
5) Hybrid inheritance - hybrid inheritance is a mix of two or
more types of inheritance.
Multilevel inheritance
#include<iostream>
#include<string.h>
using namespace std;
class staff
private:
char address[25];
char empname[25];
public:
void getdata(char N[25],char A[25])
strcpy(empname,N);
strcpy(address,A);
void showdata()
cout<<"Name of Employee : "<<empname<<endl;
cout<<"Address : "<<address<<endl;
};
class clerk:public staff
private:
int empno;
char branch[10];
public:
void getdata(char N[25],char A[25],int E,char B[10])
staff::getdata(N,A);
empno=E;
strcpy(branch,B);
void showdata()
staff::showdata();
cout<<"Employee number: "<<empno<<endl;
cout<<"Branch : "<<branch<<endl;
};
class pay:public clerk
private:
int days;
public:
pay(char N[25],char A[25],int E,char B[10],int D)
clerk::getdata(N,A,E,B);
days=D;
}
void showdata()
clerk::showdata();
cout<<"Salary Payable :
"<<10000*days/31<<endl;
};
int main()
pay p("Ashwini","Mumbai",4,"Thane",27);
p.showdata();
Hierarchical Inheritance
#include<iostream>
#include<string.h>
using namespace std;
class Account
protected:
char accountno[10],name[10];
public:
void getacno(char A[10])
strcpy(A,accountno);
Account()
cout<<"Enter account number"<<endl;
cin>>accountno;
cout<<"Enter account holder name"<<endl;
cin>>name;
};
class Saving:public Account
int balance;
char A[10];
public:
Saving()
balance=0;
}
void deposit_money(int M)
balance=balance+M;
void withdraw_money(int M)
if(balance<M+200)
cout<<"\n No balance to withdraw money";
else
balance=balance-M;
void showbalance()
getacno(A);
cout<<"\nSaving Account no. : "<<A;
cout<<"\nName : "<<name;
cout<<"\nThe balance is "<<balance;
}
};
class Current:public Account
int balance;
char A[10];
public:
Current()
balance=0;
void deposit_money(int M)
balance=balance+M;
void withdraw_money(int M)
if(balance<M+5000)
cout<<"\n No balance to withdraw money";
}
else
balance=balance-M;
void showbalance()
getacno(A);
cout<<"\nCurrent Account no. : "<<A;
cout<<"\nName : "<<name;
cout<<"\nThe balance is "<<balance;
};
int main()
Current c;
Saving s;
int t=1;
int ch;
while(t)
{
cout<<"\n\n";
cout<<"\n1 : Deposit Money in Saving";
cout<<"\n2 : Withdraw Money in Saving";
cout<<"\n3 : Deposit Money in Current";
cout<<"\n4 : Withdraw Money in Current";
cout<<"\n5 : Exit";
cout<<"\n-------------------------";
cout<<"\n Choose option=>";
cin>>ch;
switch(ch)
case 1:
s.deposit_money(500);
s.showbalance();
break;
case 2:
s.withdraw_money(200);
s.showbalance();
break;
case 3:
c.deposit_money(5000);
c.showbalance();
break;
case 4:
c.withdraw_money(500);
c.showbalance();
break;
case 5:
exit(1);
Hybrid Inheritance
#include<iostream>
#define size 1
using namespace std;
class Staff
private:
int empno;
char name[20];
public:
void getdata()
cout<<"\n Enter employee number";
cin>>empno;
cout<<"\nEnter name";
cin>>name;
void putdata()
cout<<"\n"<<empno<<"\t"<<name<<"\t";
};
class Salary:public Staff
protected:
float basic,da;
public:
void getSalary()
{
cout<<"\nEnter basic salary";
cin>>basic;
cout<<"\nEnter DA";
cin>>da;
void putSalary()
cout<<basic<<"\t"<<da<<"\t";
};
class Overtime
protected:
float OT;
public:
void getOT()
cout<<"\n Enter Overtime";
cin>>OT;
void putOT()
{
cout<<OT<<"\t";
};
class totSalary:public Salary,public Overtime
private:
float total;
public:
void showTotal()
total=basic+da+OT;
cout<<total;
};
int main()
totSalary s[size];
for(int i=0; i<size; i++)
s[i].getdata();
s[i].getSalary();
s[i].getOT();
cout<<"------------------------\n";
cout<<"\nNo.\tName\tBasic\tDA\tOT\tTotal"<<endl;
for(int i=0; i<size; i++)
s[i].putdata();
s[i].putSalary();
s[i].putOT();
s[i].showTotal();
Ambiguity in Multiple Inheritance
In multiple inheritance, when a function with the same name
appears in more than base class. Consider the following two
classes
#include<iostream>
using namespace std;
class M
public:
void display()
{
cout<<"Class M\n";
};
class N
public:
void display()
cout<<"Class N\n";
};
class P:public M,public N
public:
void display()
M::display();
};
int main()
{
P p;
p.display();
Ambiguity may also arise in single inheritance applications. For
instance, consider the follwing situation
#include<iostream>
using namespace std;
class A
public:
void display()
cout<<"Class A\n";
};
class B:public A
public:
void display()
{
cout<<"Class B\n";
};
int main()
B b;
b.display();
b.A::display();
b.B::display();
Virtual Base classes
The duplication of inherited members due to these multiple paths
can be avoided by making the common base class (ancestor
class) as virtual base class while declaring the direct or
intermediate base classes as shown below
class A
----
----
};
class B1:virtual public A
-----
-----
};
class B2:public virtual A
-----
-----
};
class C:public B1,public B2
-----
-----
};
When a class is made a virtual base class, C++ takes necessary
care to see that only one copy of that class is inherited,
regardless of how many inheritance paths exist between the
virtual base class and a derived class.
#include<iostream>
using namespace std;
class Student
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
void put_number(void)
cout<<"Roll number : "<<roll_number<<"\n";
};
class Test:virtual public Student
protected:
float part1,part2;
public:
void get_marks(float x, float y)
part1=x;
part2=y;
void put_marks()
cout<<"Marks Obtained : \n";
cout<<"Part1 = "<<part1<<"\n";
cout<<"Part2 = "<<part2<<"\n";
};
class Sports:public virtual Student
protected:
float score;
public:
void get_score(float s)
score=s;
void put_score()
cout<<"Sports wt : "<<score<<"\n";
};
class Result:public Test,public Sports
float total;
public:
void display();
};
void Result::display()
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"Total score : "<<total<<"\n";
int main()
Result r;
r.get_number(678);
r.get_marks(30.5,35.5);
r.get_score(7.0);
r.display();