In structure composition, you just include one structure in the other. For classes, use the methods of the included class to do work on the objects of that class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
class sillyint {
private:
int x;
public:
sillyint(): x( 0 ) { }
sillyint( int x ): x( x ) { }
friend ostream & operator << ( ostream &, sillyint & );
};
ostream & operator << ( ostream & outs, sillyint & si ) {
outs << si.x;
return outs;
}
class sillyint_list {
private:
vector<sillyint> v;
public:
sillyint_list() { }
sillyint_list &add( int i ) { v.push_back( i ); return *this; }
friend ostream & operator << ( ostream &, sillyint_list & );
};
ostream & operator << ( ostream & outs, sillyint_list & ls ) {
outs << '(';
for (int i = 0; i < ls.v.size() -1; i++) outs << ls.v[ i ] << ',';
if (ls.v.size() > 0) outs << ls.v.back();
outs << ')';
return outs;
}
|
So I've created a sillyint object (which is an int that you can't do anything to except print it) and a list of them.
1 2 3 4 5
|
int main() {
sillyint_list ints;
ints.add( 1 ).add( 2 ).add( 3 ).add( 4 ).add( 5 );
cout << ints << endl;
}
|
Produces:
(1,2,3,4,5)
The thing to notice is how one object just had a variable of the other object type. Then I used the other object's defined interface to do stuff to it (print it).
That should get you started.
Oh, before I forget: from the information you've given me and some context based on your data type, I think that the linked list should be a list of 'Appointment's.
I could be wrong, however. It could be that an 'Appointment' stores a list (like my sillyint_list stores a list of sillyints)... but that makes little sense to my mind...
If you are allowed to use the STL, the std::list is a linked list. (std::vector is an array.)
heh :-)