Question

In: Computer Science

Define the class HotelRoom. The class has the following private data members: the room number (an...

Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler should display the message “Negative Parameter” [20pts]. Include a toString() function that nicely formats and returns a string that displays the information about the hotel room [10pts].

  1. Write a main function to test the class HotelRoom, create a HotelRoom object. Try to set the room rate to an invalid value to generate an exception. Invoke the toSting() function to display the HotelRoom object. [20pts]
  2. Derive the classes GuestRoom form the base class HotelRoom. The GuestRoom has private data fields and public functions:
    1. The private data field capacity (an Integer) that represents the maximum number of guests that can occupy the room. [5pts]
    2. The private data member status (an integer), which represents the number of guests in the room (0 if unoccupied). [5pts]
    3. An integer data field days that represents the number of days the guests occupies the room. [5pts]
    4. Add constructors and get/set functions to the GuestRoom class. The set function for the status data member must throw an out_of_range exception if it tries to set status to value greater than the capacity. [30pts]
    5. The function calculateBill() that returns the amount of guest’s bill. [10pts]
    6. Redefine the function toString() that formats and returns a string containing all pertinent information about the GuestRoom. [15pts]
  3. Derive the classes MeetingRoom form the base class HotelRoom. The class has the following private data filed sand public functions:
    1. A private data field seats, which represents the number of seats in the room. [5pts]
    2. An integer data field status (1 if the room is booked and 0 otherwise). [5pts]
    3. Add constructors and get/set functions to the GuestRoom class. [10pts]
    4. Redefine the function toSting() to format and return a string containing all pertinent information about the MeetingRoom. [20pts]
    5. The function CalculateBill(), which returns the amount of the bill for renting the room for one day. The function calculates the bill as follows: the number of seats multiplied by 10.00, plus 500.00. [20pts]
  4. Write a main function to test the classes GuestRoom and MeetingRoom. Invoke the calculateBills and toStirng() in each of the objects. [40pts]]
  5. Make changes to the HotelRoom class to implement polymorphism. Add a virtual function calculateBill() that returns 0.00 and make the toString() function in the HotelRoom class a virtual function. Write the function displayHotelRoom() that receive a base class type reference as a parameter, then invokes the functions calculateBill() and toString(). The function must return void. [50pts]
  6. From the main function invoke the function displayHotelRoom() three separate times and each time send a HotelRoom, a GuestRoom, and a MeetingRoom type objects. [20pts]

  7. Repeat parts e and f but make appropriate changes in such a way that HotelRoom is turned into an abstract base class. [50pts]

Solutions

Expert Solution

1.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class HotelRoom
{
   private:
   int roomNumber;
   double dailyRate;
  
   public:
   //constructors
   HotelRoom()
   {
       roomNumber = 0;
       dailyRate = 0.0;
   }
  
   HotelRoom(int roomNumber,double dailyRate)
   {
       this->roomNumber = roomNumber;
       this->dailyRate = dailyRate;
      
   }
   //set and get methods
   void setRoomNumber(int roomNumber)
   {
       try
       {
           if(roomNumber < 0)
           throw std::invalid_argument( "Negative parameter" );
               this->roomNumber = roomNumber;
       }catch(invalid_argument& ia) {
       cout << ia.what() << '\n';
       }
   }
   int getRoomNumber()
   {
       return roomNumber;
   }
   void setDailyRate(double dailyRate)
   {
       try
       {
           if(dailyRate < 0)
           throw std::invalid_argument( "Negative parameter" );
               this->dailyRate = dailyRate;
       }catch(invalid_argument& ia) {
       cout << ia.what() << '\n';
       }
   }
   double getDailyRate()
   {
       return dailyRate;
   }
   string toString()
   {
       stringstream ss;
       ss<< "Hotel Room Number : "<<getRoomNumber()<<" Daily Rate : $"<<getDailyRate();
       return ss.str();
   }
};

class GuestRoom : HotelRoom
{
   private:
   int capacity;
   int status;
   int days;
  
   public:
  
   GuestRoom(int roomNumber,double dailyRate,int capacity,int status,int days):HotelRoom(roomNumber,dailyRate)
   {
       this->capacity = capacity;
       this->status = status;
       this->days = days;
   }
   void setCapacity(int capacity)
   {
       this->capacity = capacity;
   }
   int getCapacity()
   {
       return capacity;
   }
   void setStatus(int status)
   {
       try
       {
       if(status > capacity)
       throw out_of_range("out of range");
       this->status = status;
       }catch(out_of_range& e) {
       cout << e.what() << '\n';
       }
   }
   void setDays(int days)
   {
       this->days = days;
   }
   int getDays()
   {
       return days;
   }
   double calculateBill()
   {
       return days* getDailyRate();
   }
   string toString()
   {
       stringstream ss;
       ss<< "Guest Room Number : "<<getRoomNumber()<<" Daily Rate : $"<<getDailyRate()<<" Days : "<<getDays()<<" ";
       return ss.str();
   }
  
};

class MeetingRoom : HotelRoom
{
   private:
   int numOfSeats;
   int status;

  
   public:
   MeetingRoom(int roomNumber,double dailyRate,int numOfSeats,int status):HotelRoom(roomNumber,dailyRate)
   {
       this->numOfSeats = numOfSeats;
       this->status = status;
   }
   void setNumOfSeats(int numOfSeats)
   {
       this->numOfSeats = numOfSeats;
   }
   int getNumOfSeats()
   {
       return numOfSeats;
   }
   void setStatus(int status)
   {
       this->status = status;
   }
   int getStatus()
   {
       return status;
   }
   string toString()
   {
       stringstream ss;
       ss<< "Meeting Room Number : "<<getRoomNumber()<<" Daily Rate : $"<<getDailyRate()<<" Number of seats : "<<getNumOfSeats()<<" Status :"<<getStatus();
       return ss.str();
   }
   double calculateBill()
   {
       return getNumOfSeats()*10.00 +500.00;
   }
};


int main() {
  
   HotelRoom hr1(344,170.99);
   cout<<hr1.toString();
  
   cout<<endl;
  
   GuestRoom gr(405,150.55,100,56,4);
   cout<<gr.toString();
  
   cout<<endl;
  
   MeetingRoom mr(510,300.00,200,1);
   cout<<mr.toString();
   return 0;
}

Output:

Hotel Room Number : 344 Daily Rate : $170.99
Guest Room Number : 405 Daily Rate : $150.55 Days : 4 
Meeting Room Number : 510 Daily Rate : $300 Number of seats  : 200 Status  :1

2.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class HotelRoom
{
   private:
   int roomNumber;
   double dailyRate;
  
   public:
   //constructors
   HotelRoom()
   {
       roomNumber = 0;
       dailyRate = 0.0;
   }
  
   HotelRoom(int roomNumber,double dailyRate)
   {
       this->roomNumber = roomNumber;
       this->dailyRate = dailyRate;
      
   }
   //set and get methods
   void setRoomNumber(int roomNumber)
   {
       try
       {
           if(roomNumber < 0)
           throw std::invalid_argument( "Negative parameter" );
               this->roomNumber = roomNumber;
       }catch(invalid_argument& ia) {
       cout << ia.what() << '\n';
       }
   }
   int getRoomNumber()
   {
       return roomNumber;
   }
   void setDailyRate(double dailyRate)
   {
       try
       {
           if(dailyRate < 0)
           throw std::invalid_argument( "Negative parameter" );
               this->dailyRate = dailyRate;
       }catch(invalid_argument& ia) {
       cout << ia.what() << '\n';
       }
   }
   double getDailyRate()
   {
       return dailyRate;
   }
   virtual string toString()
   {
       stringstream ss;
       ss<< "Hotel Room Number : "<<getRoomNumber()<<" Daily Rate : $"<<getDailyRate();
       return ss.str();
   }
   virtual double calculateBill()
   {
       return 0.0;
   }
};

class GuestRoom : public HotelRoom
{
   private:
   int capacity;
   int status;
   int days;
  
   public:
  
   GuestRoom(int roomNumber,double dailyRate,int capacity,int status,int days):HotelRoom(roomNumber,dailyRate)
   {
       this->capacity = capacity;
       this->status = status;
       this->days = days;
   }
   void setCapacity(int capacity)
   {
       this->capacity = capacity;
   }
   int getCapacity()
   {
       return capacity;
   }
   void setStatus(int status)
   {
       try
       {
       if(status > capacity)
       throw out_of_range("out of range");
       this->status = status;
       }catch(out_of_range& e) {
       cout << e.what() << '\n';
       }
   }
   void setDays(int days)
   {
       this->days = days;
   }
   int getDays()
   {
       return days;
   }
   double calculateBill()
   {
       return days* getDailyRate();
   }
   string toString()
   {
       stringstream ss;
       ss<< "Guest Room Number : "<<getRoomNumber()<<" Daily Rate : $"<<getDailyRate()<<" Days : "<<getDays()<<" ";
       return ss.str();
   }
  
};

class MeetingRoom : public HotelRoom
{
   private:
   int numOfSeats;
   int status;

  
   public:
   MeetingRoom(int roomNumber,double dailyRate,int numOfSeats,int status):HotelRoom(roomNumber,dailyRate)
   {
       this->numOfSeats = numOfSeats;
       this->status = status;
   }
   void setNumOfSeats(int numOfSeats)
   {
       this->numOfSeats = numOfSeats;
   }
   int getNumOfSeats()
   {
       return numOfSeats;
   }
   void setStatus(int status)
   {
       this->status = status;
   }
   int getStatus()
   {
       return status;
   }
   string toString()
   {
       stringstream ss;
       ss<< "Meeting Room Number : "<<getRoomNumber()<<" Daily Rate : $"<<getDailyRate()<<" Number of seats : "<<getNumOfSeats()<<" Status :"<<getStatus();
       return ss.str();
   }
   double calculateBill()
   {
       return getNumOfSeats()*10.00 +500.00;
   }
};

void displayHotelRoom(HotelRoom &room)
{
   cout<<"Bill = $"<<room.calculateBill();
   cout<<endl;
   cout<<room.toString();
}

int main() {
  
   HotelRoom hr(344,170.99);
   displayHotelRoom(hr);
  
   cout<<endl;
  
   GuestRoom gr(405,150.55,100,56,4);
   displayHotelRoom(gr);
  
   cout<<endl;
  
   MeetingRoom mr(510,300.00,200,1);
   displayHotelRoom(mr);
   return 0;
}

Output:

Bill = $0
Hotel Room Number : 344 Daily Rate : $170.99
Bill = $602.2
Guest Room Number : 405 Daily Rate : $150.55 Days : 4 
Bill = $2500
Meeting Room Number : 510 Daily Rate : $300 Number of seats  : 200 Status  :1

Do ask if any doubt. Please upvote.


Related Solutions

Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT