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