SlideShare a Scribd company logo
1 of 15
Download to read offline
 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
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.
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.
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
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:
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;
total=m1+m2+m3;
cout<<"nTotal Marks : "<<total;
percentage=total/3;
cout<<"nPercentage : "<<percentage;
}
void disp_test_marks()
{
cout<<"nClass Test-1 Marks:"<<m1;
cout<<"nClass Test-2 Marks:"<<m2;
cout<<"nClass Test-3 Marks:"<<m3;
cout<<"nTotal Marks :"<<total;
cout<<"nPercentage :"<<percentage;
}
};
int main()
{
Marks m1;
m1.get_stud_info();
m1.get_test_marks();
cout<<"n**********************STUDENT INFORMATION**********************";
m1.disp_stud_info();
m1.disp_test_marks();
return 0;
}
5. Write a program to demonstrate constructor in derived class.
#include <iostream.h>
class alpha
(
private:
int x;
public:
alpha(int i)
{
x = i;
cout << "n alpha initialized n";
}
void show_x()
{
cout << "n x = "<<x;
}
);
class beta
(
private:
float y;
public:
beta(float j)
{
y = j;
cout << "n beta initialized n";
}
void show_y()
{
cout << "n y = "<<y;
}
);
class gamma : public beta, public alpha
(
private:
int n,m;
public:
gamma(int a, float b, int c, int d):: alpha(a), beta(b)
{
m = c;
n = d;
cout << "n gamma initialized n";
}
void show_mn()
{
cout << "n m = "<<m;
cout << "n n = "<<n;
}
);
void main()
{
gamma g(5, 7.65, 30, 100);
cout << "n";
g.show_x();
g.show_y();
g.show_mn();
}
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:
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;
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;
}
};
int main()
{
programmer p;
p.get_person_info();
p.get_data();
p.get_info();
p.disp_person_info();
p.disp_data();
p.disp_info();
return 0;
}
8. Describe multiple inheritances with suitable example.
 Multiple inheritance occurs when a class inherits from more than one b
a
s
e
class. So the class can inherit features from multiple base classes using
multiple inheritance. This is an important feature of object oriented
programming languages such as C++.
A diagram that demonstrates multiple inheritance is given below −
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:

More Related Content

What's hot

What's hot (20)

11 Unit 1 Chapter 03 Data Handling
11   Unit 1 Chapter 03 Data Handling11   Unit 1 Chapter 03 Data Handling
11 Unit 1 Chapter 03 Data Handling
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Data types in php
Data types in phpData types in php
Data types in php
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Hashing and separate chain
Hashing and separate chainHashing and separate chain
Hashing and separate chain
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Pointers
PointersPointers
Pointers
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Stack
StackStack
Stack
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 

Similar to Types of Inheritance in C

Similar to Types of Inheritance in C (20)

Inheritance
InheritanceInheritance
Inheritance
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4
 
29c
29c29c
29c
 

Recently uploaded

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 

Recently uploaded (20)

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
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;
  • 7. total=m1+m2+m3; cout<<"nTotal Marks : "<<total; percentage=total/3; cout<<"nPercentage : "<<percentage; } void disp_test_marks() { cout<<"nClass Test-1 Marks:"<<m1; cout<<"nClass Test-2 Marks:"<<m2; cout<<"nClass Test-3 Marks:"<<m3; cout<<"nTotal Marks :"<<total; cout<<"nPercentage :"<<percentage; } }; int main() { Marks m1; m1.get_stud_info(); m1.get_test_marks(); cout<<"n**********************STUDENT INFORMATION**********************"; m1.disp_stud_info(); m1.disp_test_marks(); return 0; }
  • 8. 5. Write a program to demonstrate constructor in derived class. #include <iostream.h> class alpha ( private: int x; public: alpha(int i) { x = i; cout << "n alpha initialized n"; } void show_x() { cout << "n x = "<<x; } ); class beta ( private: float y; public: beta(float j) { y = j;
  • 9. cout << "n beta initialized n"; } void show_y() { cout << "n y = "<<y; } ); class gamma : public beta, public alpha ( private: int n,m; public: gamma(int a, float b, int c, int d):: alpha(a), beta(b) { m = c; n = d; cout << "n gamma initialized n"; } void show_mn() { cout << "n m = "<<m; cout << "n n = "<<n; } );
  • 10. void main() { gamma g(5, 7.65, 30, 100); cout << "n"; g.show_x(); g.show_y(); g.show_mn(); }
  • 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; } };
  • 14. int main() { programmer p; p.get_person_info(); p.get_data(); p.get_info(); p.disp_person_info(); p.disp_data(); p.disp_info(); return 0; } 8. Describe multiple inheritances with suitable example.  Multiple inheritance occurs when a class inherits from more than one b a s e class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++. A diagram that demonstrates multiple inheritance is given below −
  • 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: