Linked Lists

I'm having some difficulty figuring out how to get a program that I am working on for a class that I'm taking to work. First off, we are to create an appointment class that will store the date, time, and description of an appointment. We are to be able to add, delete, and then display (print to a text file) the results of the appointments typed in. I'm not exactly sure of how to get the information that the user inputs into the linked lists so that it stores all appointments which include (time, date, and description) as I was saying.

Appointment.cpp file
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
#include "Appointment.h"

#include <iostream>
using namespace std;

#include <string>
using std::getline;

Appointment::Appointment()
{
}

Appointment::~Appointment()
{
}

ostream &operator<<(ostream &output, Appointment &appoint)
{
	output << appoint.appointmentDesc;
	
	return output;
	
}


istream &operator>>(istream &input, Appointment &appoint)
{
	getline(cin, appoint.appointmentDesc);
}


Appointment.h file
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
#ifndef APPOINTMENT_H_
#define APPOINTMENT_H_

#include <iostream>
using std::istream;
using std::ostream;

#include <string>
using std::string;

class Appointment
{
public:
	Appointment();
		
	virtual ~Appointment();
	
	friend ostream &operator<<( ostream &, Appointment &);
	friend istream &operator>>( istream &, Appointment &);
	
private:
	string appointmentDesc;
	
};

#endif /*APPOINTMENT_H_*/ 


Instead of using Inheritance with our Date.h, Date.cpp, Time.h, Time.cpp files, we are to use composition. How do I properly use composition to call those classes from my Appointment class.

Also my main exe file is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Appointment.h"

#include <iostream>
using namespace std;

int main()
{
	Appointment myAppointment;
	cout << "Please type in the appoinment description." <<endl;
	cin >> myAppointment;
	
	cout << "\nBelow is a list of all of your appointments."<<endl;
	cout << myAppointment;
	
	cout << endl;
	
	system("PAUSE");
	return 0;
}


In NO WAY, do I want anyone to just write this program for me. However, I would greatly appreciate any help, guidance, good refernces of places I can learn more about how to do what I'm needing to do in order to write this program. Thanks.

Using Eclipse as my IDE.
Last edited on
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 :-)
Last edited on
Topic archived. No new replies allowed.