Answer to Question #191978 in C++ for Ajith

Question #191978

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-13T05:26:00-0400
#include <iostream>

class Box {
private:
    double *length;
    double *breath;
    double *height;
public:
    Box(double length, double breath, double height);
    Box(const Box& copy);
    ~Box();
    double findVolume();
};


Box::Box(double length, double breath, double height) {
    this->length = new double(length);
    this->breath = new double(breath);
    this->height = new double(height);
}

Box::Box(const Box& copy) {
    this->length = new double(*copy.length);
    this->breath = new double(*copy.breath);
    this->height = new double(*copy.height);
}

Box::~Box() {
    delete length;
    delete breath;
    delete height;
}

double Box::findVolume() {
    return *length * *breath * *height;
}

int main() {
    Box one(10, 15, 20);
    Box two(one);

    std::cout << "The volume of the first box is: " << one.findVolume() << std::endl;
    std::cout << "The volume of the second box (created using copy constructor) is: " << two.findVolume() << std::endl;

    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