0% found this document useful (0 votes)
3 views

Unit 5 Inheritance

BIT

Uploaded by

tamangdorje69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 5 Inheritance

BIT

Uploaded by

tamangdorje69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit 5

Inheritance

5.1 Introduction to Inheritance


The capability of a class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
The main advantage of inheritance is code reusability.
Base Class: The class that inherits properties from another class is called Sub class or Derived
Class.
Derived Class: The class whose properties are inherited by sub class is called Base Class or
Super class.
The Syntax of Derived class:
class derived_class_name :: visibility-mode base_class_name
{
// body of the derived class.
}

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.
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 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.
Note: The private members in the base class cannot be directly accessed in the derived class,
while protected members can be directly accessed.

5.2 Types/Forms of inheritance


1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance

1|Page Prepared By: Ramesh Kumar Chaudhary


5. Hybrid inheritance

Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.

Where 'A' is the base class, and 'B' is the derived class.
Example:
#include <iostream>
using namespace std;

class Account {
public:
float salary = 60000;
};

class Programmer: public Account {


public:
float bonus = 5000;
};

int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}

Output:
Salary: 60000
Bonus: 5000

Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.

2|Page Prepared By: Ramesh Kumar Chaudhary


Example:
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};

class Dog: public Animal


{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};

class BabyDog: public Dog


{
public:
void weep() {
cout<<"Weeping...";
}
};

int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}

Output:
Eating...
Barking...
Weeping...

3|Page Prepared By: Ramesh Kumar Chaudhary


Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes from
two or more classes.

Syntax of the Derived class:


class D : visibility B-1, visibility B-2, ?
{
// Body of the class;
}

Example:
#include <iostream>
using namespace std;

class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};

class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};

class C : public A, public B


{
public:
void display()

4|Page Prepared By: Ramesh Kumar Chaudhary


{
cout << "The value of a is : " <<a<<endl;
cout << "The value of b is : " <<b<<endl;
cout<<"Addition of a and b is : "<<a+b;
}
};

int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}

Output:
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.

Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a base
class.

Syntax:
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}

5|Page Prepared By: Ramesh Kumar Chaudhary


class D : public A
{
// body of class D.
}

Example:
#include <iostream>
using namespace std;

class Shape // Declaration of base class.


{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};

class Rectangle : public Shape // inheriting Shape class


{
public:
int rect_area()
{
int result = a*b;
return result;
}
};

class Triangle : public Shape // inheriting Shape class


{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};

int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
cout<<"Enter the length and breadth of rectangle:"<<endl;
cin>>length>>breadth;
r.get_data(length,breadth);
cout<< "Area of the rectangle is :"<<r.rect_area()<<endl;

6|Page Prepared By: Ramesh Kumar Chaudhary


cout<<"Enter the base and height of the triangle:" <<endl;
cin>>base>>height;
t.get_data(base,height);
cout<<"Area of the triangle is:"<<t.triangle_area()<<endl;
return 0;
}

Output:
Enter the length and breadth of a rectangle:
8
6
Area of the rectangle is : 48
Enter the base and height of the triangle:
6
4
Area of the triangle is : 12

Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.

Example:
#include <iostream>
using namespace std;

class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : " << endl;
cin>>a;
}
};

class B : public A
{

7|Page Prepared By: Ramesh Kumar Chaudhary


protected:
int b;
public:
void get_b()
{
cout << "Enter the value of 'b' : " << endl;
cin>>b;
}
};

class C
{
protected:
int c;
public:
void get_c()
{
cout << "Enter the value of c is : " << endl;
cin>>c;
}
};

class D : public B, public C


{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
cout<<"Multiplication of a,b,c is: "<<a*b*c<<endl;
}
};

int main()
{
D d;
d.mul();
return 0;
}

Output:
Enter the value of 'a' :
6
Enter the value of 'b' :
7
Enter the value of c is :
8
Multiplication of a,b,c is: 336

8|Page Prepared By: Ramesh Kumar Chaudhary


Ambiquity Resolution in Inheritance
Ambiguity can be occurred in using the multiple inheritance when a function with the same
name occurs in more than one base class.
Example:
#include <iostream>
using namespace std;

class A
{
public:
void display()
{
cout << "Class A" <<endl;
}
};

class B
{
public:
void display()
{
cout << "Class B" <<endl;
}
};

class C : public A, public B


{
void view()
{
A::display();
B::display();
}
};

int main()
{
C c;
c.A::display();
c.B::display();
return 0;
}

Output:
Class A
Class B

9|Page Prepared By: Ramesh Kumar Chaudhary


5.3 Constructor and Destructor in derived classes
Order of execution of Constructors and Destructors
It is possible for a base class, a derived class, or both, to contain a constructor and/or
destructor.
It is important to understand the order in which these are executed when an object of a
derived class comes into existence and when it goes out of existence.
Example:
#include <iostream>
using namespace std;

class base {
public:
base() { cout << "Constructing base\n"; }
~base() { cout << "Destructing base\n"; }
};

class derived: public base {


public:
derived() { cout << "Constructing derived\n"; }
~derived() { cout << "Destructing derived\n"; }
};

int main()
{
derived ob;
return 0;
}

Output:
Constructing base
Constructing derived
Destructing derived
Destructing base

Constructor and Destructor in Multiple Inheritance


Example:
#include <iostream>
using namespace std;

class base1 {
public:
base1() { cout << "Constructing base1\n"; }
~base1() { cout << "Destructing base1\n"; }
};

class base2 {
public:

10 | P a g e Prepared By: Ramesh Kumar Chaudhary


base2() { cout << "Constructing base2\n"; }
~base2() { cout << "Destructing base2\n"; }
};

class derived: public base1, public base2 {


public:
derived() { cout << "Constructing derived\n"; }
~derived() { cout << "Destructing derived\n"; }
};

int main()
{
derived ob;
return 0;
}

Output:
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base2
Destructing base1

How to call the parameterized constructor of base class in derived class constructor?
// C++ program to show how to call parameterised Constructor
// of base class when derived class's Constructor is called

// C++ program to show how to call parameterised Constructor


// of base class when derived class's Constructor is called

#include <iostream>
using namespace std;

// base class
class Parent {
int x;

public:
// base class's parameterised constructor
Parent(int i)
{
x = i;
cout << "Inside base class's parameterised "
"constructor"
<< endl;
}
};

// sub class
class Child : public Parent {

11 | P a g e Prepared By: Ramesh Kumar Chaudhary


public:
// sub class's parameterised constructor
Child(int x): Parent(x)
{
cout << "Inside sub class's parameterised "
"constructor"
<< endl;
}
};

// main function
int main()
{
// creating object of class Child
Child obj1(10);
return 0;
}

Output:
Inside base class's parameterised constructor
Inside sub class's parameterised constructor

5.4 Aggregation (Classes Within Classes)


Aggregation is called a “has a” relationship. In object-oriented programming, aggregation may
occur when one object is an attribute of another.
Syntax:
class A
{
};
class B
{
A objA; // define objA as an object of class A
};

Virtual base class


Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances”
of a given class appearing in an inheritance hierarchy when using multiple inheritances.
Figure below shows the need of virtual base calss.

12 | P a g e Prepared By: Ramesh Kumar Chaudhary


Fig: Multiple inheritance leads ambiguity

Example:
#include <iostream>
using namespace std;

class A {
public:
void show()
{
cout << "Hello from A \n";
}
};

class B : public virtual A {


};

class C : public virtual A {


};

class D : public B, public C {


};

int main()
{

13 | P a g e Prepared By: Ramesh Kumar Chaudhary


D object;
object.show();
}

Output:
Hello from A

14 | P a g e Prepared By: Ramesh Kumar Chaudhary

You might also like