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.
|