C++ multilevel inheritance

C++ multilevel inheritance

it is not uncommon that a class is derived from another derived class who in the below fig.
The class A is a base(super) class for the derived(child) class B , which in turn serves as a base class for the derived class C. the class B is know as intermediate base class since it provides a link for the inheritance between A and C . The chain ABC is know as inheritance path

A derived(child) class with multilevel inheritance is declared as follows

class A
{ // Base class
……….
……….
};
class B: public A
{
………… //B derived from A
…………
…………
}
class C: public B
{ // C derived from B
…………
…………
…………
}

This process can be extended to any number of levels

multilevel inheritance pic in c++

Let us consider a simple example . Assume that the test result of a batch of students are stored in three different classes.class every student stored the roll number, class test stores the marks obtained in 2 subjects and class result stores the total marks obtained in the test. the class result can inherit the details of the marks obtained in the test and the roll number of students through multilevel inheritance

let us now consider the case of private derivation

let us take example
class student
{
protected:
int roll;
public:
void get_number(int);
void put_number();
};
void student::get_number(int a)
{
roll=a;
}
void student:: put_number()
{
std::cout << "roll number:" << roll << "\n";
}
class test: public student // first level derivation
{
protected:
float sub1;
float sub2;
public:
void get_marks(float,float);
void put_marks(void);
};
void test::get_marks(float x, float y)
{
sub1=x;
sub2=y;
}
void test::put_marks()
{
std::cout << "MArks in SUB1=" << sub1 << "\n";
std::cout << "MArks in sub2=" << sub2 << "\n";
}
class Result: public test // second level derivation
{
float total; // private by default
public:
void display();
};

the class result,after inheritance from grandfather through father would contain the following members

private:
float total;
protected:
int roll;
//inherited from student via test
float sub1; // inherit from test
float sub2; // inherit from test
public:
void get_number(int); // from student via test
void put_number(); // from student via test
void get_marks(float,float); // from test
void put_marks(); // from test
void display(); // own member

The inherited functions put_number() and put_marks() can be used in the definition of display() function:

void result:: display()
{
total=sub1+sub2;
put_number();
put_marks();
std::cout << "total=" << total << "\n";
}
here is simple main() program
int main()
{
result student1;
student1.get_number(111);
student1.get_marks(7.50,59.5);
student1.display();
return 0;
}

This will display the result of student1. The complete program is shown in below program

#include <iostream>
using namespace std;
class student
{
protected:
int roll;
public:
void get_number(int);
void put_number();
};
void student::get_number(int a)
{
roll=a;
}
void student :: put_number()
{
std::cout << "roll number" << roll << "\n";
}
class test: public student
{
protected:
float sub1;
float sub2;
public:
void get_marks(float,float);
void put_marks();
};
void test:: get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void test:: put_marks()
{
std::cout << "marks in sub1=" << sub1 << "\n";
std::cout << "marks in sub2=" << sub2 << "\n";
}
class reult: public test
{
float total;
public:
void display();
};
int reult:: display()
{
total=sub1+sub2;
put_number();
put_marks();
std::cout << "total=" << total << "\n";
}
int main()
{
reult student1;
student1.get_number(111);
student1.get_marks(80.8,78.5);
student1.display();
}

display the following output
roll number:111marks in SUB1=75
MArks in SUB2=59.5
Total=134.5

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Multilevel Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise