• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Multiple Inheritance in C++
Next →
← Prev

Multiple Inheritance in C++

By Dinesh Thakur

So far, we have been deriving a class from a single base class. However, it is also possible to derive a class from two or more unreleased base classes. When we derive a class from two or more base classes, this form of inheritance is known as Multiple Inheritance in C++. It allows the new class to inherit the functionality of two or more well developed and tested base classes, thus reusing predefined classes’ features. Like Single Inheritance, each derived class in multiple inheritances must have a kind of relationship with its base classes. This form of inheritance is useful in those situations in which the derived class represents a combination of features from two or more base classes. For Example: In an educational institute, a member can act like a teacher and a student. He may be a student of higher class as well as teaching lower classes.

The syntax for declaring a derived class that inherited from more than one base classes is

class der_class_name access_specifier base1, access_specifier base2, ...
{
//Implementation
};

Here access_specifier may be either public, private or protected. Each base class from which class derived must be specified to the colon’s right after the access specifier and must be separated by commas.

The below figure shows multiple inheritances in which class C derived from class A and class B, independent of each other.

multiple inheritance in C++Here, class C inherits all the members of its base classes A and B. The following program segment shows how class C derived from two base classes A and B publicly.

class C : public A,public B {
 //Implementation
};

The inheritance rules and the usages of access specifier for each base class member remain the same as in single inheritance. The access to each base class member is independent of the access to the members of other base classes using which you derive your class. For example: In the above derivation, public members of the base classes A and B inherited as public and protected members are inherited as protected in the derived class C.

Therefore, the object of the derived class can access public members of all the classes.

It can be represented in the following figure :

base class members in Multiple Inheritance in C++Some examples of derive class specification for inheriting a class from more than one base classes are as follows :

Class C : private A, private B {.....};
Class C : protected A, public B {....};
Class C : public A, B {... };
Class C : A, B { ... };

In the above examples, if we skip the access specifier while deriving a class, then it is private by default.

#include<iostream.h>
#include<conio.h>
class base1 {
 protected:
   int x;
 public:
  void readx() {
   cout<<"Enter value of x :";
   cin>>x;
  }
  void showx()
    {cout<<"x= "<<x;}
};
class base2 {
  protected:
   int y;
  public:
   void ready() {
    cout<<''Enter value of y :";
    cin>>y;
   }
   void showy()
     {cout<<"\ny "<<y;}
};
class der public basel,public base2 {
 private:
   int z;
 public:
  void add() {
    z=x+y; //Accessing protected members of base classes
  }
  void showz () 
   { cout<<"\nz "<<z; }
};
int main() {
  clrscr();
  der d1;
  d1.readx();//calling readx() of basel
  d1.ready{);//calling ready() of base2
  d1.add();//calling add() of der
  d1.showx();//calling showx() of basel
  d1.showy();//calling showy() of base2
  d1.showz();//calling showz() of der
  getch(); return 0;
}
Output
Enter value of x : 10
Enter value of y : 20
x = 10
y = 20
z = 30

Explanation: In this program, we define a class base1 containing a protected data member x and public member functions readx() and showx() for inputting and displaying the value of x. Similarly, another class base2 is defined that contains a data member y and two public member function ready() and showy(). The class der is inherited from both the base classes base1 and base2 publicly through multiple inheritances. As the class der is derived publicly from both the base class base1 and base2, the derived class object can access public base1 and base2 in the main(). In the main() , the statement d1.readx () ; invokes the member function readx() of the base1 class for inputting value of x. Similarly the statement, d1.ready() ; invokes the ready() of base2 for inputting value of y. The statement d1.add(); invokes the member function add() of the der class to perform the addition of values of protected data members x and y as they can be accessed directly in the der class. The remaining statement displays the values of x, y and z.

You’ll also like:

  1. Multiple Levels of Inheritance
  2. Multiple Inheritance in Java with Example
  3. Multiple Threads in Java by Inheritance
  4. Write A C++ Program To Show The Relationship Of Multiple Data With Multiple Object.
  5. What is MIMD (Multiple-instruction multiple-data)?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW