0

I wrote this code and having trouble with passing a three argument constructor that set the first to room number, second to room capacity, Here is where I'm stuck the three part is to set the room rate, and that sets the room occupancy to 0 here is the code that I wrote

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>


using namespace std;

class HotelRoom
{
private:
string room_no;
int Capacity;
int occupancy;
double rate;
public:
HotelRoom();
HotelRoom(string, int, int, double );

};  
HotelRoom::HotelRoom (string room, int cap, int occup = 0, double rt )
{
room_no = room;
Capacity = cap;
occupancy = occup;
rate = rt;

cout << " Room number is " << room_no << endl;
cout << " Room Capacity is " << Capacity << endl;
cout << " Room rate is " << rate << endl;
cout << "Occupancy is " << occup << endl;
}

int _tmain(int argc, _TCHAR* argv[])

{

cout << setprecision(2)
     << setiosflags(ios::fixed)
     << setiosflags(ios::showpoint);

HotelRoom Guest (" 123", 4, 55.13);


system("pause");
return 0;
}
0

3 Answers 3

4
HotelRoom::HotelRoom (string room, int cap, int occup = 0, double rt )

is illegal, if you provide default values they must either

  • Have no arguments afterwards
  • all arguments following must also have a default value

To work around this make your occup the last variable in your consructor.

HotelRoom::HotelRoom (string room, int cap, double rt, int occup=0 )

Another note:

Only provide the default value in your header, rewriting the default value will give you an error during declaration.

header.h

HotelRoom(string,int,double,int occup=0);

imp.cpp

HotelRoom::HotelRoom (string room, int cap, double rt, int occup ) 
{
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

2

In addition to Gmercer015's answer. In C++11 you can use delegating constructors.

HotelRoom::HotelRoom (string room, int cap, int occup, double rt )
{
    ...
}

HotelRoom::HotelRoom (string room, int cap, double rt )
    : HotelRoom(room, cap, 0, rt)
{}

Comments

1

AFAIR, parameters with default values cannot be followed by parameters without. In your case you should have rate precede occupancy of the latter can have a default value while the former cannot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.