Advertisement Remove all ads

Write the Definition of a Class Container in C++ with the Following Description. Private Members Public Members - Computer Science (C++)

Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads

Write the definition of a class CONTAINER in C++ with the following description.

Private Members

-Radius, Height      // float

- Type                  // int (1 for Cone, 2 for Cylinder)

- Volume              // float

- CalVolume()       // Member function to calculate

                          // volume as per the Type

Type Formula to calculate Volume
1 3.14*Radius*Height
2 3.14*Radius*Height/3

Public Members

- GetValues ()    //A function to allow a user to enter of Radius, Height and Type. Also, call function CalVolume() from it.

- ShowAll ()      // A function to display Radius, Height, Type and Volume of Container

Advertisement Remove all ads

Solution

Class CONTAINER
{ 
    float Radius, Height;
    int Type;
    float Volume ;
    void CalVolume(float Radius, float Height, int n)
    {
       Volume = (3.14*Radius*Radius*Height)/n;
    }
    void CalVolume(float Radius, float Height)
    {
       Volume = 3.14*Radius*Radius*Height;
    }

public:
      void GetValues( )
      {
         cout << "Enter Radius";
         cin >>Radius;
         cout<<"Enter Height";
         cin>>Height;
         cout<<"Enter Type (1 for cone and 2 for cylinder)";
         cin>>Type;
         switch(Type)
         {
            case 1: CalVolume(Radius, Height, 3);
                    ShowAll();
                    break;
           case 2: CalVolume(Radius, Height);
                   ShowAll( );
                   break;
         }
}
void ShowAll()
{
    cout<< "Radius : "<<Radius;
    cout<<"Height :"<< Height;
    cout << "Volume : "<<Volume;
}
};
Concept: Member of a Class - Data Members and Member Functions (Methods)
  Is there an error in this question or solution?
Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×