• 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++ » Hybrid Inheritance in C++
Next →
← Prev

Hybrid Inheritance in C++

By Dinesh Thakur

In some situations, it is essential to design a program using two or more forms of Inheritance. Combining two or more forms of Inheritance to design a program is known as Hybrid Inheritance in C++. Such a form of Inheritance represents a complex class hierarchy. The following figures show some possible representations of hybrid inheritance.

hybrid inheritance in c++The above Figure A represents hybrid Inheritance as it is a combination of multilevel and multiple Inheritance. Similarly, figure B represents hybrid Inheritance as it is a combination of multiple Inheritance and multilevel Inheritance with a different structure.

To understand hybrid Inheritance, consider an example of calculating a student’s result based on marks obtained in internal and external examinations. To simulate this problem, the various classes involved and their inheritance relationships are described in Figure, which indicates both multilevel and multiple Inheritance.

The student class maintains basic information of a student like rollno and name. The class exam_internal derived from student class that inherits students’ information and maintains marks obtained in the internal examination. The class exam_external maintain marks obtained in his external examination. We assume that the maximum marks in a subject for internal and external examination are 40 and 60, respectively. (So maximum marks in a subject is 100). The resulting class derived from the classes exam_internal and exam_external that calculates the result of a student. The above hybrid inheritance implements in the following program.

#include<iostream.h>
#include<conio.h>
class student {
 private:
  int rollno;
  char name[20];
 public:
  void read() {
    cout<<"\nEnter rollno and name :";
    cin>>rollno>>name;
}
  void show() {
    cout<<"\nRoll no = "<<rollno;
    cout<<"\nName = "<<name;
  }
};
class exam_internal : public student {
 protected:
  int sub1_marks;
  int sub2_marks;
 public:
  void read_marks() {
   cout<<"Enter Internal marks of subject 1(MAX 40) ";
   cin>>sub1_marks;
   cout<<"Enter Internal marks of subject 2(MAX 40) ";
   cin>>sub2_marks;
  }
  void display_marks() {
   cout<<"\nInternal marks of subject 1 = "<<sub1_marks;
   cout<<"\nInternal marks of subject 2 = "<<sub2_marks;
  }
};
class exam_external {
 protected:
  int sub1_ext_marks;
  int sub2_ext_marks;
 public:
  void read_marks() {
   cout<<"Enter external marks of subject 1 = ";
   cin>>sub1_ext_marks;
   cout<<"Enter external marks of subject 2 = ";
   cin>>sub2_ext_marks;
}
 void display_marks() {
  cout<<"\nExternal marks of subject 1 "<<sub1_ext_marks;
  cout<<"\nExternal marks of subject 2 "<<sub2_ext_marks;
 }
};
class result:public exam_internal, public exam_external {
 private:
  int total_marks;
 public: 
  void cal_result() {
    total_marks=sub1_marks + sub2_marks + sub1_ext_marks + sub2_ext_marks;
    cout<<"Total mark obtained = "<<total_marks; 
  }
};
int main() {
 clrscr();
 result r1;
 cout<<"Enter student information : \n";
 r1.read(); //call read() of student class
 cout<<"Enter marks of Internal Examination :\n";
 r1.exam_internal::read_marks();
 cout<<"\nEnter marks of External Examination : \n";
 r1.exam_external::read_marks();
 cout<<"Displaying Student Details \n";
 r1.show(); //calls show() of student class
 r1.exam_internal::display_marks();
 r1.exam_external::display_marks();
 cout<<"\ncalculating and displaying result\n";
 r1.cal_result();//call cal_result of result class
 getch(); return 0;
}

Explanation: In main(), we create the object r of the result class through which we input the student’s basic information, his internal and external marks details. Then, the inputted information is displayed—finally, the statement.

r1.cal_result();

invokes the cal_result ()of result class that calculated the total marks obtained by a student, displayed.

You’ll also like:

  1. What is Hybrid networks?
  2. What is Inheritance? Types of Inheritance in c++
  3. Multiple Inheritance in C++
  4. Inheritance in Java
  5. What is Class Inheritance?
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