Create a class RoomOccupancy that can be used to record the number of people in
ID: 3633538 • Letter: C
Question
Create a class RoomOccupancy that can be used to record the number of people in the rooms of a building. The class has the attributesnumberInRooms: an array to record the number of people in rooms
totalNumber: the total number of people in all rooms
The class has the following methods:
addOneToRoom: given a room number, this method adds a person to the room and increases the value of totalNumber.
removeOneFromRoom: given a room number, this removes a person from one room, ensuring that numberInRoom does not go below zero, and decreases the value of totalNumer as needed
getNumber: returns the number of people in the room
getTotal: a static method that returns the total number of people
Write a program that tests the class RoomOccupancy described above: start with 0 person in all of the rooms, add one person to one room, display the total number of people, then remove a person from that room and disply total number again, and last, try to remove a person from the same room again.
Explanation / Answer
This program contains three files. RoomOccupancy.h header file in which class is defined. RoomOccupancy.cpp in which class members are declared. RoomOccupancymain.cpp in which the code for using the class is written. RoomOccupancy.h: #ifndef ROOMOCCUPANCY_H #define ROOMOCCUPANCY_H #include #include using namespace std; class RoomOccupancy { public: //member variables int size; int *a; int totalNumber; int numberofRooms; public: //constructor RoomOccupancy(int); //method to add a person a given room number void addOneToRoom(int); //method to remove a person a given room number void removeOneFromRoom(int); //method to display number of persons in a given room number int getNumber(int); //method to display number of persons in all rooms int getTotal(); }; #endif RoomOccupancy.cpp: #include "stdafx.h" #include "RoomOccupancy.h" RoomOccupancy::RoomOccupancy(int rno) { numberofRooms = rno; a = new int(rno); for (int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.