Inheritance
Inheritance
Inheritance
Introduction
z 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
z Class that
Cl h i h i
inherits d
data members
b andd member
b
functions from the existing class.
z It is also called as Sub class or Child class.
Types of Inheritance
– Multilevel
M ltile el inheritance: The child class is derived
deri ed A
from another derived class.
B
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
member function of the same class
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:
i b;
int b void
id 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: { {
i t m;
int 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=”
cout<< m n <<m *n<<”\n”;
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
cout class X
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;
roll no; float sub1; float sub2; public:
public: public: void display(void){
void get_no(int a) void get_marks(float x,float y) total=sub1+sub2;
{ rool_no=a; } { sub1=x; sub2=y; } put_no();
void put_no(){ put_marks();
k ()
cout<<”Roll void put_marks(void){ cout<<”total=”<<total;}
number:”<<roll_no ;} cout<<”marks in };
}; sub1=”<<sub1<<”\n”;
sub1 sub1 \n ;
cout<<”marks in
sub2=”<<sub2<<”\n”;
int main(){
}
result student1;;
}
};
student1.get_no(111); OUTPUT:
student1.get_marks(75.0, Roll number: 111
59.5); Marks in sub1=75
student1.display();
t d t1 di l () M k iin sub2=59.5
Marks b2 59 5
return 0; Total=134.5
}
class student
{ Hierarchical inhertance
protected:
int roll_no;
public: class test:public student class sports:public student
void get_no(int a) { {
{ roll_no
roll no=a;
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
cout<< marks in cout<<”sports
p
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_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 {
pprotected: protected:
int roll_no; float score;
public: public:
void get_no(int a) void get_score(float s)
{ roll_no=a;
roll no=a; } { score
score=s;
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
p test,public
,p sports{
p {
protected: float total;
float part1,part2; public:
public: void display(void){
void get_marks(float
get marks(float x,float
x float y) t t l
total=part1+part2+score;
t1+ t2+
{ part1=x; part2=y; } put_no();
void put_marks(void) { put_marks();
cout<<”marks obtained:”<<”\n” pput_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);
d 1 (6 0) Part2 33
Part2=33
student1.display(); Sports wt:6
return 0; Total score=66.5
}
Virtual base class
z Two copies
T i off theh traits
i off Stud
S d will
ill appear in
i
Stud
Result class, that will lead to ambiguity
z The solution is to make the base class Stud as a Test Sport
virtual base class.
z 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;
p();
c1.disp();
}
Assignment 1
Student
Roll
Test
getRoll()
Mark1
PrintRoll() R lt
Result
Mark2
Total
o
getMark()
displayMark()
PrintMark()
Output:Detail information about 10 students
Assignment 2
Country
Religion State
Person
Identity
WAP to gget the identityy of a person
p ,,where all
the classes have suitable datamember & member
function.
Assignment 3
Person
name
code
Account Admin
pay experience
Master
name
code
experience
p
pay
WAP to make Person as virtual base class and create, update and
di l the
display h information
i f i about
b the
h Master
M objects.
bj
Note: Define the constructors for each class.