Sign in to follow this  
Followers 0
JohnOne

OO entry

7 posts in this topic

So this is my big explosion into the world of OO in C++.

If someone would be so kind as to suggest what exactly I need

focus on to get to my next goal I'd be mighty obliged.

class Room
{
private:

bool occupied;
bool riverveiw;
bool ensuite;

public:

Room () : occupied(false), riverveiw(false), ensuite(true)
{

}

bool IsEnsuite() {

return ensuite;
}
};


class Guest
{
private:

bool VIP;
double totalbill;

public:

Guest ()
{

}
};


class BBHouse
{
private:

public:

Room room1;
Room room2;
Room room3;
Room room4;

BBHouse ()
{

}
};


int main () {

BBHouse BB1;
if (BB1.room1.IsEnsuite()) {

std::cout << "Ensuite" << "\n";
}

std::cin.get();
}
Sorry, tags stripping leading WS.

I have a Guest class there as you see, but guests come and go, so what is the

procedure called for dynamically adding a guest to a room, and of course removing too.

I thin what I have here is called composition, and I have a sneaky feeling that aggregation

is what I need, but I'm unsure how to combine the two.

Any hint / tips / angry confrontations welcome?


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

e: misread post, sec

i would just use any stl container (list/vec probably), and encapsulate + get/add/remove methods, which also handle checkout bills and such

Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I think with one class you have neither composition nor aggregation. When you get into inheritance and the like then you can then start talking in those terms. (I'm no expert so I may very well be wrong.)

Edit: Good start though. You've come a long way in your programming knowledge.

EDIT: One facepalm after another. I just expanded your post to see more than one class. DAMMIT!

Edited by LaCastiglione

Share this post


Link to post
Share on other sites

Cheers.

Getting a little scared looking at stl containers though.

Is this my only path?

You shouldn't be - theyre there to help you. And, no it isn't. but im pretty sure it's the easiest - you dont wanna handle dynamic memory yourself, unless it's absolutely necessary.

Firstly, you choose a container - a list, map or vector would be ideal for this type of job. Any would do, but here's a quick example using a map:

#include <map>
#include <string>


int main() {
    ...

    hotel.AddGuest("john");

    hotel.RemoveGuest("john");

    ...
}

class BBHouse
{

    ....

    std::map<std::string, Guest> guests; // maps provide an easy interface to look up an guest using a string like an array.
    //consider adding rooms as a map/list or something too, so you can have an expandable hotel. seems nice

public: void AddGuest(string name, int room = -1) {
        //this assumes a design where this class autofits a guest into a room, unless a specific room number is given
        // snip: this is the room our algorithm gives us. maybe add errorchecking here if house is full
        // not quite sure how you want room & guest relationship - but use references so rooms and guests have relationships
        Room & assigned_room = get_first_avaible_room();

        Guest new_guest;
        //new_quest.name = name; optional: see further down
        guests[name] = guest;

        //should room be selfaware?
        assigned_room.add(guest);
    }

public: void RemoveGuest(string name) {
        //do billing here

        //now erase entry
        guests.erase(guests.find(name));

    }



private: Room & get_first_avaible_room(); // returns a reference to a room, that is empty

    ....

}


class Guest {

    ...
    //this is optional, but it might be nice to have the guest name stored
    public: string name;

    ...

}
1 person likes this

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0