Question

C++ Define the class HotelRoom. The class has the following private data members: the room number...

C++

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 [3pts]. The constructors and the 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” [3pts]. Include a toString() function that nicely formats and returns a string that displays the information ( room number and rate) about the HotelRoom object. [2pts].

  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. [2pts]
  2. Derive the classes GuestRoom from the base class HotelRoom. The GuestRoom has private data fields and public functions as follows:
    1. The private data field capacity (an Integer) that represents the maximum number of guests that can occupy the room. [1pts]
    2. The private data member status (an integer), which represents the number of guests in the room (0 if unoccupied). [1pts]
    3. An integer data field days that represents the number of days the guests occupy the room. [1pts]
    4. Add constructors and get/set functions to the GuestRoom class. The set function for the status data field must throw an out_of_range exception if it tries to set status to value greater than the capacity. [3pts]
    5. The function calculateBill() that returns the amount of guest’s bill. [2pts]
    6. Redefine the function toString() that formats and returns a string containing all pertinent information about the GuestRoom. [2pts]
  3. Derive the classes MeetingRoom from the base class HotelRoom. The class has the following private data fields and public functions:
    1. A private data field seats, which represents the number of seats in the room. [1pts]
    2. An integer data field status (1 if the room is booked and 0 otherwise). [1pts]
    3. Add constructors and get/set functions to the MeetingRoom class. [1pts]
    4. Redefine the function toSting() to format and return a string containing all pertinent information about the MeetingRoom. [2pts]
    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. [2pts]
  4. Write a main function to test the classes GuestRoom and MeetingRoom. Invoke the calculateBills and toStirng() in each of the objects. [4pts]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

#include <iostream>

using namespace std;

// HotelRoom class

class HotelRoom{

// private data members

private:

// int room number and double rate

int roomNumber;

double rate;

// public constructors,getters and setters

public:

// default constructor

HotelRoom(){

roomNumber=0;

rate=0;

}

// constructor with parameters

HotelRoom(int roomNumber,double rate){

// string exception for negative parameter

string invalid_argument="Negative Parameter";

// if either room number or rate parameter is negative then throw exception

if(roomNumber<0)

throw invalid_argument;

if(rate<0)

throw invalid_argument;

// set arguments to the private data members

this->roomNumber=roomNumber;

this->rate=rate;

}

// getters for data members

int getRoomNumber(){

return this->roomNumber;

}

double getRate(){

return this->rate;

}

// setters for data members

void setRoomNumber(int roomNumber){

// if room number argument is negative throw exception

string invalid_argument="Negative Parameter";

if(roomNumber<0)

throw invalid_argument;

// else set room member data member

this->roomNumber=roomNumber;

}

void setRate(double rate){

// if rate is negative throw exception

string invalid_argument="Negative Parameter";

if(rate<0)

throw invalid_argument;

// else set rate data member

this->rate=rate;

}

// toString method

// returns string containing details of HotelRoom object

string toString(){

// return string formation

string retString="Rate for room number ";

retString.append(to_string(this->roomNumber));

string str2=" is ";

str2.append(to_string(this->rate));

retString.append(str2);

// return the string formed

return retString;

}

};

// main function

int main() {

// create a HotelRoom object

HotelRoom room;

// inside try catch

try{

// initialize HotelRoom object with negative numbers

room=HotelRoom(-1,34);

}catch(string exception){

// print the exception raised

cout<<exception<<endl;

}

// print HotelRoom object

cout<<room.toString()<<endl;

}

#include <iostream>
using namespace std;
// HotelRoom class
class HotelRoom{
  // private data members
  private:
  // int room number and double rate
  int roomNumber;
  double rate;
  // public constructors,getters and setters
  public:
  // default constructor
  HotelRoom(){
    roomNumber=0;
    rate=0;
  }
  // constructor with parameters
  HotelRoom(int roomNumber,double rate){
    // string exception for negative parameter
    string invalid_argument="Negative Parameter";
    // if either room number or rate parameter is negative then throw exception
    if(roomNumber<0)
      throw invalid_argument;
    if(rate<0)
      throw invalid_argument;
    // set arguments to the private data members
    this->roomNumber=roomNumber;
    this->rate=rate;
  }
  // getters for data members
  int getRoomNumber(){
    return this->roomNumber;
  }
  double getRate(){
    return this->rate;
  }
  // setters for data members
  void setRoomNumber(int roomNumber){
    // if room number argument is negative throw exception
    string invalid_argument="Negative Parameter";
    if(roomNumber<0)
      throw invalid_argument;
    // else set room member data member
    this->roomNumber=roomNumber;
  }
  void setRate(double rate){
    // if rate is negative throw exception
    string invalid_argument="Negative Parameter";
    if(rate<0)
      throw invalid_argument;
    // else set rate data member
    this->rate=rate;
  }
  // toString method 
  // returns string containing details of HotelRoom object
  string toString(){
    // return string formation
    string retString="Rate for room number ";
    retString.append(to_string(this->roomNumber));
    string str2=" is ";
    str2.append(to_string(this->rate));
    retString.append(str2);
    // return the string formed
    return retString;
  }
};
// main function
int main() {
  // create a HotelRoom object
  HotelRoom room;
  // inside try catch
  try{
    // initialize HotelRoom object with negative numbers
    room=HotelRoom(-1,34);
  }catch(string exception){
    // print the exception raised
    cout<<exception<<endl;
  }
  // print HotelRoom object
  cout<<room.toString()<<endl;
}

Negative Parameter Rate for room number 0 is 0.000000

b,c,d)

#include <iostream>

using namespace std;

// HotelRoom class

class HotelRoom{

// private data members

private:

// int room number and double rate

int roomNumber;

double rate;

// public constructors,getters and setters

public:

// default constructor

HotelRoom(){

roomNumber=0;

rate=0;

}

// constructor with parameters

HotelRoom(int roomNumber,double rate){

// string exception for negative parameter

string invalid_argument="Negative Parameter";

// if either room number or rate parameter is negative then throw exception

if(roomNumber<0)

throw invalid_argument;

if(rate<0)

throw invalid_argument;

// set arguments to the private data members

this->roomNumber=roomNumber;

this->rate=rate;

}

// getters for data members

int getRoomNumber(){

return this->roomNumber;

}

double getRate(){

return this->rate;

}

// setters for data members

void setRoomNumber(int roomNumber){

// if room number argument is negative throw exception

string invalid_argument="Negative Parameter";

if(roomNumber<0)

throw invalid_argument;

// else set room member data member

this->roomNumber=roomNumber;

}

void setRate(double rate){

// if rate is negative throw exception

string invalid_argument="Negative Parameter";

if(rate<0)

throw invalid_argument;

// else set rate data member

this->rate=rate;

}

// toString method

// returns string containing details of HotelRoom object

string toString(){

// return string formation

string retString="Rate for room number ";

retString.append(to_string(this->roomNumber));

string str2=" is ";

str2.append(to_string(this->rate));

retString.append(str2);

// return the string formed

return retString;

}

};

// GuestRoom class deriving from HotelRoom

// all public parts of HotelRoom going to public portion of GuestRoom

class GuestRoom:public HotelRoom{

// private members of guestRoom is room capacity,status which contains number of occupants and

// days denoting number of days for which the room is booked

private:

int capacity;

int status;

int days;

// public constructors

public:

// default

GuestRoom(){

this->capacity=0;

this->status=0;

this->days=0;

}

// call to setters functions with appropriate parameters

GuestRoom(int capacity,int status,int days){

this->setCapacity(capacity);

this->setStatus(status);

this->setDays(days);

}

// setters

void setCapacity(int capacity){

this->capacity=capacity;

}

void setStatus(int status){

// thorw out_of_range exception if number of guests is greater than capacity of room

string out_of_range="Number of guests out of capacity of the room";

if(status>this->capacity)

throw out_of_range;

this->status=status;

}

void setDays(int days){

this->days=days;

}

// getters

int getCapacity(){

return this->capacity;

}

int getStatus(){

return this->status;

}

int getDays(){

return this->days;

}

// calculate Bill function which returns product of room rate and number of days occupied

double calculateBill(){

return this->getRate()*this->getDays();

}

// toString function which returns a string containing all the details of the gurst room

string toString(){

string str1="Guest room number ";

str1.append(to_string(this->getRoomNumber()));

string str11=" capacity is ";

str11.append(to_string(this->capacity));

string str2=" ,currently having ";

str2.append(to_string(this->status));

string str3=" occupants for ";

str3.append(to_string(this->days));

str3.append(" days at rate per day of ");

str3.append(to_string(this->getRate()));

str2.append(str3);

str11.append(str2);

str1.append(str11);

return str1;

}

};

// MeetingRoom class derived from HotelRoom where all public members of HotelRoom goes to public part of MeetingRoom.

class MeetingRoom:public HotelRoom{

// private data members

private:

// number of seats in meeting room

int seats;

// status of room

// 0 if unoccupied,1 if occupied

int status;

// public constructors

public:

// default

MeetingRoom(){

this->seats=0;

this->status=0;

}

// call to setters appropriatly for paramaterized constructors

MeetingRoom(int seats,int status){

this->setSeats(seats);

this->setStatus(status);

}

// setters

void setSeats(int seats){

this->seats=seats;

}

void setStatus(int status){

this->status=status;

}

// getters

int getSeats(){

return this->seats;

}

int getStatus(){

return this->status;

}

// calculate bill function which return product of number of seats and 10 added with 500.

double calculateBill(){

return this->getSeats()*10+500;

}

// toString function which returns string containing all the relevant details of the meeting room

string toString(){

string str1="Meeting room number ";

str1.append(to_string(this->getRoomNumber()));

string str11= " ,having ";

str11.append(to_string(this->seats));

string str2=" seats is ";

// check for status of the room

if(this->status)

str2.append("booked.");

else

str2.append("not booked.");

str11.append(str2);

str1.append(str11);

return str1;

}

};

// main function

int main() {

// create a Guest Room and Meeting room objects

GuestRoom groom;

MeetingRoom mroom;

// inside try catch

try{

// call to relevant setters for both GuestRoom and MeetingRoom objects

groom.setCapacity(10);

groom.setDays(5);

groom.setRate(500);

groom.setRoomNumber(101);

groom.setStatus(5);

mroom.setSeats(300);

mroom.setStatus(1);

mroom.setRoomNumber(303);

// call to toString and calculateBill methods for both GuestRoom and MeetingRoom objects

cout<<groom.toString()<<endl;

cout<<groom.getRoomNumber()<<" room number's bill is "<<groom.calculateBill()<<endl;

cout<<mroom.toString()<<endl;

cout<<mroom.getRoomNumber()<<" room number's bill is "<<mroom.calculateBill()<<endl;


}catch(string exception){

// print the exception raised

cout<<exception<<endl;

}

}

#include <iostream>
using namespace std;
// HotelRoom class
class HotelRoom{
  // private data members
  private:
  // int room number and double rate
  int roomNumber;
  double rate;
  // public constructors,getters and setters
  public:
  // default constructor
  HotelRoom(){
    roomNumber=0;
    rate=0;
  }
  // constructor with parameters
  HotelRoom(int roomNumber,double rate){
    // string exception for negative parameter
    string invalid_argument="Negative Parameter";
    // if either room number or rate parameter is negative then throw exception
    if(roomNumber<0)
      throw invalid_argument;
    if(rate<0)
      throw invalid_argument;
    // set arguments to the private data members
    this->roomNumber=roomNumber;
    this->rate=rate;
  }
  // getters for data members
  int getRoomNumber(){
    return this->roomNumber;
  }
  double getRate(){
    return this->rate;
  }
  // setters for data members
  void setRoomNumber(int roomNumber){
    // if room number argument is negative throw exception
    string invalid_argument="Negative Parameter";
    if(roomNumber<0)
      throw invalid_argument;
    // else set room member data member
    this->roomNumber=roomNumber;
  }
  void setRate(double rate){
    // if rate is negative throw exception
    string invalid_argument="Negative Parameter";
    if(rate<0)
      throw invalid_argument;
    // else set rate data member
    this->rate=rate;
  }
  // toString method 
  // returns string containing details of HotelRoom object
  string toString(){
    // return string formation
    string retString="Rate for room number ";
    retString.append(to_string(this->roomNumber));
    string str2=" is ";
    str2.append(to_string(this->rate));
    retString.append(str2);
    // return the string formed
    return retString;
  }
};
// GuestRoom class deriving from HotelRoom
// all public parts of HotelRoom going to public portion of GuestRoom
class GuestRoom:public HotelRoom{
  // private members of guestRoom is room capacity,status which contains number of occupants and 
  // days denoting number of days for which the room is booked
  private:
  int capacity;
  int status;
  int days;
  // public constructors
  public:
  // default 
  GuestRoom(){
    this->capacity=0;
    this->status=0;
    this->days=0;
  }
  // call to setters functions with appropriate parameters
  GuestRoom(int capacity,int status,int days){
    this->setCapacity(capacity);
    this->setStatus(status);
    this->setDays(days);
  }
  // setters
  void setCapacity(int capacity){
    this->capacity=capacity;
  }
  void setStatus(int status){
    // thorw out_of_range exception if number of guests is greater than capacity of room
    string out_of_range="Number of guests out of capacity of the room";
    if(status>this->capacity)
      throw out_of_range;
    this->status=status;
  }
  void setDays(int days){
    this->days=days;
  }
  // getters 
  int getCapacity(){
    return this->capacity;
  }
  int getStatus(){
    return this->status;
  }
  int getDays(){
    return this->days;
  }
  // calculate Bill function which returns product of room rate and number of days occupied
  double  calculateBill(){
    return this->getRate()*this->getDays();
  }
  // toString function which returns a string containing all the details of the gurst room
  string toString(){
    string str1="Guest room number ";
    str1.append(to_string(this->getRoomNumber()));
    string str11=" capacity is ";
    str11.append(to_string(this->capacity));
    string str2=" ,currently having ";
    str2.append(to_string(this->status));
    string str3=" occupants for ";
    str3.append(to_string(this->days));
    str3.append(" days at rate per day of ");
    str3.append(to_string(this->getRate()));
    str2.append(str3);
    str11.append(str2);
    str1.append(str11);
    return str1;
  }

};
// MeetingRoom class derived from HotelRoom where all public members of HotelRoom goes to public part of MeetingRoom.
class MeetingRoom:public HotelRoom{
  // private data members
  private:
  // number of seats in meeting room
  int seats;
  // status of room
  // 0 if unoccupied,1 if occupied
  int status;
  // public constructors
  public:
  // default
  MeetingRoom(){
    this->seats=0;
    this->status=0;
  }
  // call to setters appropriatly for paramaterized constructors
  MeetingRoom(int seats,int status){
    this->setSeats(seats);
    this->setStatus(status);
  }
  // setters
  void setSeats(int seats){
    this->seats=seats;
  }
  void setStatus(int status){
    this->status=status;
  }
  // getters
  int getSeats(){
    return this->seats;
  }
  int getStatus(){
    return this->status;
  }
  // calculate bill function which return product of number of seats and 10 added with 500.
  double  calculateBill(){
    return this->getSeats()*10+500;
  }
  // toString function which returns string containing all the relevant details of the meeting room
  string toString(){
    string str1="Meeting room number ";
    str1.append(to_string(this->getRoomNumber()));
    string str11= " ,having ";
    str11.append(to_string(this->seats));
    string str2=" seats is ";
    // check for status of the room
    if(this->status)
      str2.append("booked.");
    else
      str2.append("not booked.");
    
    str11.append(str2);
    str1.append(str11);
    return str1;
  }

};
// main function
int main() {
  // create a Guest Room and Meeting room  objects
  GuestRoom groom;
  MeetingRoom mroom;
  // inside try catch
  try{
    // call to relevant setters for both GuestRoom and MeetingRoom objects
    groom.setCapacity(10);
    groom.setDays(5);
    groom.setRate(500);
    groom.setRoomNumber(101);
    groom.setStatus(5);

    mroom.setSeats(300);
    mroom.setStatus(1);
    mroom.setRoomNumber(303);

    // call to toString and calculateBill methods for both GuestRoom and MeetingRoom objects
    cout<<groom.toString()<<endl;
    cout<<groom.getRoomNumber()<<" room number's bill is "<<groom.calculateBill()<<endl;
    cout<<mroom.toString()<<endl;
    cout<<mroom.getRoomNumber()<<" room number's bill is "<<mroom.calculateBill()<<endl; 


  }catch(string exception){
    // print the exception raised
    cout<<exception<<endl;
  }
}

Guest room number 101 capacity is 10 currently having 5 occupants for 5 days at rate per day of 500.000000 101 room numbers

Add a comment
Know the answer?
Add Answer to:
C++ Define the class HotelRoom. The class has the following private data members: the room number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me to write a c++ program that will Implement a HotelRoom class, with private...

    Please help me to write a c++ program that will Implement a HotelRoom class, with private data members: the room number, room capacity (representing the maximum number of people the room can accommodate), the occupancy status (0 or the number of occupants in the room), the daily room rate. Member functions include: • a 4-argument constructor that initializes the four data members of the object being created (room number, room capacity, room rate, occupancy status) to the constructor's arguments. The...

  • in c++ Define and implement the class Employee with the following requirements: private data members string...

    in c++ Define and implement the class Employee with the following requirements: private data members string type name a. b. double type hourlyRate 2. public member functions a. default constructor that sets the data member name to blank"and hourlyRate to zero b. A constructor with parameters to initialize the private data members c. Set and get methods for all private data members d. A constant method weeklyPay() that receives a parameter representing the number of hours the employee worked per...

  • C++ design a class named Technician that contains private data members to store the following: -technician's...

    C++ design a class named Technician that contains private data members to store the following: -technician's name (a string) - number of service calls - total time of all service calls - average service call time the technician class should also have the following public member functions: - a constructor function that initializes all data members by obtaining their values from the users. with the exception of the average service call time which will be calculated as: total_time/number_of_call - a...

  • Create a class named Cash. Include one private integer variable to hold the number of dollars...

    Create a class named Cash. Include one private integer variable to hold the number of dollars and one to hold the number of cents. Create your own accessor and mutator functions to read and set both member variables. Create a function that returns the amount of money as a double. Test all of your functions. Use at least 4 Cash objects. Must be done in c++

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named month...

    C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named monthNumber that holds the number of the month (January is 1, February is 2, etc.). Also provide the following functions: • A default constructor that sets the monthNumber field to 1. • A constructor that accepts the number of month as an argument and sets...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • Create an Inventory class with data members for stock number, quantity, and price, and overloaded data...

    Create an Inventory class with data members for stock number, quantity, and price, and overloaded data entry and output operators. The data entry operator function should throw: - An error message, if the stock number is negative or higher than 999 - The quantity, if it is less than 0 - The price, if it is over $100.00 Then perform the following tasks: a. Write a main() function that instantiates an array of five Inventory objects, and accepts data for...

  • Overloading Design a UML diagram for a TimeClock class with the following private data members: •...

    Overloading Design a UML diagram for a TimeClock class with the following private data members: • float days • float hours The class should also have the following public member methods: • Constructor with a default that sets the data values to 0 • getDays to return the private data member days • getHours to return the private data member hours • setDays to set the private data member days • setHours to set the private data member hours •...

  • he class definition for a Hank Account class, contains the following private data nembers: the name,...

    he class definition for a Hank Account class, contains the following private data nembers: the name, account number (both are character arrays of 30 elements each) alance, and interest rate Cbolth are double). It also have the following public member unctions Bank AccountO: This function is the default constructor. deposito: subsequently adjusts the balance. (balance- balance + amt) withdrawo: This function is passed an amount to withdraw and subsequently adjusts the balance. (balance balance- amt). cale interestO: This function calculates...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT