0% found this document useful (0 votes)
41 views25 pages

Inheritance

Inheritance allows the creation of new classes from existing classes, extending the reusability of attributes and behaviors. There are different types of inheritance including simple, multiple, multilevel, hierarchical, and hybrid. Visibility modes for inheritance include public, protected, and private which determine how members of the base class are accessible in the derived class.

Uploaded by

VINEETH Vinnu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views25 pages

Inheritance

Inheritance allows the creation of new classes from existing classes, extending the reusability of attributes and behaviors. There are different types of inheritance including simple, multiple, multilevel, hierarchical, and hybrid. Visibility modes for inheritance include public, protected, and private which determine how members of the base class are accessible in the derived class.

Uploaded by

VINEETH Vinnu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

I h it

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

z Inheritances are of 5 types


yp 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
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

– 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 : p
public X
{
// Class Y now inherits the members of Class X publicly
}

base class (X) derived class (Y)


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

base class (X) derived class (Y)


public members protected
protectedd members
b 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
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” ;
} }
}; };

class Z:public X,public Y{


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

• 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.
Constructor in derived class
z As long as no base class contructor takes any arguments, the derived class
need not have a constructor function.
z 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.
z Always the base constructor is executed first and then the constructor in
the derived class is executed.
z 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 { };
() derived constructor
B();
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
id show_x(void){
h ( id){ cout<<”y=”<<y<<”\n”; cout<<”gamma
t ” initialized”;
i iti li d”
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();
g.show_mn(); y=10 75
y=10.75
return 0; m=20
} n=30
Nesting(containership) of classes

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


it is referred as member object.
j
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
id get()
() void
id show()
h () B obj2;
bj2
{ { public:
cout<<“fun1”; cout<<“fun2”; void disp()
} } {
}; }; obj1.get();
obj2.show();
}
};

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy