chapter_5_-_inheritance
chapter_5_-_inheritance
Compiled By:
Sanket Shah
Inheritance
• Inheritance is the process, by which class can
acquire the properties and methods of another class.
• The mechanism of deriving a new class from an old
class is called inheritance.
• The new class is called derived class and old class is
called base class.
• The derived class may have all the features of the
base class and the programmer can add new features
to the derived class.
Type of Inheritance
Single Inheritance
• If a class is derived from a single class then it
is called single inheritance.
• Class B is derived from class A
Type of Inheritance
Multilevel Inheritance
• A class is derived from a class which is
derived from another class then it is called
multilevel inheritance
• Here, class C is derived from class B and class
B is derived from class A, so it is called
multilevel inheritance.
Type of Inheritance
Multiple Inheritance
• If a class is derived from more than one class
then it is called multiple inheritance.
• Here, class C is derived from two classes,
class A and class B.
Type of Inheritance
Hierarchical Inheritance
• If one or more classes are derived from one
class then it is called hierarchical inheritance.
• Here, class B, class C and class D are derived
from class A.
Type of Inheritance
Hybrid Inheritance
• It is a combination of any above 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.
Single Inheritance
Multilevel Inheritance
Multilevel Inheritance
Output
Multiple Inheritance
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A's constructor called" << endl;
}
};
class B
{
public:
B()
{
cout << "B's constructor called" << endl;
}
};
Multiple Inheritance
class C: public B, public A // Note the order
{
public:
C()
{
cout << "C's constructor called" << endl;
}
};
int main()
{
C c;
return 0;
}
The diamond problem
• The diamond problem occurs when two super
classes of a class have a common base class.
For example, in the following diagram, the TA
class gets two copies of all attributes of Person
class, this causes ambiguities.
#include<iostream>
using namespace std;
class A
{
// Data members of person
public:
int _a;
};
};
};
int main()
{
D d;
int w= d._a=10;
cout<<"W :"<<w<<endl;
int x=d._b=10;
cout<<"X :"<<x<<endl;
int y=d._c=10;
cout<<"Y :"<<y<<endl;
int z=d._d=10;
cout<<"Z :"<<z<<endl;
return 0;
}
Hierarchical Inheritance
#include<iostream>
using namespace std;
class A
{
public:
void dispA()
{
cout<<"class A method"<<endl;
}
};
}
};
Hybrid Inheritance
class result: public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total Score="<<total<<"\n";
}
Hybrid Inheritance
int main()
{
//clrscr();
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0;
}