Answer to Question #191903 in C++ for VARUN

Question #191903

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


1
Expert's answer
2021-05-12T14:56:53-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: "<<A.Volume();
    Box B(A);
    cout<<"Volume of B: "<<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