Need help with inheritance and creating a ToString function

Hello everyone, I am new to c++ and am working on a project for class. The purpose of this project is to create an airline flight reservation system that will allow a user to book and purchase flights. The first milestone is asking me to create 2 classes (hubNode and fightNode) that implements a linked list node and can read 2 seperate .csv files (flightInfo and hubInfo) and load that data into 2 seperate linked lists but connected through inheritance. I feel my code does that to some degree except I am not using inheritance and would like some guidance on how implement inheritance given my code below. Also, I am asked to create a debugging function that loops over each hub and prints the outgoing flights, and that is where I am stuck. I know I need two seperate ToString functions inside each class and the use of inheritance will connect the two classes enabling me to loop over hubNode and print flightNode info, I am not sure how to implement it. Any help is appreciated, thank you.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  #include <iostream>
#include <fstream>
#include <string>
class FlightNode;
class Date_Time;
class HubNode;
using namespace std;
int main () {
  string line,value1,value2,value3,value4, hubName, hubLocation, hline, hvalue1, hvalue2;
  int number,i=0;
  ifstream flightfile ("flight.csv");
  ifstream hubFile ("hub.csv");
 class HubNode {
 public:
	  string name;
	  string location;
	  HubNode* next;
	  FlightNode* headFlights;
 	  
	  void print();
	  string setName(string name) {return name = name;}
	  string setLocation(string location) {return location = location;}
  };

class FlightNode {
public:
	string flightNumber;
	double price;
	string flightCompany;
	Date_Time* departure;
	int duration;
	HubNode* source;
	HubNode* destination;
	FlightNode *next;

	//virtual float getBaggageFees();
	//virtual int getDelay();
};

class Date_Time {
public:
	int minutes;
	int hours;
	int day;
	int month;
	int year;

	//Date_Time(); do we need this....
	void AddMinutes(int min);
	string ToString();
};

FlightNode *head = new FlightNode;
FlightNode *temp = new FlightNode;
FlightNode *nextNode = new FlightNode;
HubNode *hNode = new HubNode;
HubNode *hubHead = new HubNode;
HubNode *hubTemp = new HubNode;
HubNode *hubNextNode = new HubNode;
HubNode *hubPointer = new HubNode;
HubNode *first = new HubNode;

Date_Time *dT = new Date_Time;
temp = head;

cout <<"Flight Data File" << endl;

  if (flightfile.is_open())
  {
	   while ( getline (flightfile,line) )// Omit the Caption Line.
	 { 
		while(getline(flightfile,value1,','))//Get every value in order.
		{ 
		if (temp == head)
		{
			head->flightNumber = value1;
			cout << "FlightNum: " << head->flightNumber << endl;
			getline(flightfile, value2,',');
			number=atof(value2.c_str());
			head->price = atof(value2.c_str());
			cout << "Price: " << head->price <<'\n';
			getline(flightfile,value3,',');
			hNode->name = value3;
			head->source = hNode; //hNode is now becoming head->source
			number=atoi(value3.c_str());
			cout << "Source: " << head->source->name << endl;
			getline(flightfile,value4,',');
			hNode->name = value4;
			head->destination = hNode;
			cout << "Destination: " << head->destination->name << endl;
			number=atoi(value4.c_str());
			getline(flightfile,value1,'/');
			number = atoi(value1.c_str());
			dT->minutes = number;
			cout << "minutes: " << dT->minutes << endl;
			getline(flightfile,value1, '/');
			number = atoi(value1.c_str());
			dT->hours = number;
			cout << "hours :" << dT->hours << endl;
			getline(flightfile,value1, '/');
			number = atoi(value1.c_str());
			dT->day = number;
			cout << "days :" << dT->day<< endl;
			getline(flightfile,value1, '/');
			number = atoi(value1.c_str());
			dT->month = number;
			cout << "months :" << dT->month << endl;
			getline(flightfile,value1, ',');
			number = atoi(value1.c_str());
			dT->year = number;
			cout << "years :" << dT->year << endl;
			getline(flightfile,value1, ',');
			number = atoi(value1.c_str());
			head->duration = number;
			cout<< "duration :" << head->duration << endl;
			getline(flightfile,value1, '\n');
			head->flightCompany = value1;
			cout<< "Company :" << head->flightCompany<< endl;
			cout <<'\n';
			head->next = nextNode; //heads next is now nextNode
			temp = nextNode; // temp node is now nextNode
		}
		temp->flightNumber = value1;
		cout << "FlightNum: " << temp->flightNumber << endl;
		getline(flightfile, value2,',');
		number=atof(value2.c_str());
		temp->price = atof(value2.c_str());
		cout << "Price: " << temp->price <<'\n';
		getline(flightfile,value3,',');
		hNode->name = value3;
		temp->source = hNode; //hNode is now becoming temp->source
		number=atoi(value3.c_str());
		cout << "Source: " << temp->source->name << endl;
		getline(flightfile,value4,',');
		hNode->name = value4;
		temp->destination = hNode;
		cout << "Destination: " << temp->destination->name << endl;
		number=atoi(value4.c_str());
		getline(flightfile,value1,'/');
		number = atoi(value1.c_str());
		dT->minutes = number;
		cout << "minutes: " << dT->minutes << endl;
		getline(flightfile,value1, '/');
		number = atoi(value1.c_str());
		dT->hours = number;
		cout << "hours :" << dT->hours << endl;
		getline(flightfile,value1, '/');
		number = atoi(value1.c_str());
		dT->day = number;
		cout << "days :" << dT->day<< endl;
		getline(flightfile,value1, '/');
		number = atoi(value1.c_str());
		dT->month = number;
		cout << "months :" << dT->month << endl;
		getline(flightfile,value1, ',');
		number = atoi(value1.c_str());
		dT->year = number;
		cout << "years :" << dT->year << endl;
		getline(flightfile,value1, ',');
		number = atoi(value1.c_str());
		temp->duration = number;
		cout<< "duration :" << temp->duration << endl;
		getline(flightfile,value1, '\n');
		temp->flightCompany = value1;
		cout<< "Company :" << temp->flightCompany<< endl;
		cout <<'\n';
		temp->next = nextNode; //current becomes temp next node
		temp = nextNode; //temp becomes current node
		}
	} 
	flightfile.close();
	}
  else  { cout << "Unable to open file"; }
  
  //hub data file
   if (hubFile.is_open())
  {
	   while ( getline(hubFile,line) )// Omit the Caption Line.
	 { 
		while(getline(hubFile,value1,','))//Get every value in order.
		{ 
		
			hubHead->name = value1;
			cout<<"Airport Name :"<< hubHead->name <<endl;
			getline(hubFile, value2, '\n');
			hubHead->location = value2;
			cout<<"Airport Location :" << hubHead->location << endl;
			cout <<"\n" << endl;
	
		}
	} 
	hubFile.close();
	}
  else  { cout << "Unable to open file"; }
  getchar();

  delete head;
  delete temp;
  delete hNode;
  delete hubHead;

  return 0;
  }
  
 
What is the link? Inheritance can represent IS-A relation or IS-IMPLEMENTED-IN-TERMS-OF relation. Composition and templates can do the latter too.

Flights do not sound that they were Hubs, nor Hubs look like special flights. Thus, not a IS-A.

List of flights is a list. List of hubs is a list. Both must have list management code. There is no point in writing that code twice. Flights IS-A list. Hubs IS-A list.


You have awful duplication of your flight input. Do input as one block/function and then output as separate block/function (preferably a member of fightNode). When you manage to hide most of that code elsewhere, you might start to see logical errors in your input loop.


Do not define classes within main() or any other function.
I may have gotten inheritance and polymorphism mixed up when reading the directions for the project. I attached a link to a sample UML for this project.

https://drive.google.com/file/d/0B-Zb2xLh-EGIVGI5U21WcWdvZ3M/edit?usp=sharing
Topic archived. No new replies allowed.