Answer to Question #236024 in C++ for MAHA

Question #236024

Write a program in which you have to find the 'volume and Area of 5 Rooms' by using class and objects


1
Expert's answer
2021-09-11T14:25:10-0400
#include <iostream>
#include <vector>

class Room {
private:
    double height = 0;
    double area = 0;
public:
    Room(double height, double area)
    : height(height), area(area)
    {
    }

    double getArea() const {
        return area;
    }

    double getVolume() const {
        return area * height;
    }
};

class House {
private:
    std::vector<Room> rooms;

public:
    House(const std::vector<Room> &rooms) : rooms(rooms) {
    }

    double getArea() const {
        double area = 0;
        for (const Room &room: rooms)
            area += room.getArea();
        return area;
    }

    double getVolume() const {
        double volume = 0;
        for (const Room &room: rooms)
            volume += room.getVolume();
        return volume;
    }
};

int main()
{
    std::vector<Room> rooms;
    rooms.push_back(Room(1, 10));
    rooms.push_back(Room(2, 20));
    rooms.push_back(Room(3, 30));
    rooms.push_back(Room(4, 40));
    rooms.push_back(Room(5, 50));
    House house(rooms);
    std::cout << "Area: " << house.getArea() << std::endl;
    std::cout << "Volume: " << house.getVolume() << std::endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS