Create a Class named base which has two data members. Then derive a class
derived1 from base class which has one data members. Derive a class derived2 from
derived1.
i. Write functions for each class to get and display values.
ii. Write main() function to create object of derived2 and through that object
access the data member of base class and derived1 class
#include<iostream>
using namespace std;
class base
{
protected:
int a,b;
public:
void getdata()
{
cout<<" Enter num 1 ";
cin>>a;
cout<<" Enter num 2 ";
cin>>b;
}
void display()
{
cout<<"\n\n Num 1 = "<<a<<endl<<"Num2 = "<<b<<endl;
}
};
class derived1:public base
{ protected:
int c;
base A1;
public:
void getdata()
{
cout<<"Enter Num 3";
cin>>c;
}
void display()
{
A1.display();
cout<<" Num 3 = "<<c<<endl;
}
};
class derived2:public derived1
{ public:
void display()
{
cout<<"num1 ="<<a<<endl<<"num2 = "<<b<<endl<<"num3 = "<<c<<endl;
}
};
int main()
{
derived2 a2;
a2.base::getdata();
a2.derived1::getdata();
a2.display();
return 0;
}
Comments
Leave a comment