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