Jump to content

OO entry


JohnOne
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
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;

    ...

}

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...