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

Module 3 Inheritance

Module 3 covers inheritance and polymorphism in C++, explaining the concept of inheritance where a derived class acquires properties and behaviors from a base class. It details various types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples for each type. Additionally, it discusses visibility modes (public, private, protected) and their implications on member accessibility in derived classes.
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)
0 views

Module 3 Inheritance

Module 3 covers inheritance and polymorphism in C++, explaining the concept of inheritance where a derived class acquires properties and behaviors from a base class. It details various types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples for each type. Additionally, it discusses visibility modes (public, private, protected) and their implications on member accessibility in derived classes.
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/ 16

Module 3–Inheritance & Polymorphism

DAYANANDA SAGAR COLLEGE OF ENGINEERING


ShavigeMalleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
(An Autonomous Institution affiliated to VTU, Belagavi)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Module 3
Inheritance
Inheritance is a process in which one object acquires all the properties and behaviors of its parent
object automatically. the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.

Advantage of C++ Inheritance


Code reusability: Now you can reuse the members of your parent class. So, there is no need to
define the member again. So less code is required in the class.

Types of Inheritance
C++ supports five types of inheritance:

 Single inheritance
 Multiple inheritance
 Hierarchical inheritance
 Multilevel inheritance
 Hybrid inheritance

Department of CSE, DSCE Page 1


Module 3–Inheritance & Polymorphism

Derived Classes
A Derived class is defined as the class derived from the base class.

The Syntax of Derived class:

class derived_class_name : visibility-mode base_class_name


{
// body of the derived class.
}

Where,

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly
inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

 When the base class is privately inherited by the derived class, public members of the base
class becomes the private members of the derived class. Therefore, the public members of the
base class are not accessible by the objects of the derived class only by the member functions
of the derived class.
 When the base class is publicly inherited by the derived class, public members of the base
class also become the public members of the derived class. Therefore, the public members of
the base class are accessible by the objects of the derived class as well as by the member
functions of the base class.

Note:
 In C++, the default mode of visibility is private.
 The private members of the base class are never inherited.

Department of CSE, DSCE Page 2


Module 3–Inheritance & Polymorphism

C++ 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.

C++ Single Level Inheritance Example: Inheriting Fields


When one class inherits another class, it is known as single level inheritance. Let's see the
example of single level inheritance which inherits the fields only.

#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;
}

Department of CSE, DSCE Page 3


Module 3–Inheritance & Polymorphism

Output:

Salary: 60000
Bonus: 5000

In the above example, Employee is the base class and Programmer is the derived class.

C++ Single Level Inheritance Example: Inheriting Methods


Let's see another example of inheritance in C++ which inherits methods only.

#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}

Output:

Eating...

Department of CSE, DSCE Page 4


Module 3–Inheritance & Polymorphism

Barking...

Let's see a simple example.

#include <iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};

class B : private A
{
public:
void display()
{
int result = mul();
cout <<"Multiplication of a and b is : "<<result<< endl;
}
};
int main()
{
B b;
b.display();

return 0;
}

Department of CSE, DSCE Page 5


Module 3–Inheritance & Polymorphism

Output:

Multiplication of a and b is : 20

In the above example, class A is privately inherited. Therefore, the mul() function of class 'A'
cannot be accessed by the object of class B. It can only be accessed by the member function of
class B.

How to make a Private Member Inheritable


The private member is not inheritable. If we modify the visibility mode by making it public, but
this takes away the advantage of data hiding.

C++ introduces a third visibility modifier, i.e., protected. The member which is declared as
protected will be accessible to all the member functions within the class as well as the class
immediately derived from it.

Visibility modes can be classified into three categories:

 Public: When the member is declared as public, it is accessible to all the functions of the
program.
 Private: When the member is declared as private, it is accessible within the class only.
 Protected: When the member is declared as protected, it is accessible within its own class as well
as the class immediately derived from it.

Department of CSE, DSCE Page 6


Module 3–Inheritance & Polymorphism

Visibility of Inherited Members

Base class visibility Derived class visibility

Public Private Protected

Private Not Inherited Not Inherited Not Inherited

Protected Protected Private Protected

Public Public Private Protected

C++ Multilevel Inheritance


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

C++ Multi Level Inheritance Example


When one class inherits another class which is further inherited by another class, it is known as
multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the
members of all its base classes.

Department of CSE, DSCE Page 7


Module 3–Inheritance & Polymorphism

Let's see the example of multi level inheritance in C++.

#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...

Department of CSE, DSCE Page 8


Module 3–Inheritance & Polymorphism

Barking...
Weeping...

C++ 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;
}

Let's see a simple example of multiple inheritance.

#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)

Department of CSE, DSCE Page 9


Module 3–Inheritance & Polymorphism

{
a = n;
}
};

class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A, public B
{
public:
void display()
{
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;
}

Department of CSE, DSCE Page 10


Module 3–Inheritance & Polymorphism

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.

C++ Hybrid Inheritance


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

Let's see a simple example:

#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()

Department of CSE, DSCE Page 11


Module 3–Inheritance & Polymorphism

{
cout << "Enter the value of 'a' : " << endl;
cin>>a;
}
};

class B : public A
{
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;

Department of CSE, DSCE Page 12


Module 3–Inheritance & Polymorphism

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' :


10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000

C++ Hierarchical Inheritance


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

Department of CSE, DSCE Page 13


Module 3–Inheritance & Polymorphism

Syntax of Hierarchical inheritance:

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

Let's see a simple example:

#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;

Department of CSE, DSCE Page 14


Module 3–Inheritance & Polymorphism

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 a rectangle: " << endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();

Department of CSE, DSCE Page 15


Module 3–Inheritance & Polymorphism

cout << "Area of the rectangle is : " <<m<<endl;


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

Output:

Enter the length and breadth of a rectangle:


23
20
Area of the rectangle is : 460
Enter the base and height of the triangle:
2
5
Area of the triangle is : 5

Department of CSE, DSCE Page 16

You might also like