Question #181894

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.


Runtime input

3.3

1.2

1.5

8.5

6.1

2.2

Output should be

5.94


Expert's answer

#include <iostream>
using namespace std;
class Box{
    float l, w, h, s;
    public:
    Box(){}
    Box(float length, float width, float height){
        this->l = length;
        this->w = width;
        this->h = height;
        this->s = l * w * h;
    }
    void operator==(const Box a){
        if(this->s > a.s) cout<<a.s;
        else cout<<endl<<this->s;
    }
};
int main(){
    Box boxe[2];
    float length, width, height;
    for(int i = 0; i < 2; i++){
        cin>>length;
        cin>>width;
        cin>>height;
        boxe[i] = Box(length, width, height);
    }
    boxe[0] == boxe[1];
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS