By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

need help with vecotrs and other thing please!

P: 7
Hi, just new to this forum, and i was wondering if anyone could help me.

I've got a piece of work to do, i have done the majority of it, just the last, and the most important thing lol, that i am stuck with.

there are 4 classes, Room, Bedroom (type of room), ConferenceRoom (type of room) and Date(in a header file).

Expand|Select|Wrap|Line Numbers
  1. class Room{
  2.                 private:
  3.                     int roomNumber;
  4.                     double rate;
  5.                     string customer;
  6.                     Date booking;
  7.  
  8.                 public:
  9.                     Room (int, double);
  10.                     void bookRoom (string, Date);
  11.                     Date getBooking() {return booking;}
  12.                     double getRate();
  13.                     int getRoomNumber();
  14.                     string getDetails();
  15.  
  16.            };
  17.  
  18. class Bedroom{
  19.       friend class Room;
  20.  
  21.                 private:
  22.                     string facilities;
  23.  
  24.                 public:
  25.                     Bedroom (int, double, string);
  26.                     string getDetails;
  27.              };
  28.  
  29. class ConferenceRoom{
  30.       friend class Room;
  31.  
  32.                 private:
  33.                     int seatingCapacity;
  34.  
  35.                 public:
  36.                     ConferenceRoom (int, double, int);
  37.                     int getCapacity;
  38.                     string getDetails();
  39.                     };
Basically, i have got to make a program which:

takes info about a hotel and its rooms from a text file (DONE)

put that info into a vector name Rooms (Done, sort of)

i then need to be able to print info about the rooms using the getDetails member functions from the three room classes.

i need to be able to book a room by getting the user to input the date they require and there name.

need to be able to show all vacant rooms for a inputted date.

need to be able to show the hotels income for a certain date by adding up the price from what hotels are booked for a certain date,
Expand|Select|Wrap|Line Numbers
  1. vector <Room> Hotel;
Now the bit i am stuck on is getting info from the vector which is declared globally into the classes so that i can print relevant info.

Also, is there any way i can hold objects of type Bedroom and ConferenceRoom inside the vector for Rooms?

it seems a lot but once i grasp the vecotr situation i think it will be easier!

i cant find any good tutorials with vecotrs that are of type that is user defined!

Its just that i cant find any way of doing what i am supposed to do!!

Any help much appreciated!!

thanks

Shaun
Aug 12 '07 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You said ti yourself:
there are 4 classes, Room, Bedroom (type of room), ConferenceRoom (type of room) and
A Room is a base class
A Bedroom derives from Room
A ConferecneRoom derives from Room

You have a vector<Room>.

All of your functions have Room* or Room& arguments.

You create a Bedroom object and add it to the vector. It's Room isn't it??
Ditto for ConferenceRoom.

That's enough hints for now.
Aug 14 '07 #2

P: 7
hi, much appreciated for replying!

I was sort of going down that sort of path last night, but your comment has just reassured me! thank you!

But i am having trouble getting it to work now!

i have declared the vector:

Expand|Select|Wrap|Line Numbers
  1. vector <Room*> hotel;
then i read in my values to create a Bedroom object:

Expand|Select|Wrap|Line Numbers
  1. hoteldetails >> h_rNumber;
  2.         hoteldetails >> h_rate;
  3.         getline(hoteldetails, h_facilities);
  4.  
  5.         Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
  6.  
  7.         hotel.push_back(nwbed);
and then i get an error on line 58:

58: no matching function for call to `Room::Room()'
note: candidates are: Room::Room(const Room&)
note: Room::Room(int, double)

the code on line 58 is:

Expand|Select|Wrap|Line Numbers
  1. 57: Bedroom::Bedroom(int roomNumber, double rate, string facilities)
  2. 58: {
  3. 59:     roomNumber = 0;
  4. 60:     rate = 0.0;
  5. 62:     facilities = " ";
  6. 63: }
and i cant work out where i am going wrong, please help!!!

Thank you again!!

Shaun B
Aug 15 '07 #3

RRick
Expert 100+
P: 463
Bedroom is derived from Room. The Bedroom constructor also needs to set the Room's constructor values. Your Bedroom doesn't set any constructor for Room, so the compiler looks for the default constructor for Room. It couldn't find one, so it gave you an error message.

The Base class is set by the Derived class using the ':'. For example:
Expand|Select|Wrap|Line Numbers
  1. class Derived( int ival, double dval): Base( ival)
  2. {
  3.      blah blah blah
  4. }
  5.  
In this case, Derived is calling the Base( int) constructor. If one doesn't exist, the compiler will let you know.
Aug 15 '07 #4

P: 7
Hi, once again, many thanks for the replys, i have now got the program adding the objects to the vector, i ahve just tried printing it out and it is outputting the actual memory addresses, does anyone have any ideas?

Code:

Expand|Select|Wrap|Line Numbers
  1. Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
  2.  
  3.         hotel.push_back(nwbed);
  4.  
  5.         for (int p = 0; p < hotel.size(); p++)
  6.         {
  7.             cout << hotel[p] << endl;
  8.         }
Thanks!!!

Shaun B
Aug 15 '07 #5

RRick
Expert 100+
P: 463
This is printing out what you ask it to do. hotel[p] returns a pointer to Room and cout prints that.

To get cout to print out the values of Room, you'll have to dereference the pointer and have a method return something that cout can print. Usually this is a string. In fact, in java, there is a special virtual method called "toString" that does this.

You can define a toString method in Room and have it print out Room info, and then override it for the other rooms. You can also have the derived classes use Room's toString to make their own. The declaration and use looks like:
Expand|Select|Wrap|Line Numbers
  1. std::string  Room::toString( const std::string & indent="")
  2. .........
  3. cout << hotel[p]->toString( "    ") << endl;
  4.  
The indent variable is a "cool" feature that allows the caller to specify a shift for you info. This makes the output more readable. Inside the toString method, make sure indent is the first thing printed for each new line.
Aug 16 '07 #6

P: 7
hi, thanks for you help, im going to try that tonight!!

Another question though im afraid, really sorry!

Once i have put all the data into the vecotr (all the bedrooms and conference rooms)

is there any way that i will just be able to print out the Bedrooms from the vector?

And, also, is there any way i can say, just print out a Bedroom that has the room number, for example 200. can you search individual elements of the object in the vector like that?

Thanks

Shaun B
Aug 16 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.