Inheritance
Inheritance
Inheritance
Introduction
Inheritance
New classes created from existing classes
Extends Reusability of existing attributes and
behaviors
The existing class is called as Base class or Super
class or Parent class.
Derived class
Class that inherits data members and member
functions from the existing class.
It is also called as Sub class or Child class.
Types of Inheritance
C
Types of Inheritance (Contd..)
A
– Hierarchical inheritance: The traits of one
base class are derived by several child
classes. B C D
more inheritances.
B C
D
class X{
X
};
class Y : visibility-mode X{ Y
};
class Y : private X
{
// Class Y now inherits the members of Class X privately
}
Private Protected
Data Members can be Can be accessed by the
accessed only by member function of the
member function of the same class
same class. Data Members can be
Data Members cannot be accessed by the member
accessed outside the function of the
class immediate derived class.
However, in that class it
is private and hence it
cannot be further
inherited.
Single Inheritance
class X{ class Y: public X{
int a; int c;
public: public:
int b; void set()
void get() { c=15;}
{ a=5; b=10;} void show()
void disp() { cout<<c; }
{ cout<<a<<b; } };
};
main(){
Y y1;
y1.get();
y1.disp(); // 5, 10
y1.set();
y1.show(); // 15
}
Multiple Inheritance
class M
{ class P:public M,public N int main()
protected: { {
int m; public: P p;
public: void display(void); p.get_m(10);
void get_m(int x) }; p.get_n(20);
{ m=x; } void P::display(void) p.display();
}; { return 0;
class N cout<<”m=” <<m <<”\n”; }
{ cout<<”n=” <<n <<”\n”;
protected: cout<<”m*n=” <<m *n<<”\n”;
int n; }
public:
void get_n(int y)
{ n=y; } OUTPUT:
}; m=10
n=20
m*n=200
Ambiguity in Multiple Inheritance
class X{ class Y{
public: public:
void disp() void disp()
{ cout<<“class X” ; { cout<<“class Y” ;
} }
}; };
main(){
class Z:public X,public Y Z z1;
{ z1.X::disp(); // class X
public: z1.Y::disp(); // class Y
void disp() z1.Z::disp(); // class Z
{ cout<<“class Z” ; } z1.disp(); // class Z
}; }
Multilevel inheritance
class student class test:public student class result:public test
{ { {
protected: protected: float total;
int roll_no; float sub1, sub2; public:
public: public: void display(void){
void get_no(int a) void get_marks(float x,float y) total=sub1+sub2;
{ roll_no=a; } { sub1=x; sub2=y; } put_no();
void put_no(){ put_marks();
cout<<”Roll void put_marks(void){ cout<<”total=”<<total;}
number:”<<roll_no ;} cout<<”marks in };
}; sub1=”<<sub1<<”\n”;
cout<<”marks in
OUTPUT: sub2=”<<sub2<<”\n”;
Roll number: 111 } int main(){
Marks in sub1=75 }; result student1;
Marks in sub2=59.5 student1.get_no(111);
Total=134.5 student1.get_marks(75.0,
59.5);
student1.display();
return 0;
}
class student
{ Hierarchical inhertance
protected:
int roll_no;
public: class test:public student class sports:public student
void get_no(int a) { {
{ roll_no=a; } protected: protected:
void put_no(void){ float sub1; float sub2; float score;
cout<<”roll_no:”<< public: public:
roll_no; } void get_marks(float x,float y) void get_score(float s)
}; { sub1=x; sub2=y; } { score=s; }
void put_score(void)
int main(){ void put_marks(void){ {
test t; cout<<”marks in cout<<”sports
sports s; sub1=”<<sub1<<”\n”; wt:”<<score<<”\n”;
t.get_no(110); cout<<”marks in } };
t.get_marks(75.0,59.5); sub2=”<<sub2<<”\n”;
s.get_no(220); }
s.get_score(78); };
t.put_no(); // 110
t.put_marks(); // 75, 59.5
s.put_no(); // 220
s.put_score(); // 78
}
class student class sports
{ Hybrid inheritance {
protected: protected:
int roll_no; float score;
public: public:
void get_no(int a) void get_score(float s)
{ roll_no=a; } { score=s; }
void put_no(void){ void put_score(void)
cout<<”roll_no:”<< {
roll_no; } cout<<”sports
}; wt:”<<score<<”\n”;
} };
class test:public student
{ class result:public test,public sports{
protected: float total;
float part1,part2; public:
public: void display(void){
void get_marks(float x,float y) total=part1+part2+score;
{ part1=x; part2=y; } put_no();
void put_marks(void) { put_marks();
cout<<”marks obtained:”<<”\n” put_score();
<<”Part1=”<<part1<<”\n” cout<<”total score:”<<total<<”\n”;
<<”Part2=”<<part2<<”\n”; }
} }; };
Hybrid inheritance (Contd..)
int main()
{ OUTPUT:
result student1; Roll_no:1234
student1.get_no(1234); Marks obtained:
student1.get_marks(27.5,33.0); Part1=27.5
student1.get_score(6.0); Part2=33
student1.display(); Sports wt:6
return 0; Total score=66.5
}
Virtual base class
Two copies of the traits of Stud will appear in
Stud
Result class, that will lead to ambiguity.
The solution is to make the base class Stud as a Test Sport
virtual base class.
This can be done by specifying virtual keyword Result
while defining the direct derived classes.
class Test:public virtual Stud class Sport:virtual public Stud
{ {
}; };
main(){
C c1;
c1.disp();
}
Assignment 1
Student
Roll
Test
getRoll()
Mark1
PrintRoll() Result
Mark2
Total
getMark()
displayMark()
PrintMark()
Output:Detail information about 10 students
Assignment 2
Country
Religion State
Person
Identity
WAP to get the identity of a person ,where all
the classes have suitable datamember & member
function.
Assignment 3
Person
name
code
Account Admin
pay experience
Master
name
code
experience
pay
WAP to make Person as virtual base class and create, update and
display the information about the Master objects.
Note: Define the constructors for each class.