Unit 3 Inheritance
Unit 3 Inheritance
PROGRAMMING
(with C++)
Unit-3
Inheritance
Multiple Inheritance
Class Z:access_specifier X, access_specifier Y...
X Y Z
Hierarchical Inheritance
Intermediate
base class Y test
Multilevel Inheritance
Hybrid Inheritance
W
Int I
Int I (of X)
Int I (of Y)
Int J (of X)
Int K (of Y)
Z
OOPS (Unit - 3) Dr. Gaurav Gupta 39
Associate Professor (IT Deptt.)
#include"iostream.h" class Y: public W
#include"conio.h" {
class W public:
{ int k;
public: };
int i; class Z: public X, public Y
}; {
public:
class X : public W
int sum;
{ public:
};
int j;
};
OOPS (Unit - 3) Dr. Gaurav Gupta ( Department of IT ) 40
int main (void)
{
clrscr(); OUTPUT
Z obj;
obj.X::i=1; The sum is 6
obj.j=2;obj.k=3;
obj.sum=obj. X::i+ obj.j+obj.k;
// obj.i will be ambiguous
cout<<"the sum is "<<obj.sum;
return 0;
}
If X and Y derives class W as a virtual class then only one copy of W-members will
be inherited by X and Y.
Teacher Officer
Subject Grade
Class
Typist
speed
Casual
Regular Daily
Wages