Thursday 9 April 2015

Multilevel Inheritance

Derivation of a class from another derived class is called multilevel inheritance. It is very common in inheritance that a class is derived from a derived class, from the figure, the class B is the base class for the derived class D1, which is in turn serves as a base class for the derived class D2. The class D1 provides a link for the inherit ance between B and D2, and is known as intermediate base class. The chain B, D1, D2 is known as the inheritance path.

#include <iostream.h>
class student
{
protected:
int rno;
public:
void getnumber(int);
void putnumber(void);
};
void student::getnumber(int a)
{
rno = a;
}
void student::putnumber(void)
{
cout<<”Roll Number is”<<rno<<endl;
}
class test:public student
{
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(void)
{
cout<<”marks in sub1 = “<<sub1<<endl;
cout<<”marks in sub2 = “<<sub2<<endl;
}
class result:public test
{
float total;
public:
void display(void);
};
void result::display(void)
{
total = sub1 + sub2;
putnumber();
put_marks();
cout<<”Total = “<<total<<endl;
}
void main()
{
result student1;
student1.getnumber(111);
student1.get_marks(56,45);
student1.display();
}

No comments:

Post a Comment