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)
#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;
}
Comments
Leave a comment