Professional Documents
Culture Documents
5 Day Inheritance C++
5 Day Inheritance C++
5 Day Inheritance C++
C++ Inheritance
Inheritance in Object Oriented Programming can be described as
a process of creating new classes from existing classes.
Inheritance Syntax
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
Asterisc Computer Institute C++
Single Inheritance
In this type of inheritance one derived class inherits from only one
base class. It is the most simplest form of Inheritance.
Multiple Inheritance
In this type of inheritance a single derived class may inherit from two
or more than two base classes.
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
Asterisc Computer Institute C++
Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits
from some other class. The Super class for one, is sub class for the other.
Single Inheritance
Single inheritance represents a form of inheritance when there is only one base class and
#include <iostream>
class A
public:
intx,y;
void assign()
cin>>x;
cin>>y;
}
Asterisc Computer Institute C++
void addition()
cout<<"\nAddition "<<x+y;
};
class B:public A
public:
void subtraction()
cout<<"\nSubtraction : "<<x-y;
};
int main()
B b;
b.assign();
b.addition();
Asterisc Computer Institute C++
cout<<"\nFor Subtraction : ";
b.assign();
b.subtraction();
return 0;
MultiLevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits
from some other class. The Super class for one, is sub class for the other
#include <iostream>
class A
public:
intx,y;
void assign()
cin>>x;
cin>>y;
void addition()
cout<<"\nAddition "<<x+y;
Asterisc Computer Institute C++
}
};
class B:public A
public:
void subtraction()
cout<<"\nSubtraction : "<<x-y;
};
class C:public B
public:
void multiplication()
cout<<"\nMultiplication : "<<x*y;
};
int main()
C c;
c.assign();
c.addition();
Asterisc Computer Institute C++
c.assign();
c.subtraction();
c.assign();
c.multiplication();
return 0;
Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two
base classes.
#include <iostream>
classStudDetail
public :
string name;
introllno;
voidacceptStudDetail()
cin>>rollno;
Asterisc Computer Institute C++
cout<<"\nEnter Name : ";
cin>>name;
};
classExamDetail
public :
int m1,m2,m3,m4,m5;
voidacceptExamDetail()
cin>>m1;
cin>>m2;
cin>>m3;
cin>>m4;
cin>>m5;
};
Asterisc Computer Institute C++
class Stud : public StudDetail,publicExamDetail
public:
int total;
voidcal()
total=m1+m2+m3+m4+m5;
void show()
cout<<"\nName : "<<name;
cout<<"\nMark1 : "<<m1;
cout<<"\nMark2 : "<<m2;
cout<<"\nMark3 : "<<m3;
cout<<"\nMark4 : "<<m4;
cout<<"\nMark5 : "<<m5;
cout<<"\nTotal : "<<total;
};
int main()
Asterisc Computer Institute C++
{
Stud s;
s.acceptStudDetail();
s.acceptExamDetail();
s.cal();
s.show();
return 0;
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
#include <iostream>
class A
public:
int x;
void accept()
cin>>x;
};
Asterisc Computer Institute C++
class B:public A
public:
voidfindSquere()
cout<<"\nSquere : "<<x*x;
};
class C:public A
public:
voidfindCube()
cout<<"\nCube : "<<x*x*x;
};
int main()
B b;
C c;
cout<<"\nForSque re : ";
Asterisc Computer Institute C++
b.accept();
b.findSquere();
c.accept();
c.findCube();
return 0;
#include <iostream>
class mm
{
protected:
introllno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout<< "Roll Number Is:"<<rollno<< "\n"; }
}; class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(intx,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout<< "Subject 1:" << sub1 << "\n";
cout<< "Subject 2:" << sub2 << "\n";
}
Asterisc Computer Institute C++
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{e=s;}
void put_extra(void)
{ cout<< "Extra Score::" << e << "\n";}
};
class res : public marks, public extra{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout<< "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}