C++ Inheritance introduction | Practice | GeeksforGeeks

Create two classes:
Cuboid
The Cuboid class should have three data fields- length, width and height of int types. The class should have display() method, to print the length, width and height of the cuboid separated by space.
CuboidVol 
The CuboidVol class is derived from Cuboid class, i.e., it is the sub-class of Cuboid class. The class should have read_input() method, to read the values of length, width and height of the Cuboid. The CuboidVol class should also overload the display() method to print the volume of the Cuboid ( length * width * height ).
Input:
The first line contains the number of test cases and one and only line of each test case contains 3 space separated integer denoting length, width, and height of the Cuboid
Output:
The output should consist of exactly two lines: 
In the first line, print the length, width, and height of the cuboid separated by space. 
In the second line, print the volume of the cuboid.
Constraints:
0 <= (length, width, height) <= 100
Example:
Sample input:
1
12 10 2
Sample output:
12 10 2 
240
Explanation:
As here length = 12, width = 10 and height = 2
Volume of the cuboid is = ( length * width * height )
                                      = 12 * 10 * 2
                                      = 240
 
0
atulharsh274Premium2 weeks ago
class Cuboid {  //Add your code here.  protected:  int length,width,height;  public:  void display(){      cout<<length<<" "<<width<<" "<<height<<endl;  }};
class CuboidVol: public Cuboid{   //Add your code here.   public:   void read_input(int l , int w , int h){       length = l;        width = w;        height = h;   }   void display(){       cout<<length*height*width<<endl;   }};
 
0
vamsichippanapalli3 months ago
// { Driver Code Starts//Initial Template for C++
#include <iostream>using namespace std;
// } Driver Code Ends//User function Template for C++
class Cuboid {   protected:  int lenth;  int width;  int height; public:   void read_input(int l,int w,int h)  {   lenth=l;   width=w;   height=h;      }  void display()    {       cout<<endl<<lenth<<" "<<width<<" "<<height<<endl;    }};
class CuboidVol: public Cuboid{ public:   void display()     {            cout<<endl<<(lenth*width*height);     }};
// { Driver Code Starts.
int main(){   int t;   cin>>t;      while(t--)   {       int l, w, h;       cin>>l>>w>>h;      // Declare a CuboidVol object      CuboidVol c_vol;         // Read length width and height       c_vol.read_input(l,w,h);         //  Print length, width and height      c_vol.Cuboid::display();         // Print the Volume       c_vol.display();   }      return 0;}  // } Driver Code Ends
0
vamsichippanapalli3 months ago
void display()    {       cout<<lenth<<" "<<width<<" "<<height<<endl;    }
0
tushargarg98683 months ago
// { Driver Code Starts//Initial Template for C++
#include <iostream>using namespace std;
// } Driver Code Ends//User function Template for C++
class Cuboid {  //Add your code here.  protected:  int length;  int width;  int height;  public:  void display(){      cout<<length<<" "<<width<<" "<<height<<endl;  }};
class CuboidVol: public Cuboid{   //Add your code here.   public:   void read_input(int l, int w, int h){       length=l;       width=w;       height=h;   }      void display(){       cout<<(length*width*height)<<endl;  }};
// { Driver Code Starts.
int main(){   int t;   cin>>t;      while(t--)   {       int l, w, h;       cin>>l>>w>>h;      // Declare a CuboidVol object      CuboidVol c_vol;         // Read length width and height       c_vol.read_input(l,w,h);         //  Print length, width and height      c_vol.Cuboid::display();         // Print the Volume       c_vol.display();   }      return 0;}  // } Driver Code Ends
0
himanshuharsh19993 months ago
class Cuboid 
{
   protected:
        int length, width, height;
    public:
        void display(){
            cout<<length<<" "<<width<<" "<<height<<endl;
        }
};

class CuboidVol: public Cuboid
{
    public:
    void read_input(int l, int w, int h)
    {
        length = l;
        width = w;
        height = h;
    }
    void display(){
        cout<<length*width*height<<endl;
    }
};
0
martial9 months ago
martial
https://uploads.disquscdn.c... https://uploads.disquscdn.c...
0
Utkarsh Aditya
This comment was deleted.
0
Utkarsh Aditya
This comment was deleted.
0
Utkarsh Aditya
This comment was deleted.
0
Utkarsh Aditya
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.