sdf-inheritance
sdf-inheritance
Class B
Data
Functions
Inheritance
▪ Polymorphism enables to define many forms of a function and it is
invoked based on the object calling it.
▪ The functionality of a function differs according to the object that
calls it.
▪ Inheritance helps to implement polymorphism. Class A
Data
Functions
Class B
Data
Functions
Advantages of inheritance
• When a class inherits from another class, there are three benefits:
• (1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new
methods
(3) You can modify the existing class by overloading its methods
with your own implementations
Inheritance
Without Inheritance
Prepared by Ashish Mishra 8
Why and when to use inheritance?
Code Reusability
With Inheritance
Prepard by Ashish Mishra 9
Modes of Inheritance
• Public mode: If we derive a sub class from a public base class. Then the
public member of the base class will become public in the derived class and
protected members of the base class will become protected in derived class.
Private members of the base class will never get inherited in sub class.
• Protected mode: If we derive a sub class from a Protected base class. Then
both public member and protected members of the base class will become
protected in derived class. Private members of the base class will never get
inherited in sub class.
• Private mode: If we derive a sub class from a Private base class. Then both
public member and protected members of the base class will become Private
in derived class. Private members of the base class will never get inherited
in sub class.
class Mammal { }
};
public:
class Bat: public Mammal, public WingedAnimal { };
Mammal()
int main()
{ {
cout << "Mammals can give Bat b1;
direct birth." << endl; return 0;
} }
}; Output
class WingedAnimal { Mammals can give direct birth.
Public
} Private
Protected
Prepard by Ashish Mishra 25
INHERITANCE
FOLLOWING ARE THE THREE POSSILBE STYLE OF DERIVATION
Public Derivative
class Derived_Class_name: public Base_class_name
{
Private Derivative
class Derived_Class_name: private Base_class_name
{
Protected Derivative
class Derived_Class_name: protected Base_class_name
{
Private Private
Protected Protected
Public Public
Private
Protected
Public
Private Private
Protected Protected
Public Public
Private
Protected
Public
output: A:default
B test(1); B 33
Constructor Rules for Derived Classes
You can also specify an constructor of the base
class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass
args )
{ DerivedClass constructor body }
class B
{
public:
B() { cout << "B's constructor called" << endl; }
OUTPUT:-
}; B's constructor called
A's constructor called
C's constructor called
35
#include<iostream>
using namespace std;
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
}; OUTPUT:-
A's constructor called
B's constructor called
C's constructor called
36
class C: public A, public B
Inheritance Types : Review
Class D inherits the members of class A via two paths i.e. class B and class C.
In the above example, both Class B & Class C inherit Class A, they both have single copy of Class A members.
However, Class D inherits both Class B & Class C, therefore Class D has two copies of Class A members, one from
Class B and another from Class C.
Example PERSON 39
Name
Phone number
address
STUDENT FACULTY
Name Name
Phone number Phone number
Address Address
STUDENT FACULTY
Enrollment No Salary
Enrollment No Subject taught
CGPA Subject taught
CGPA Salary
TA
The duplication of inherited members due to these multiple paths can be avoided by making a common
base class as virtual base class while declaring the direct or intermediate base classes as below:
class person
{ };
class student: virtual public person
{ };
class faculty: public virtual person
{ };
class TA: public student, public faculty
{ };
• When a class is made a virtual base class, C++ takes necessary care to
see that only one copy of that class members is inherited, regardless of
how many paths exist between the virtual base class and a derived class.
• You can visualize it as, A may not be direct base class of D, but
virtually it is a base class of D (shown in dotted line). So any
communication with A from D will be done as if A is its own base class
(and not via B or C).
More examples on virtual inheritance and
diamond/pyramid problems
Example 1
OUTPUT:
int main()
a()
{ b()
d obj; a()
return 0; c()
} d()
Example 2
OUTPUT:
int main()
a()
{ b()
d obj; a()
return 0; c()
} d()
Example 3
OUTPUT:
int main()
a()
{ a()
d obj; b()
return 0; c()
} d()
Example 4
class a class b : public virtual a class c: virtual public a class d : public b, public c
{ { { {
int x; int y; int z; int u;
public: public: public: public:
a() b() c() d()
{ { { {
cout<<"\n a()"; cout<<"\n b()"; cout<<"\n c()"; cout<<"\n d()";
} } } }
a(int t) b(int t) c(int t) d(int t)
{ { { {
cout<<"\n a(int)"; cout<<"\n b(int)"; cout<<"\n c(int)"; cout<<"\n d(int)";
} } } }
};
}; }; };
OUTPUT:
int main()
a()
{ b()
d obj; c()
return 0; d()
}
Example 5
OUTPUT:
int main()
a(int) a()
{ a() a(int)
d obj; b(int) b()
d obj2(10); c() c(int)
return 0; d() d(int)
}
Example 6
class a class e class c: public b, public d
{ int x; { int x1; { int z;
public: public: public:
a() e() c(): e(2), d(7)
{ cout<<"\n a()"; {cout<<"\n e()"; { cout<<"\n c()";
} } }
a(int t) e(int t) c(int t) : a(1)
{ cout<<"\n a(int)"; {cout<<"\n e(int)"; { cout<<"\n c(int)";
} } } a()
}; }; }; e(int)
b()
class b : virtual public a class d : virtual public e
int main() d(int)
{ int y; { int y;
{ c()
public: public:
c obj;
b():a(3) d()
c obj2(10);
{ {
return 0; a(int)
cout<<"\n b()"; cout<<"\n d()";
} e()
} }
b(int t) d(int t) : e(2) b()
{ { d()
cout<<"\n b(int)"; cout<<"\n d(int)"; c(int)
} }
}; };
Example 7
Just changed the order
class a class e class c: public d, public b
here
{ int x; { int x1; { int z;
public: public: public:
a() e() c(): e(2), d(7)
{ cout<<"\n a()"; {cout<<"\n e()"; { cout<<"\n c()";
} } }
a(int t) e(int t) c(int t) : a(1)
{ cout<<"\n a(int)"; {cout<<"\n e(int)"; { cout<<"\n c(int)";
} } } e(int)
}; }; }; a()
d(int)
class b : virtual public a class d : virtual public e
int main() b()
{ int y; { int y;
{ c()
public: public:
c obj;
b():a(3) d()
c obj2(10);
{ {
return 0; e()
cout<<"\n b()"; cout<<"\n d()";
} a(int)
} }
b(int t) d(int t) : e(2) d()
{ { b()
cout<<"\n b(int)"; cout<<"\n d(int)"; c(int)
} }
}; };
Method Overriding
•As we know, inheritance is a feature of OOP that allows
us to create derived classes from a base class. The
derived classes inherit features of the base class.
•Suppose, the same function is defined in both the
derived class and the based class. Now if we call this
function using the object of the derived class, the
function of the derived class is executed.
•This is known as function overriding in C++. The
function in derived class overrides the function in base
class. 52
Method Overriding
• To override a function you must have the same signature in child
class. By signature I mean the data type and sequence of
parameters. Here we don’t have any parameter in the parent
function so we didn’t use any parameter in the child function.
53
Requirements for Overriding a Function
1.Inheritance should be there. Function
overriding cannot be done within a class. For
this we require a derived class and a base
class.
2.Function that is redefined must have exactly
the same declaration in both base and derived
class, that means same name, same return
type and same parameter list.
54
Derived Function
Method Overriding
// C++ program to demonstrate function overriding class Derived : public Base {
public:
#include <iostream> void print() {
using namespace std; cout << "Derived Function" << endl;
}
class Base { };
public:
void print() { int main() {
cout << "Base Function" << endl; Derived derived1;
} derived1.print();
}; return 0;
}
Outpu: Derived Function
55