You are on page 1of 15

Unit- III : Extending classes using inheritance

1. Define Inheritance and enlist its types.


 Inheritance is a mechanism of reusing and extending existing classes
without modifying them, thus producing hierarchical relationships
between them. Inheritance is almost like embedding an object into a class.
Type of Inheritance:
✓ Single inheritance
✓ Multi-level inheritance
✓ Multiple inheritance
✓ Hierarchical Inheritance
✓ Hybrid Inheritance

2. Explain different visibility modes used in inheritance.


 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.
3. State different types of inheritance with diagram.
 Single inheritance
In this inheritance, a derived class is created from a single base class.
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.

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

Hierarchical Inheritance

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

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 Roll No:";

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 Whereas multiple inheritance is one in
the derived class inherits the which the derived class acquires two or
single base class. more base classes.
2. In single inheritance, the derived While in multiple inheritance, the derived
class uses the features of the class uses the joint features of the inherited
single base class. base classes.
3. Single inheritance requires small While multiple inheritance requires more run
run time as compared to multiple time time as compared to single inheritance
inheritance due to less overhead. due to more overhead.
4. Single inheritance is a lot of close In contrast, multiple inheritance is a lot of
to specialization. close to generalization.
5. Single inheritance is implemented While multiple inheritance is implemented
as Class DerivedClass_name : as Class DerivedClass_name :
access_specifier Base_Class{};. access_specifier Base_Class1,
access_specifier Base_Class2, ….{}; .
6. Single inheritance is simple in While multiple inheritance is complex in
comparison to the multiple comparison to the single inheritance.
inheritance.
7. Single inheritance can be C++ supports multiple inheritance but
implemented in any programming multiple inheritance can’t be implemented in
language. 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 base
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:

Naik Siddhesh 20IF228

You might also like