Inheritance is a mechanism where a derived class extends a base class without modifying it, creating hierarchical relationships. There are different types of inheritance including single, multi-level, multiple, hierarchical, and hybrid. Visibility modes like private, protected, and public determine which members are inherited from the base class. Multilevel inheritance involves deriving a class from a derived class, while multiple inheritance allows a class to inherit from more than one base class. Hybrid inheritance combines different inheritance types, such as hierarchical and multiple inheritance.
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Types of Inheritance in C
1. Inheritance is a mechanism of reusing and extending existing c
l
a
s
s
e
s
without modifying them, thus producing hierarchical relationships
betweenthem.Inheritanceis almostlike embedding anobjectinto aclass.
Type of Inheritance:
✓ Single inheritance
✓ Multi-level inheritance
✓ Multiple inheritance
✓ Hierarchical Inheritance
✓ Hybrid Inheritance
2. Explain different visibility modes used in inheritance.
Name-Anant Ambadas Patil
Roll no-20IF232
Unit- III : Extending classes using
inheritance
1. Define Inheritance and enlist its types.
Private visibility mode
When a base class is inherited with private visibility mode the public and
protected members of the base class become ‘private’ members of the derived
class
protected visibility mode
When a base class is inherited with protected visibility mode the protected and
2. public members of the base class become ‘protected members ‘ of the derived
class
public visibility mode
When a base class is inherited with public visibility mode , the protected
members of the base class will be inherited as protected members of the
derived class and the public members of the base class will be inherited as
public members of the derived class.
3. In this inheritance, a derived class is created from a single baseclass.
In the given example, Class A is the parent class and Class B is the child class
since Class B inherits the features and behavior of the parent class A.
3. State different types of inheritance with diagram.
Single inheritance
Multi-level inheritance
In this inheritance, a derived class is created from another derived class. In the given
example, class c inherits the properties and behavior of class B and class B
inherits the properties and behavior of class B. So, here A is the parent class of B
and class B is the parent class of C. So, here class C implicitly inherits the properties
and behavior of class A along with Class B i.e there is a multilevel of inheritance.
4. In this inheritance, a derived class is created from more than one base class. This
inheritance is not supported by .NET Languages like C#, F# etc. and Java Language.
In the given example, class c inherits the properties and behavior of class B and class
A at same level. So, here A and Class B both are the parent classes for Class C.
In this inheritance, more than one derived classes are created from a single base class
and futher child classes act as parent classes for more than one child classes.
In the given example, class A has two childs class B and class D. Further, class B
and class C both are having two childs - class D and E; class F and G respectively.
Multiple inheritance
Hierarchical Inheritance
5. This is combination of more than one inheritance. Hence, it may be a combination
of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or
Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple
inheritance.
Since .NET Languages like C#, F# etc. does not support multiple and multipath
inheritance. Hence hybrid inheritance with a combination of multiple or multipath
inheritances is not supported by .NET Languages.
Hybrid inheritance
4. Write a program to implement single inheritance from following figure,
accept and display data for 2 objects.
#include<iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
6. void get_stud_info()
{
cout<<"n Enter Student RollNo:";
cin>>rollno;
cout<<"n Enter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"nRoll No:"<<rollno;
cout<<"nName:"<<name;
}
};
class Marks:public Student
{
protected:
int m1,m2,m3,total;
float percentage;
public:
void get_test_marks()
{
cout<<"n Enter class Test-1 Marks:";
cin>>m1;
cout<<"n Enter Class Test-2 Marks:";
cin>>m2;
cout<<"n Enter Class Test-3 Marks:";
cin>>m3;
11. 6. Differentiate between multiple inheritance and multilevel inheritance.
S.NO Single inheritance Multiple inheritance
1. Single inheritance is one in which
the derived class inherits the
single base class.
Whereas multiple inheritance is one in
which the derived class acquires two or
more base classes.
2. In single inheritance, the derived
class uses the features of the
single base class.
While in multiple inheritance, the derived
class uses the joint features of the inherited
base classes.
3. Single inheritance requires small
run time as compared to multiple
inheritance due to less overhead.
While multiple inheritance requires more run
time time as compared to single inheritance
due to more overhead.
4. Single inheritance is a lot of close
to specialization.
In contrast, multiple inheritance is a lot of
close to generalization.
5. Single inheritance is implemented
as Class DerivedClass_name :
access_specifier Base_Class{};.
While multiple inheritance is implemented
as Class DerivedClass_name :
access_specifier Base_Class1,
access_specifier Base_Class2, ….{}; .
6. Single inheritance is simple in
comparison to the multiple
inheritance.
While multiple inheritance is complex in
comparison to the single inheritance.
7. Single inheritance can be
implemented in any programming
language.
C++ supports multiple inheritance but
multiple inheritance can’t be implemented in
any programming language(C#, Java
doesn’t support multiple inheritance).
6. Write a program that illustrates multilevel inheritance.
#include<iostream>
using namespace std;
class person
{
protected:
char name[20], gender[20];
int age;
public:
12. void get_person_info()
{
cout<<"n Enter Name:";
cin>>name;
cout<<"n Enter Gender:";
cin>>gender;
cout<<"n Enter Age:";
cin>>age;
}
void disp_person_info()
{
cout<<"nName :"<<name;
cout<<"nGender:"<<gender;
cout<<"nAge :"<<age;
}
};
class employee:public person
{
protected:
int emp_id;
char company[20];
float salary;
public:
void get_data()
{
cout<<"n Enter Employee ID:";
cin>>emp_id;
13. cout<<"n Enter Company Name:";
cin>>company;
cout<<"nEnter Salary";
cin>>salary;
}
void disp_data()
{
cout<<"nEmployee ID:"<<emp_id;
cout<<"nCompany :"<<company;
cout<<"nSalary :"<<salary;
}
};
class programmer:public employee
{
protected:
int no_of_prog_lang_known;
public:
void get_info()
{
cout<<"nProgramming Language known:";
cin>>no_of_prog_lang_known;
}
void disp_info()
{
cout<<"nNo. of Programming Language Known :"<<no_of_prog_lang_known;
}
};
15. 9. Explain hybrid inheritance with example.
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.
Below image shows the combination of hierarchical and multiple
inheritance: