It sometimes happens at a hotel that a guest wants to be transferred from one ro
ID: 3681859 • Letter: I
Question
It sometimes happens at a hotel that a guest wants to be transferred from one room to another. In the HotelRoom class, add a friend function Transfer() that transfers a guest from one room to another. The function should have two arguments. The first argument should be the number of the room you are transferring the guest from; the second argument should be the room number you are transferring the guest to. The function should make appropriate changes to the occupancy status of both rooms . Code a main() to test the new friend function
Explanation / Answer
Please find the required code and output below :
#include <iostream>
using namespace std;
class HotelRoom
{
int room101=5;
int room202=3;
public:
friend void Transfer(HotelRoom hr, int fromRoom, int toRoom);
int getRoom101Count(){
return room101;
}
int getRoom202Count(){
return room202;
}
};
void Transfer(HotelRoom hr,int from, int to)
{
if(from==101)
hr.room101--;
else if(from==202)
hr.room202--;
if(to==101)
hr.room101++;
else if(to==202)
hr.room202++;
cout << "Capacity after transfer: room101 = " << hr.room101 << "; room202 = " << hr.room202 <<endl;
}
// Main function for the program
int main( )
{
HotelRoom hr;
cout << "Capacity before transfer: room101 = " << hr.getRoom101Count() << "; room202 = " << hr.getRoom202Count() <<endl;
Transfer(hr,101,202);
return 0;
}
-----------------------------------------------------------------
OUTPUT :
Capacity before transfer: room101 = 5; room202 = 3
Capacity after transfer: room101 = 4; room202 = 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.