2

I currently have a program for an airline reservations database that uses list<string>flight_list and string name (which is solely the last name). I need to extend this program so that it stores a linked linked of Passenger objects instead of strings, I assume by changing it to list<Passenger> passenger_list? I'm having trouble understanding how to convert the functions within my class to accommodate for this change. The passenger object must contain each passenger's first name, last name and destination. There's a lot, but these are a couple of the functions as an example:

void read_from_file(list<string>& flist, string filename)
{
    string name;
    ifstream input(filename.c_str());
    while (input >> name) 
    {                   
        flist.push_back(name);
    }
    input.close();
}

void insert(list<string>& flist, string name)
{
    flist.push_back(name);
}

bool check_reservation(list<string>& flist, string name)
{
    list<string>::iterator i1, i2;
    i1 = flist.begin();
    i2 = flist.end();
    return (find(i1, i2, name) != i2);
}

I'm working with a main.cc class and a database.cc class. So yeah, any tips on how to generalize the classes would be appreciated please.

Brittany
  • 119
  • 1
  • 10

2 Answers2

1

Some observations that I hope helpful (but be careful: the following code is untested).

If your Passenger need first name, last name and destination, I suppose that there is a Passenger constructor with three string parameters.

I suppose that your Read_from_file() should become something like

void read_from_file(list<Passenger> & flist, string const & filename)
{
    string firstN, lastN, dest;
    ifstream input(filename.c_str());
    while (input >> firstN >> lastN >> dest) 
    {                   
        flist.push_back(Passenger(firstN, lastN, dest));  // for C++98
        // flist.emplace_back(fistN, lastN, dest); // for C++11/14
    }
    // input.close(); no need for close: file closed by destructor
}

Suggestion: pass parameters by reference, and using const, when possible.

Similarly, the insert() can be adjusted in a simple way (in two different modes, depending on whether you're using a recent C++ compiler or pre C++11)

void insert(list<Passenger> & flist, string const & firstN,
            string const & lastN, string const & dest)
{
    flist.push_back(Passenger(firsN, lastN, dest)); // C++98 version
    // flist.emplace_back(firsN, lastN, dest); // C++11/14 version
}

Less obvious how to check_reservation(). And really dependent from the C++98/C++11 question.

I suppose that you want a check_reservation() that want find a Passenger giving first name, last name and destination.

In C++11 is fairly easy (supposing your Passenger contains a firsN, a lastN and a dest member)

bool check_reservation(list<Passenger> const & flist, string const & firstN,
                       string const & lastN, string const & dest)
{
  return flist.cend() != find_if(flist.cbegin(), flist.cend(),
                                 [&](Passenger const & p)
                                     (return    (fistN == p.fistN)
                                             && (lastN == p.lastN)
                                             && (dest == p.dest);));
}

In C++98 you don't have lambda functions so is more tricky. You have to construct a functor for comparison; something like

struct person_eq : std::unary_function <Passenger, bool>
{
    string const & f;
    string const & l;
    string const & d;

    person_eq (string const & f0, string const & l0, string const & d0)
       : f(f0), l(l0), d(d0)
         {}
    bool operator() (Passenger const & p) const
     { return (p.firstN == f) && (p.lastN == l) && (p.dest == d); }
};

and your check_reservation() should become something like

    bool check_reservation(list<Passenger> const & flist, string const & firstN,
                           string const & lastN, string const & dest)
    {
      return flist.cend() != find_if(flist.cbegin(), flist.cend(),
                                     person_eq(fistN, lastN, dest));
    }

I repeat: this code is untested, so be careful.

p.s.: sorry for my bad English.

Edit

Sorry: if it's defined the operator== for your Passenger, your check_reservation() can be really easy

bool check_reservation(list<Passenger> const & flist, string const & firstN,
                       string const & lastN, string const & dest)
{
  return flist.cend() != find(flist.cbegin(), flist.cend(),
                              Passenger(firstN, lastN, dest));
}
max66
  • 65,235
  • 10
  • 71
  • 111
  • Thanks heaps! I haven't tested it yet but this has definitely made it a lot more clear what I have to do for the rest of the functions also. I appreciate it! – Brittany Mar 31 '16 at 12:51
0

Define class Passenger correctly and everything else (fine passenger give a name etc. etc.) will be lot easier for you and anyone else who happens to be lucky one to maintain it in future.

Think through and implement following for class Passenger:

destructor
copy constructor
copy assignment operator

Good luck ! If you face specific issue with your C++ code, post it as separate question.

Reference: https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)#Rule_of_Three

vcp
  • 962
  • 7
  • 15