Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

using classes within classes

Posted on 2005-04-25
6
Medium Priority
?
264 Views
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
{
public:
            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;}

private:
            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

Cam
0
Comment
Question by:dauler
  • 3
  • 3
6 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13866051
Like so:

   instance.function();

or:

   pointer->function();

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:

   (*_checkIn).input():

or:

   _checkIn->input();


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

Regards;
0
 

Author Comment

by:dauler
ID: 13879289
InteractiveMind,

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
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13879328
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 ;)
Rob.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:dauler
ID: 13879442
InteractiveMind:

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
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13879579
lol :-) Sure.

Best of luck with it.

Regards;
0
 

Author Comment

by:dauler
ID: 13881578
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!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

810 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