Inheritance
Inheritance
1
Introduction
◼ Why Inheritance ?
❑ Because Reusability is achieved by Inheritance.
❑ How ?
Create a new class and reuse/inherit the properties
of an existing one – Objects are reused when Inherited.
3
The Syntax (Cont..)
class father
{
private: // Can’t be Inherited
public:
protected:
};
public:
protected:
};
4
The Syntax (Cont..)
class father
{
private: // Can’t be Inherited
public:
protected:
};
public:
protected:
};
5
The Syntax (Cont..)
class father
{
private: // Can’t be Inherited
public:
protected:
};
public:
protected:
};
6
A program
// Program: ex1.cpp void Derived :: mul(void)
# include<iostream.h> {
using namespace std; //c = a * b; will be an error
class Base c = get_a() * b;
{ }
int a; void Derived :: show_abc(void)
public: {
int b; show_a();
void input_ab(){a=5; b=10;} cout << “b=“ << b << “\n”;
int get_a() {return a;} cout << “c=“ << c << “\n”;
void show_a() }
{ //---------------------------------//
cout << “a=“ << a << “\n”; int main() OUTPUT
} {
}; Derived d; a=5
d.input_ab();
class Derived : public Base d.show_a(); a=5
{ d.mul();
int c; b=10
d.show_abc();
public: c=50
void mul(void); d.b=20;
void show_abc(void); d.mul(); a=5
}; d.show_abc();
b=20
return (0);
} c=100 7
Types of Inheritance
◼ Single
◼ Multiple
◼ Multilevel
◼ Hierarchical
◼ Hybrid
8
Single Inheritance
9
Multilevel Inheritance
10
A program (Multi level Inheritance)
// program ex2.cpp
class test : public student class result : public test int main()
# include<iostream> //first level derivation //second level derivation {
using namespace std; { { result student1;
protected: float total; //private by //student1 object
class student float sub1, sub2; default created
{ public: public:
protected: void get_marks(float,float); void display(void); student1.get_no(111);
int roll_no; void put_marks(void); }; student1.get_marks(75.
public: }; 0,59.5);
void get_no(int); void result :: display (void) student1.display();
void put_no(void); void test :: get_marks(float x, {
}; float y) total=sub1+sub2; return 0;
{ put_no(); }
void student :: get_no(int a) sub1=x; sub2=y; put_marks();
{ } cout<<“total=“<<total;
roll_no=a; }
} void test :: put_marks()
{
void student :: put_no() cout<<”marks in OUTPUT
{ sub1=”<<sub1<<”\n”; Roll number: 111
cout<<”Roll cout<<”marks in Marks in sub1=75
number:”<<roll_number ; sub2=”<<sub2<<”\n”; Marks in
} } sub2=59.5
Total=134.5
11
Multiple Inheritance
12
A program (Multiple Inheritance)
// program ex3.cpp
class P : public M, public N int main()
#include<iostream> { {
using namespace std; public: P p;
void display(void); p.get_m(10);
class M }; p.get_n(20);
{ //----------------------------------------// p.display();
protected: void M :: get_m(int x)
int m; { return 0;
public: m=x; }
void get_m(int); }
}; void N :: get_n(int y)
{
class N n=y;
{ }
protected: void P :: display(void) OUTPUT
int n; { M=10
public: cout<<”m=” <<m <<”\n”; N=20
void get_n(int); cout<<”n=” <<n <<”\n”; M*n=200
}; cout<<”m*n=” <<m *n<<”\n”;
}
13
Hierarchical Inheritance
The traits of one class may be inherited by more than one class.
Student
14
Hybrid Inheritance
Grand Father
Father Mother
Son
15
Virtual Base Class
Grand Parent
Parent 1 Parent 2
Child
When a class is made virtual base class, C++ takes necessary care to see that only one copy
of that class in inherited, regardless of how many inheritance paths exist between the virtual
17
base class and a derived class.