Sunteți pe pagina 1din 26

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

 Inheritances are of 5 types A


– Simple inheritance: The child class derives
from only one parent class. B

– Multiple inheritance: The child class derives A B


from multiple parent classes.
C

– Multilevel inheritance: The child class is derived 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

– Hybrid inheritance: Combination of two or A

more inheritances.
B C

D
class X{
X
};

class Y : visibility-mode X{ Y

};

 Visibility modes are of 3 types.


• public
• protected
• private
Public Visibility Mode
class Y : public X
{
// Class Y now inherits the members of Class X publicly
}

base class (X) derived class (Y)


public members public
protected members protected
private members Can’t be inherited
Protected Visibility Mode
class Y : protected X
{
// Class Y now inherits the members of Class X protectedly
}

base class (X) derived class (Y)


public members protected
protected members protected
private members Can’t be inherited
Private Visibility Mode
Not Compulsory

class Y : private X
{
// Class Y now inherits the members of Class X privately
}

base class (X) derived class (Y)


public members private
protected members private
private members Can’t be inherited
Difference Between Private and
Protected

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” ;
} }
}; };

class Z:public X,public Y{


public: main(){
void disp() Z z1;
{ cout<<“class Z” ; z1.disp(); //class Z
} }
};
Ambiguity resolution in Inheritance
(Member Function Overriding)
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
{ {
}; };

• virtual and public keywords my exchange their position.


• The Stud class has become virtual base class and only one copy
of the traits from Stud will appear in Result.
class A
{
public:
void disp()
{
cout<<“Base Class”
}
}
Constructor in derived class
 As long as no base class contructor takes any arguments, the derived class
need not have a constructor function.
 However, if any base class contains a constructor with one or more
arguments, then it is mandatory for the class to have a constructor and pass
the arguments to the base class contructors.
 Always the base constructor is executed first and then the constructor in
the derived class is executed.
 Syntax for defining Derived Class Constructor:
<derived constructor>(<argument list>):base class constructor<argument
list>
Methods of inheritance Order of execution
A(); base constructor
Class B:public A { };
B(); derived constructor
B(); base (first)
Class A:public B,public C { }; C(); base (second)
A(); derived
C(); virtual base constructor
Class A:public B, virtual public C { }; B(); ordinary base constructor
A(); derived
Constructor in derived class(example…)
class alpha{ class beta{ class gamma:public beta,public alpha
int x; float y; {
public: public: int m,n;
alpha(int i) beta(float j){ public:
{ y=j; gamma(int a,float b,int c,int
x=i; cout<<”beta initialized”; d):alpha(a),beta(b){
cout<<”alpha initialized”; } m=c;
} void show_y(void){ n=d;
void show_x(void){ cout<<”y=”<<y<<”\n”; cout<<”gamma initialized”;
cout<<”x=”<<x<<”\n”; } }
} }; void show_mn(void){
}; cout<<”m=”<<m<<”\n”
<<”n=”<<n<<”\n”;
}
int main() Output:
};
{ beta intialized
gamma g(5,10.75,20,30); alpha intialized
g.show_x(); gamma intialized
g.show_y(); x=5
g.show_mn(); y=10.75
return 0; m=20
} n=30
Nesting(containership) of classes

 If an object of a class is becoming a member of another class,


it is referred as member object.
class X { ...........}; class Y { ...........}; class Z {
X x1;
//x1 is an object of X class
Y y1;
//y1 is an object of Y class
............. };
In the above example, object of class Z contains the objects
of class A & class B. This kind of relationship is called
containership or nesting.
class A class B class C
{ { {
public: public: A obj1;
void get() void show() B obj2;
{ { public:
cout<<“fun1”; cout<<“fun2”; void disp()
} } {
}; }; obj1.get();
obj2.show();
}
};

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.

S-ar putea să vă placă și