English

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

Advertisements
Advertisements

Question

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

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;
}
};
shaalaa.com
Member of a Class - Data Members and Member Functions (Methods)
  Is there an error in this question or solution?
2017-2018 (March) All India Set 4

RELATED QUESTIONS

Find and write the output of the following C++ program code:

Note: Assume all required header files are already included in the program

void Revert(int &Num, int Last=2)
{
   Last =(Last%2==0)?Last+l: Last - l;
   for(int C=l; C <= Last; C++)
      Num+=C;
)

void main()
{
    int A=20, B=4;
    Revert(A, B) ;
    cout<<A<<"&"<<B<<end1;
    B--;
    Revert{A,B);
    cout<<A<<"#"<<B<<endl;
    Revert{B);
    cout<<A<<"#"<<B<<endl;
}

Observe the following C++ code and answer the questions (i) and (ii).

Note: Assume all necessary files are included.

class FIRST
{
   int Numl;
public:
    void Display()
    {
        cout<<Numl<<endl;            //Member Function 1
    }
};

class SECOND: public FIRST
{
    int Num2;
public:
     void Display()                //Member Function 2
     {
         cout<<Num2<<endl;
     }
};

Void main()
{
   SECOND S;
   ___________                    // Statement 1
   ___________                    // Statement 2
  
}

 

1) Which Objected Programming features is illustrated by the definition of classes FIRST and SECOND?

2) Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively using the object S


Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum values that can be assigned to each of the variables N and M

Note:

  • Assume all the .required header files are already being included in the code.
  • The function random(n) generates an integer between 0 and n-1
void main()
{
    randomize() ;
    int N=random(3), M=random(4);
    int DOCK[3][3] = {{1,2,3},{2,3,4}, {3,4,5}};
    for(int R=O; R<N; R++)
   {
       for(int C = 0; C< M; C++)
·        cout<<DOCK[R][C]<<" ";
       cout<<endl;
    }
}
1)
2)
3)
4)

Observe the following C++ code and answer the questions (i) and (ii).

Note: Assume all necessary files are included

class TEST
{
    long TCode ;
    char TTitle[20];
    float Score;
public:
    TEST()      //Member Function 1
    {
         TCode=100;strcpy(TTit1e,"FIRST Test"); Score=O;
    };
    TEST(TEST &T)    //Member Function 2
    {
         TCode=E.TCode+1;
         strcpy(TTitle , T.TTitle);
         Score=T.Score;
     }
};

void main()
{
   __________    //Statement 1
   __________    //Statement 2
}

1) Which Object-Oriented Programming feature is illustrated by the Member Function 1 and the Member Function 2 together in the class TEST?

2) Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively.


Write the definition of a member function PUSHGIFT() for a class STACK in C++, to add a GIFT in a dynamically allocated stack of GIFTs considering the following code is already written as a part of the program

struct GIFT
{
    int GCODE;           //Gift Code
    char GDESC[20];      //Gift Description
    GIFT *Link;
};
class STACK
{
    Gift *TOP;
public:
    STACK(){TOP=NULL;}
    void PUSHGIFT{);
    void POPGIFT();
    ~STACK();
};

Observe the following C++ code and answer the questions (i) and (ii):

class Passenger
{
   long PNR;
   char Name[20];
public:
   Passenger () · //Function 1
   { cout<<"Ready"<<endl; }

   void Book(long P,char N[]) //Function 2
   { PNR = P; strcpy(Name, N);}

   void Print () //Function 3
   { cout«PNR << Name <<endl; }
   
   -Passenger() //Function 4
   { cout<<"Booking cancelled! "<<endl;}
};

1) Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code:

void main()
{
   Passenger P;
   _________      //Line 1
   _________      //Line 2
}//Ends here

2) Which function will be executed at } // Ends here? What is this function referred as?


Write the definition of a class Photo In C++ with the following description:

Private Members

- Pno    //Data member for Photo Number (an integer)

-Category //Data.member for Photo Category (a string)

- Exhibit I // Data member for Exhibition Gallery (a string) 

- FixExhibit //A member function to assign Exhibition Gallery as per Category as shown in the following table

Category Exhibit
Antique Zaveri
Modern Johnsen
Classic Terenida

Public Members

-Register()   //A function to allow user to enter values Pno I category and call FixExhibi t () function

- ViewAll()  //A function to display all the data members


Write the definition of a member function PUSH( ) in C++, to add a new book in a dynamic stack of BOOKS considering the following code is already included in the program

struct BOOKS
{
   char ISBN[20], TITLE[80];
   BOOKS *Link;
};
class STACK
{
   BOOKS *Top;
public:
   STACK-() {Top=NULL;}
   void PUSH();
   void POP();
   ~STACK();
};

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×