Inheritance
Inheritance
Inheritance
• Mechanism of deriving new classes from existing
classes.
Class that is inherited,
Existing Class
known as a base class
• access-specifier
– Decides status of base class members in the
derived class.
– Can be public, private, or protected.
– Is private by default.
Thapar University UTA007 - Computer Programming I 3
Base Class – Access Control
• In any case, the private members of the base class
– Remain private to the base class only.
– Are not accessible by the members of the
derived class. Access-specifier
Base class member Accessibility in the derived class
accessibility
private protected public
Public Private Protected Public
Protected Private Protected Protected
If you want to add a new feature - eat, you need to implement the same
code for each character. This can easily become error prone (when
copying) and duplicate codes.
• it'd be a lot easier if we had a Person class with basic features like talk,
walk, eat, sleep, and add special skills to those features as per our
characters. This is done using inheritance.
• This makes your code cleaner, understandable and extendable.
• When working with inheritance, each derived class should satisfy the
condition whether it "is a" base class or not. In the example above, Maths
teacher is a Person, Footballer is a Person
class Person
{ ... .. ...
};
class MathsTeacher : public Person
{ ... .. ...
};
class Footballer : public Person
{ .... .. ...
};
What is inheritance?
• Inheritance allows software developers to derive a new
class from the existing class. The derived class inherits
features of the base class (existing class).
• The language mechanism by which one class acquires
the properties (data and operations) of another class
Geometric Figure
• access-specifier
– Decides status of base class members in the
derived class.
– Can be public, private, or protected.
– Is private by default.
1
Thapar University UTA007 - Computer Programming I
3
Inheritance and accessibility
• A derived class inherits the behavior of
base class and enhances it in some way
• Inheritance does not mean inheriting
access to another class private members
What a derived class inherits
• Every data member defined in the parent class
(although such members may not always be
accessible in the derived class!)
• Every ordinary member function of the parent class
(although such members may not always be
accessible in the derived class!)
Class Derivation
Any class can serve as a base class…
– Thus a derived class can also be a base class
syntax
class DerivedClassName:mode BaseClassName
DerivedClassName - the class being derived
mode - mode of inheritance specifies
access to the base class
members, whether
public,
protected or
private
- private by default
Types of inheritance
Inheritance
(Derived class)
Employee
class Company { …
}
class Employee : public Company {…
}
Multi-level Inheritance
• Derived class inherits from a class that itself inherits
from another class
Student
Test
Result
class Student:{…}
class Test: public Student{…}
class Result: public Test {…}
Multiple Inheritance
• Derived class inherits from more than one
base class
Person Company
Employee
class Person:{…}
class Company:{…}
class Employee: public Person, public Company
{…}
Hierarchical Inheritance
• Base class is inherited by more than one derived
classes
Student
class Student:{…}
class Arts : public Student{…}
class Engineering: public Student{…}
class Medical : public Medical {…}
Hybrid Inheritance
• Combination of above three types inheritance is hybrid
inheritance
Student
Test Sports
Result
class Student:{…}
class Test: public Student{…}
class Sports: {…}
class Result: public Test, public Sports {…}
Hybrid Inheritance
• Hybrid: Applying two or more types of inheritance.
class A class B
class A
Multiple
Single
class C
class B
Hierarchical
class D
class C class D Multi – level
Protected inheritance
Protected inheritance
class derived3
Thapar University UTA007 - Computer Programming I 36
Solution – Virtual Base Class
1. #include<iostream> Output
2. using namespace std; 10 20 30 60
3. class base { public: int i;};
4. class derived1 : virtual public base { public: int j;};
5. class derived2 : public virtual base { public: int k;};
6. class derived3 : public derived1, public derived2
7. { public: int sum; };
8. int main()
9. { derived3 ob;
10. ob.i = 10; ob.j = 20; ob.k = 30;
11. ob.sum = ob.i + ob.j + ob.k;
12. cout << ob.i << " " << ob.j << " ";
13. cout << ob.k << " " << ob.sum;
14.Thaparreturn
University
0; } UTA007 - Computer Programming I 37
Example – 1
****************************************************
Shape Angles Sides Dimensions Perimeter Area
****************************************************
Rectangle: 4 4 10.5, 5.2 31.4 54.6
Square: 4 4 4.5 18 20.25
Ellipse: 0 0 6.5, 3.2 32.1724 65.312
Circle: 0 0 2.5 15.7 19.625
Triangle: 3 3 6.3, 2.8, 3.8 12.9 3.0591
Triangle: 3 3 5.2, 1.9 7.1 4.94
****************************************************
Square Circle
int main()
{ Derived d;
return 0;
}
Constructor in Multi level Inheritance
class B class D2:public D1
{public: {public:
B() D2()
{cout<<"Base Default {cout<<"Derived 2 Default
constructor"<<endl; constructor"<<endl;
} }
}; };
int main()
class D1:public B {
{public: D2 ob2;
D1() return 0;
{cout<<"Derived 1 Default }
constructor"<<endl;
} OUTPUT
}; Base default constructor
Derived 1 default constructor
Derived 2 default constructor
Destructors in inheritance
• Destructors are called in reverse order as that of constructors
class A:public B, public C
{
};
class Base
{
public:
int fun() { cout << "Base::fun() called"; }
int fun(int i) { cout << "Base::fun(int i) called"; }
};
int main()
{
Derived d;
d.fun(5); //Compiler Error
return 0;
}
Ambiguity in Multiple Inheritance
• Suppose, two base classes have a same
function which is not overridden in derived
class.
• If you try to call the function using the object
of the derived class, compiler shows error. It's
because compiler doesn't know which
function to call. For example,
Ambiguity in Multiple Inheritance
class base1
{
public: void someFunction( ) { .... ... .... }
};
class base2
{public: void someFunction( ) { .... ... .... }
};
class derived : public base1, public base2
{
};
int main()
{
derived obj;
obj.someFunction(); // Error!
}
int main()
{
obj.base1::someFunction( );
// Function of base1 class is called
obj.base2::someFunction();
// Function of base2 class is called.
}
#include<iostream>
class Base {
private:
int i, j;
public:
Base(int ii = 0, int jj = 0): i(ii), j(jj) { }
}; Private data members
class Derived: public Base { not accessible
public:
void show(){
cout<<" i = "<<i<<" j = "<<j;
}
};
int main(void) {
Derived d;
d.show();
return 0;
}
#include<iostream>
using namespace std;
class Base1
{
public:
char c;
};
class Base2
{
public:
int c;
}; Compiler Error in "cout << c;“
class Derived: public Base1, public Base2
Use Base2::c; to resolve ambiguity
{
public:
void show() { cout << c; }
};
int main(void)
{
Derived d;
d.show();
return 0;
}
The base class members cannot be directly assigned
using initializer list. We should call the base class constructor in
order to initialize base class members.
#include<iostream>
using namespace std;
class Base
{
public :
int x, y;
public:
Base(int i, int j){ x = i; y = j; }
};
int main(void)
{
Derived q(10, 10);
q.print(); // Compiler Error
return 0;
}
The base class members cannot be directly assigned
using initializer list. We should call the base class constructor in
order to initialize base class members.
• #include<iostream>
• using namespace std;
•
• class Base
• {
• public :
• int x, y;
• public:
• Base(int i, int j){ x = i; y = j; }
• };
•
• class Derived : public Base
• {
• public:
• Derived(int i, int j): Base(i, j) {}
• void print() {cout << x <<" "<< y; }
• };
•
• int main(void)
• {
• Derived q(10, 10);
• q.print();
• return 0;
• }
A Base class pointer/reference can point/refer to
a derived class object, but the other way is not
possible.
• #include<iostream>
• using namespace std;
•
• class Base {};
•
• class Derived: public Base {};
•
• int main()
• {
• Base *bp = new Derived;
• Derived *dp = new Base;
• }
class Base
{
public:
void show()
{
cout<<" In Base ";
};
} Compiler Error in line " cout << bp->x"
class Derived: public Base
{
public:
int x;
void show()
{
cout<<"In Derived ";
}
Derived()
{
x = 10;
}
};
int main(void)
{
Base *bp, b;
Derived d;
bp = &d;
bp->show();
cout << bp->x;
return 0;
}