@
dsopshin
OK, I think you need some major reorganisation of your code, some overall points:
1. Naming of variables is confusing;
2. No need for get / set functions.
1. Variable names
The name leg is confusing, In my mind you ought to have the concept of a Route being a collection of Flights. But you have called the collection a leg. A
std::vector
might be better than an array for this. So try :
std::vector<TRS::Flight>Route; // A vector of Flight objects named Route
see below for the namespace explanation.
Using a vector means you can
std::push_back
items into the vector.
2. No need for get / set functions
There is no need to have a get / set function for each member variable.
We had a huge discussion about that here :
Get functions are only needed when access to an objects members is needed outside the class, and the same functionality cannot be achieved with a member function.
Instead of set functions, you can make use of constructors with initialiser lists. But first the class declaration:
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
|
#pragma once
#include <string>
using namespace std;
// Travel Reservation System namespace
namespace TRS {
class Flight
{
private:
std::string m_FlightNumber; // a string because we are not doing any math with it
std::string m_DestinationCity;
std::string m_DepartureTime;
std::string m_DepartureGate;
unsigned int m_DepartureMonth;
unsigned int m_DepartureDay;
public:
Flight();
Flight(std::string FlightNumber, std::string DestinationCity, std::string DepartureTime,
std::string setDepartureGate, unsigned int DepartureMonth, unsigned int DepartureDay);
~Flight();
void setNumber(int number);
void setDestinationCity(string city);
void setDepartureTime(string time);
void setDepartureGate(string gate);
void setDepartureMonth(int month);
void setDepartureDate(int date);
int getMonth();
int getDate();
void show();
void DisplayDetails();
void set();
void Display MonthDay();
}; // end of Flight class
} // end of TRS namespace
|
Now the initialiser list :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
namespace TRS {
Flight::Flight(std::string FlightNumber, std::string DestinationCity, std::string DepartureTime,
std::string DepartureGate, int DepartureMonth, int DepartureDay) : // start initialiser list
m_FlightNumber(FlightNumber),
m_DestinationCity(DestinationCity),
m_DepartureTime(DepartureTime),
m_DepartureGate(DepartureGate),
m_DepartureMonth(DepartureMonth),
m_DepartureDay(DepartureDay)
{}
void Flight::DisplayDetails() {
std::cout << "Flight " << m_FlightNumber << " for " << m_DestinationCity << " leaving at ";
std::cout << m_DepartureTime << " from gate " << m_DepartureGate << std::endl;
}
void Display MonthDay() {
std::cout << "Here is your itinerary beginning on "
<< m_DepartureMonth() << "/" << DepartureDay() << ":\n";
}
} // end of TRS namespace
|
Now, in main, you can build an object using it's constructor, then call the object's functions. This idea is part of what is called encapsulation: Generally all the functions & data for an object are within the objects class.
A further point: Remember that a class function has direct access to member variables, so there is no need to use the
this
operator, just refer to them directly. The this operator is handy for operator overloading.
As you can see, I got rid of the
using namespace std;
and added a new namespace. The
std
namespace is huge, bringing it into the global namespace pollutes it, and runs the risk of variable & function naming conflict, which is the whole point of using a namespace! Did you know that there are
std::distance
,
std::left
,
std::right
, just to name a few? So it is best to put your own classes into your own namespace(s). To refer to something in a namespace, pecede it with the name of the namespace and the scope resolution operator, like this :
std::cout
std::vector<TRS::Flight>Route; // A vector of Flight objects named Route
So in summary, if you do all these things, you will have much less & better code, and might even get better marks for your assignment.
I look forward to seeing how you go 8+)