Multilevel Inheritance: In this inheritance,the properties of parent class is inherited by child class and again the properties of child class is inherited by another child classone and this contiues.Here,the child classone acquires the properties of both child class and parent class.
syntax: class Parent
{
code specific to parent
};
class Child: visibility mode Parent
{
code related to child
};
class ChildOne : visibility mode Parent,visibility mode Child
{
code related to ChildOne
};
Example:
1.Define a class Student which has roll number and member function to input and display rollnumber. Derive a class Examination from class Student which has marks of two subjects and member function to initialize and display marks. Agian derive a class Result from class Examination and calculate total and display result.
#include<iostream.h>
#include<conio.h>
class Student
{
protected: int roll;
public: void inputdetails();
void displaydetails();
};
class Examination: public Student
{
protected: int subject1;
int subject2;
public : void setmarks();
void displaymarks();
};
class Result: public Examination
{
private: int total;
public: void displayResult();
};
void Student::inputdetails()
{
cout<<"Enter the roll no:";
cin>>roll;
}
void Student::displaydetails()
{
cout<<"The roll no is:"<<roll<<endl;
}
void Examination::setmarks()
{
cout<<"Enter the marks of Subject1:";
cin>>subject1;
cout<<"enter the marks of subject2:";
cin>>subject2;
}
void Examination::displaymarks()
{
cout<<"The marks of subject1 & subject2 :" <<subject1<<"&"<<subject2<<endl;
}
void Result::displayResult()
{
total=subject1 + subject2;
cout<<"The total marks is:"<<total<<endl;
}
void main()
{
Result obj;
obj.inputdetails();
obj.setmarks();
cout<<endl;
cout<<"Details of Given Information"<<endl;
obj.displaymarks();
obj.displaydetails();
obj.displayResult();
getch();
}
Comments
Post a Comment