A chain of single inheritance is referred to as multilevel inheritance.



In this type of inheritance, class B plays a dual role and act both as a derived class as well as a base class. When the class B is inherited from class A, B is acting as a derived class. When the  class C is derived from the class B, B is acting as a base class for the derived class C. The class C holds the characteristics of both the classes,namely class A and class B, which in turn directs the user to reuse the definitions made in those two classes.

if (class B is inherited from A under the public access specifier) then
         class C will contain the public member of both class A and B
else
         class C will contain only the public member of B

the above statement clearly mentions the position of C, when the inheritance of B from A is public or not . In any type of derivation, the protected members of the class A will be visible to the public members of classes B only and class C can never access them.

Illustrate the program for multiple inheritance with public

#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class student:public stud
{
private:
int rank;
public:
void inputdata()
{
cout<<"Rank:";
cin>>rank;
}
void modifydata()
{
char bool;
cout<<endl<<"Do you want to modify?(y/N):";
cin>>bool;
if(bool=='Y')

cout<<"Age:";
cin>>age;
cout<<"Rank:";
cin>>rank;
}
}
void outputdata()
{
cout<<"Rank:"<<rank;
}
};
class medical : public students
{
private:
char bloodgp[4];
public:
void cinputdata()
{
inputdata();
cout<<"Blood Group:";
cin>>bloodgp;
}
void coutputdata()
{
outputdata();
cout<<endl<<"Blood Group:"<<bloodgp<<endl;
}
};
void main()
{
medical m;
m.cinputdata();
m.coutputdata();
m.modifydata();
m.coutputdata();

}

OUT PUT:

Name:cnu
Roll:10
Age:15
Rank:8
Blood Group:B+


Name:cnu
Roll:10
Age:15
Rank:8
Blood Group:B+
Do you want to modify?(Y/N) Y
Age:19
Rank:3

Name:cnu
Roll:10
Age:19
Rank:3
Posted by Unknown On 01:56 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *