0% found this document useful (0 votes)
31 views17 pages

Inheritance

Inheritance allows classes to inherit properties from other classes. There are different types of inheritance including single, multilevel, multiple, hierarchical, and hybrid inheritance. Virtual base classes are used to avoid duplicating inherited members when a class inherits from multiple parent classes.

Uploaded by

Niroj Pani
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)
31 views17 pages

Inheritance

Inheritance allows classes to inherit properties from other classes. There are different types of inheritance including single, multilevel, multiple, hierarchical, and hybrid inheritance. Virtual base classes are used to avoid duplicating inherited members when a class inherits from multiple parent classes.

Uploaded by

Niroj Pani
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/ 17

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.

◼ What is Inheritance (Def) ?


❑ The mechanism of deriving a new class from an old one is
called Inheritance (Derivation).
– New class => Derived Class
– Old class => Base class
2
How to derive a Base Class (The Syntax)

class derived-class-name : visibility mode base-class-name


{
……
…… // members of derived class
};

class son : private/public/protected father


{
…………
………… // members of son
};

3
The Syntax (Cont..)
class father
{
private: // Can’t be Inherited

public:

protected:
};

class son : private father


{
private:

public:

protected:

};
4
The Syntax (Cont..)
class father
{
private: // Can’t be Inherited

public:

protected:
};

class son : public father


{
private:

public:

protected:

};
5
The Syntax (Cont..)
class father
{
private: // Can’t be Inherited

public:

protected:
};

class son : protected father


{
private:

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

Father Base Class

Son Derived Class

A derived class with one base class


Ex - program ex1.cpp (Already discussed)

9
Multilevel Inheritance

Grand Father Base Class

Father Intermediate Base Class

Son Derived Class

Deriving a class from another derived class

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

Base Class 1 Father Mother Base Class 2

Son Derived Class

One class inherits from several base classes

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

Science Engineering Medical

Mech. Comp. Sc. Electrical

14
Hybrid Inheritance

Grand Father

Father Mother

Son

15
Virtual Base Class

Grand Parent

Parent 1 Parent 2

Child

The child will inherit the properties / members of the Grand


parent two times, one through parent 1 and other trough parent 2.
To avoid this duplication of inherited members the concept of
Virtual base class is used.
16
Virtual Base Class (Syntax)
class Grand_parent
{
………………………
};

class Parent_1 : virtual public Grand_parent


{
……………………
};

class Parent_2 : public virtual Grand_parent


{
……………………
};

class Child : public Parent_1, public Parent_2


{
……………………
};

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.

You might also like