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
{
……….
……….
};
class B: public A
{
…………
…………
…………
}
class C: public B
{
…………
…………
…………
}
|
This process can be extended to any number of levels
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
{
:
;
:
get_number(int);
put_number();
};
student::get_number(int a)
{
roll=a;
}
student:: put_number()
{
std::cout << "roll number:" << roll << "\n";
}
class test: public student
{
:
sub1;
sub2;
:
get_marks(float,float);
put_marks(void);
};
test::get_marks(float x, float y)
{
sub1=x;
sub2=y;
}
test::put_marks()
{
std::cout << "MArks in SUB1=" << sub1 << "\n";
std::cout << "MArks in sub2=" << sub2 << "\n";
}
class Result: public test
{
total;
:
display();
};
|
the class result,after inheritance from grandfather through father would contain the following members
:
total;
:
;
sub1;
sub2;
:
get_number(int);
put_number();
get_marks(float,float);
put_marks();
display();
|
The inherited functions put_number() and put_marks() can be used in the definition of display() function:
result:: display()
{
total=sub1+sub2;
put_number();
put_marks();
std::cout << "total=" << total << "\n";
}
here is simple main() program
main()
{
result student1;
student1.get_number(111);
student1.get_marks(7.50,59.5);
student1.display();
0;
}
|
This will display the result of student1. The complete program is shown in below program
<iostream>
using namespace std;
class student
{
:
;
:
get_number(int);
put_number();
};
student::get_number(int a)
{
roll=a;
}
student :: put_number()
{
std::cout << "roll number" << roll << "\n";
}
class test: student
{
:
sub1;
sub2;
:
get_marks(float,float);
put_marks();
};
test:: get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
test:: put_marks()
{
std::cout << "marks in sub1=" << sub1 << "\n";
std::cout << "marks in sub2=" << sub2 << "\n";
}
class reult: test
{
total;
:
display();
};
reult:: display()
{
total=sub1+sub2;
put_number();
put_marks();
std::cout << "total=" << total << "\n";
}
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
|