Write a program in which you have to find the 'volume and Area of 5 Rooms' by using class and objects
#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;
}
Comments
Leave a comment