Answer to Question #191997 in C++ for ram@2002

Question #191997

Create the class Box with attributes length, breath, and height to find the volume of a box. Assign the values for data members using a copy constructor and display the result. Finally, free the resources of data objects using the destructor member function. (Note: Volume of a box = length * breath * height)

RUN TIME INPUT:

5.2

3.3

15.5

Output :

265.98


1
Expert's answer
2021-05-14T23:08:36-0400
#include <iostream>
using namespace std;
class Box{
    float *length, *breadth, *height;
    public:
        Box(float l, float w, float h){
            length = new float(l);
            breadth = new float(w);
            height = new float(h);
        }
        float Volume(){
            return *length * *breadth * *height;
        }
        Box(const Box &other){
            cout<<"\nCopy constructor called\n";
            this->length = new float(*other.length);
            this->breadth = new float(*other.breadth);
            this->height = new float(*other.height);
        }
        ~Box(){
            delete length;
            delete breadth;
            delete height;
        }
};
int main(){
    
    Box A(3, 5, 6);
    cout<<"Volume of A(by sample input in program): "<<A.Volume();
    Box B(5.2,3.3,15.5);
    cout<<" \nVolume (when Length = 5.2 , Breath= 3.3 , Height=15.5):  "<<B.Volume();
    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