using classes within classes

Posted on 2005-04-25
Last Modified: 2010-05-18
I am trying to develop a hotel reservation program and have constructed classes of guest, date, reservation and room.  For example here is my guest class:

#ifndef _GUEST_H
#define _GUEST_H
#include <string>

class Guest
            Guest(Room guestRoom, string name = "", string address = "",
            date checkIn, date checkOut, unsigned long card = 0): _name(name),
            _address(address), _checkIn(checkIn), _card(card)

            void getGuestInfo() const;

            void setGuestInfo(Room guestRoomstring, string, date,date,
            unsigned long){_name = name, _address = address, _checkIn =
            checkIn, _checkOut = checkOut, _card = card;}

            Room _guestRoom;
            string _name, _address;
            date _checkIn, _checkOut;
            unsigned long _card;

#endif// _GUEST_H

As you can see I have private data members that are of other type classes room and date.  In my function definitions how do I call these class functions?  Can I just for example in my void getGuestInfo function if I were to call one of my date functions can I just do it like this _checkin.input() ?  I am basically asking how do I access functions of classes within a class?  Thanks so much :D

Question by:dauler
    LVL 25

    Accepted Solution

    Like so:




    Let's take an example: You have the class called 'date'. You instantiate it, like so:

       date _checkIn;      // Or, if the date class has a constructor:   date _checkin = new date();

    You can then call it's function like so:

       _checkIn.input();    // For example

    Or, if you decide to use a pointer to the date class, like so:

       date *_checkIn;   // An example!

    You can access it's functions either like this:




    Clear??  If not, let me know and I'll try and re-word it :-)


    Author Comment


    Thank you for the pointer pun intended.  I now have an array of problems with this program.  Firstly in my Guest class I am trying to store the guest's name.  I think, though I cannot return a string in my accessor function.  I have for that :
    string name() { return _name;} where _name is my private member variable.  How do I resolve this?  As far as the pointer do I need to * all references to my Date variables or just in the private member declaration?  My previous class programs were so much easier with just integers this complex class program is a bit of a big leap for me.  Thank you so much for all your help : D
    LVL 25

    Expert Comment

    Firstly, thank you for points, and the grade 'A'.

    > this complex class program is a bit of a big leap for me.
    Trust me, it gets boring when it involves no thought. :-)

    Can I ask to see the code which you have so far? And a quick summary of what it does exactly?

    Cheers ;)

    Author Comment


    Scratch that last question I just figured out I can indeed return string values.  I'm sure I'll be bothering you again soon.

    Thanks again
    LVL 25

    Expert Comment

    lol :-) Sure.

    Best of luck with it.


    Author Comment

    Hi InteractiveMind again,

    I told you I'd be back to bother you.  Well I have a bunch of classes declared for my reservation program but now I am having trouble putting it all together.  I want to create an array with my room class of say 100 rooms that are categorized by number of beds and smoking and non smoking.  My guest class contains the interface to gather info from the prosepctive guest.  I wish to have the information stored in a dynamic array of guests and want the room array to decrease as rooms fill up.  I have no idea how to do this on this scale.  My array expertise is pretty weak.  Any simple solution will do.  This is not a real world application so perfection is far from necessary.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    A short article about problems I had with the new location API and permissions in Marshmallow
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now