Академический Документы
Профессиональный Документы
Культура Документы
PDPU
Inheritance…
Inheritance is the process by which objects of one
class acquire the properties of objects of another
class.
It supports the concept of hierarchical
classification.
e.g. The bird “sparrow” is a part of the class “flying
bird” which is again a part of the class “bird”.
2
Inheritance…
Each derived class shares common characteristics
with the class from which it is derived.
It provides reusability.
We can add additional features to an existing class
without modifying it when we derive a new class
from existing one.
3
Inheritance…
The mechanism of deriving a new class from an
old one is called inheritance (or derivation).
The old class is referred to as the base class and
the new one is called the derived class or subclass.
4
Inheritance…
The new class will have the combined features of
both the classes.
Allows the programmer
To reuse a class
To tailor the class in such a way that it does
not introduce any undesirable side-effects
into the rest of the classes.
5
Different Types of Inheritance…
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
6
Single Inheritance…
A derived class with only one base class is called
Single Inheritance.
7
Multilevel Inheritance…
A class is derived from another derived class is
known as multilevel inheritance.
C 8
Multiple Inheritance…
A derived class from several base classes is called
multiple inheritance.
A B
9
Hierarchical Inheritance…
Features of one class can be inherited by more
than one class is known as hierarchical
inheritance.
B C
10
Hybrid Inheritance…
Combination of different types of inheritance is
known as hybrid inheritance.
B C
D 11
Defining Derived Classes…
General Form…
13
Defining Derived Classes …
Features Of Base Class …
14
Defining Derived Classes …
Features Of Base Class …
15
Example…
Function Main ( )…
index1 i;
index j;
j++;
cout << "j."; j.display();
i++; i++;
cout << "i."; i.display();
i--;
cout << "i."; i.display();
Please go through
the program
“inherit1.cpp”
16
Example…
Defining Class Index…
17
Example…
Defining Class Index…
index (int c)
{
count = c;
}
void display()
{
cout << "count = " << count << endl;
}
18
Example…
Defining Class Index…
19
Example…
Deriving Class Index1…
GENERAL FORM
20
Example…
Function Main ( ) & Output …
index1 i; OUTPUT
index j;
j++;
cout << "j."; j.display(); j.count = 1
i++; i++;
cout << "i."; i.display();
i.count = 2
i--;
cout << "i."; i.display();
i.count = 1
Please go through
the program
“inherit1.cpp”
21
protected: instead of private:
why? …
22
protected: instead of private:
Answer …
23
protected: instead of private:
Answer …
24
FEW POINTS…
We can increase the functionality of the base class
(index) without modifying it through the derived
class (index1).
Inheritance does not work in reverse as the base
class & its objects have no knowledge about any
classes derived from the base class.
25
Try the following…
In class index, declare variable count as private
instead of protected.
Change visibility mode specifier to private instead
of public
(write class index1: private index
instead of class index1: public index)
Call j--; from main.
26
EXAMPLE 2 …Function main ( ) …
int n;
stack1 stk;
stk.push(10); stk.push(20); stk.push(30);
stk.push(40); stk.push(50);
n = stk.pop();
cout << "Value : " << n << " popped.“;
n = stk.pop();
cout << "Value : " << n << " popped.“;
getch();
27
EXAMPLE 2 … Defining class stack…
int pop()
{
int n;
if (top == -1)
{
cout << "Stack is empty." ;
return NULL;
}
31
EXAMPLE 2 …
Inheriting class stack1 from stack …
else
{
n = stack::pop();
return n;
}
}
};
32
EXAMPLE 2 …
What is happening? …
33
Few points…
Here, both classes stack1 and stack have common
functions.
Which one would be executed?
The function in the derived class gets a priority
when the function is called as a member of the
derived class object.
A program can declare objects of both the base
and derived classes. The two objects are
independent of one another.
34
Few Points…
Friend function can access private as well as
protected data directly,
The member functions of a derived class can
access only the protected data.
35
Visibility Of Inherited Members…
BASE DERIVED CLASS VISIBILITY
CLASS
VISIBILITY PUBLIC PRIVATE PROTECTED
DERIVATION DERIVATION DERIVATION
INTERMEDIATE
BASE CLASS TEST FATHER
DERIVED CHILD
CLASS RESULT
Please go through
the program
“inherit4.cpp”
37
Multilevel Inheritance…
Class Declaration …
class student { … };
class test : public student { … };
class result : public test { … };
38
Multilevel Inheritance…
Student Class Declaration …
class student
{
protected:
int rn;
public:
void get_rn(int);
void put_rn(void);
};
39
Multilevel Inheritance…
student class declaration …
void student::put_rn(void)
{
cout << "Roll No. = " << rn << endl;
}
40
Multilevel Inheritance…
test class declaration …
41
Multilevel Inheritance…
test class declaration …
43
Multilevel Inheritance…
result class declaration …
44
Multilevel Inheritance…
What result class will have …
private:
int total;
protected:
int rn, english, cp;
public:
void get_rn (int);
void put_rn (void);
void get_marks( int , int);
void put_marks (void);
void display (void); 45
Multilevel Inheritance…
main() function …
void main()
{
clrscr();
RESULT s10;
s10.get_rn(10);
s10.get_marks(80,85);
s10.display();
getch();
};
46
Multiple Inheritance…
A derived class from several base classes is called
multiple inheritance.
A B
47
Syntax Of Derived Class With Multiple Base
Classes…
48
Multiple Inheritance…
It allows us to combine the features of several
existing classes as a starting point for defining
new classes.
It is like a child inheriting the physical features of
one parent and the intelligence of another.
The derived class will contain all the members of
all base-classes in addition to its own members.
49
Multiple Inheritance…
Example…
class father
{
private:
char f_name[50];
char f_blood_grp[5];
int f_height_in_feet;
int f_height_in_inch;
50
Multiple Inheritance…
Example…
public:
void f_init( char*nm, char*bg, int hf, int hi)
{
strcpy(f_name,nm);
strcpy(f_blood_grp,bg);
f_height_in_feet = hf;
f_height_in_inch = hi;
};
51
Multiple Inheritance…
Example…
void f_display(void)
{
cout << "Name of father = " << f_name;
cout << "Blood Group = " << f_blood_grp;
cout << "Height = "
<< f_height_in_feet << " Feet & "
<< f_height_in_inch << " Inches ";
}
};
52
Multiple Inheritance…
Example…
class mother
{
private:
char m_name[50];
char m_blood_grp[5];
int m_height_in_feet;
int m_height_in_inch;
53
Multiple Inheritance…
Example…
public:
void m_init( char*nm, char*bg, int hf, int hi)
{
strcpy(m_name,nm);
strcpy(m_blood_grp,bg);
m_height_in_feet = hf;
m_height_in_inch = hi;
};
54
Multiple Inheritance…
Example…
void m_display(void)
{
cout << "Name of Mother = " << m_name;
cout << "Blood Group = " << m_blood_grp;
cout << "Height = "
<< m_height_in_feet << " Feet & "
<< m_height_in_inch << " Inches ";
}
};
55
Multiple Inheritance…
Example…
class child : public father, public mother
{
private:
char c_name[50];
char c_blood_grp[5];
int c_height_in_feet;
int c_height_in_inch;
56
Multiple Inheritance…
Example…
public:
void c_init( char*nm, char*bg, int hf, int hi)
{
strcpy(c_name,nm);
strcpy(c_blood_grp,bg);
c_height_in_feet = hf;
c_height_in_inch = hi;
};
57
Multiple Inheritance…
Example…
void c_display(void)
{
f_display(); m_display();
cout << "Name of child = " << c_name;
cout << "Blood Group = " << c_blood_grp;
cout << "Height = "
<< c_height_in_feet << " Feet & "
<< c_height_in_inch << " Inches ";
}
}; 58
Multiple Inheritance…
Example…
void main()
{
child c1;
c1.f_init("Darshit","O+",5,11);
c1.m_init("Ragi","A+",5,7);
c1.c_init("Aashna",“O+",5,7);
c1.c_display();
}; Please go through
the program
“inherit5.cpp”
59
Ambiguity Resolution In Inheritance …
Please go through
the program
“inherit6.cpp”
60
Virtual Base Classes…
Suppose, we have multilevel, multiple and
hierarchical inheritance , all together in one
derived class, there will be duplication of
members in derived class.
See the example.
61
EXAMPLE…
GRAND PARENT
PARENT 1 PARENT 2
CHILD
62
EXAMPLE…
The child class is derived from two base classes
called parent 1 and parent 2.
Parent 1 and parent 2 are derived from common
base class grand parent.
Thus child inherits the traits of grand parent via
two separate path. i.e. child will have duplicate set
of members inherited from grand parent.
63
Virtual Base Class …
This duplication can be avoided by making the
common base class as virtual base class while
declaring.
64
Declaration of Virtual Base Class…
67
Constructors… Mandatory Or Not…
Base No. of Derived Class
Class Argument
Absent 0 Or >0 May Or May Not
Have Constructors
Present 0 May Or May Not
Have Constructors
Present >0 Must Have Please go
through the
Constructors program “
inherit7.cpp” &
“inherit8.cpp”
68
Order of Execution of Constructor Functions…
69
Order Of Execution Of
Constructor Functions…
Virtual base class constructors are always invoked
first.
class A : public B,
virtual public C { }; C( ) , B( ), A( )
70
How To Pass Arguments To Constructors? …
72