Dynamic Objects and Run Time Polymorphism
Develop a C++ program to calculate the area of a box and compare the size of two boxes, and display which box is smaller using this pointer.
#include <iostream>
using namespace std;
class Box{
float length, width, height, size;
public:
Box(){}
Box(float l, float w, float h){
this->length = l;
this->width = w;
this->height = h;
this->size = l * w * h;
}
void operator==(const Box a){
cout<<"The size of the smaller box is ";
if(this->size > a.size) cout<<a.size;
else cout<<this->size;
}
};
int main(){
Box* boxes;
float l, w, h;
for(int i = 0; i < 2; i++){
cout<<"Input dimensions of box "<<i + 1<<endl;
cin>>l;
cin>>w;
cin>>h;
boxes[i] = Box(l, w, h);
}
boxes[0] == boxes[1];
return 0;
}
Comments
Leave a comment