Answer to Question #272338 in C++ for Asim

Question #272338

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

1
Expert's answer
2021-11-27T11:34:37-0500
#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; 
  }
  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS