Inheritance With Base Class Parameterized Constructor in C++

Inheritance With Base Class Parameterized Constructor in C++


  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Area  
  5. {  
  6. private:  
  7.     int length;  
  8.     int width;  
  9. public:  
  10.     Area(int a,int b){  
  11.         length=a;  
  12.         width=b;  
  13.     }  
  14.     int getArea(){  
  15.         return length*width;  
  16.     }  
  17. };  
  18.   
  19. class Volume:public Area  
  20. {  
  21. private:  
  22.     int height;  
  23. public:  
  24.     Volume(int a,int b,int c):Area(a,b){ /// Call constructor of the base class with parameters from constructor of the derived class  
  25.         height=c;  
  26.     }  
  27.     void getVolume(){  
  28.         cout<<"Volume : "<<height*getArea()<<endl;  
  29.     }  
  30. };  
  31.   
  32.   
  33. int main()  
  34. {  
  35.     Volume volume(3,5,7);  
  36.     volume.getVolume();  
  37.     return 0;  
  38. }  
Share:

0 Comments:

Post a Comment