[go: up one dir, main page]

0% found this document useful (0 votes)
4 views4 pages

C-- Multiple Inheritance (1)

C language

Uploaded by

504 ,DALVI RUDRA
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)
4 views4 pages

C-- Multiple Inheritance (1)

C language

Uploaded by

504 ,DALVI RUDRA
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/ 4

Inheritance is one of the key features of Object-oriented programming in C++.

It allows us to create a new class (derived class) from an existing class (base
class).

The derived class inherits the features from the base class and can have
additional features of its own.

is-a relationship
Inheritance is an is-a relationship. We use inheritance only if an is-a
relationship is present between the two classes.

Here are some examples:

● A car is a vehicle.
● Orange is a fruit.
● A surgeon is a doctor.
● A dog is an animal.

C++ Multiple Inheritance

In C++ programming, a class can be derived from more than one parent. For example, A class
Bat is derived from base classes Mammal and WingedAnimal. It makes sense because bat is a
mammal as well as a winged animal.
● Code :
Develop a C++ microproject to print college information using multiple inheritance
(1)College name
(2)no.of branches
(3) no. Of students in each branch

OUTPUT:-
In this example, we have three base classes: CollegeName, NumberOfBranches, and

NumberOfStudents. The CollegeInformation class is derived from these three base classes using

multiple inheritance. You can create an instance of CollegeInformation and set the college name,

number of branches, and the number of students in each branch, and then display the college

information using the displayCollegeInfo() method.

Ambiguity in Multiple Inheritance

The most obvious problem with multiple inheritance occurs during function
overriding.

Suppose, two base classes have a same function which is not overridden in
derived class.

If you try to call the function using the object of the derived class, compiler
shows error. It's because compiler doesn't know which function to call.

Advantages of Multiple Inheritance in C++

1. Multiple Inheritance in C++ allows a derived class to inherit more properties


and characteristics since it has multiple base classes.
2. It proves to be beneficial to design various design patterns. Some of these
patterns are:
1. Adapter pattern: This pattern is used when you need to adapt to another
interface with your class. For this, Multiple Inheritance in C++ plays a big role
in swapping an interface with another.
2. Observer patterns: This pattern is used to maintain a list of observers by
creating a class.

You might also like