Practice
Flight
Design and code a class named Flight that holds information about an airline
flight. Upon instantiation, a Flight object may receive an int holding the flight number, and a
null-terminated C-style string holding the flight
destination. The flight number should be
positive-valued. If the object receives a valid number and
a string, the object accepts both. Otherwise, the object
assumes a safe empty state. The object adopts a default
output width of ten (10) characters for the flight number.
Your class does not impose any limitation on the number of
characters in a Flight object.
Your design includes all of the member functions needed to
accomodate this feature under a variety of applications,
including the one below.
Your class also includes the following functions:
- const char* destination() const -
a query that returns the address of the first non-whitepsace
character in the description of the Flight object;
- int number() const - a query that
returns the flight number; and
- Flight& width(int) - a
modifier that changes the width of the output field for the
flight number to the integer value received if that value is a
greater than that needed to display fully the flight number; if
the integer value received is invalid, your object reverts to
the default number of characters for the class. Your
function returns a reference to the current object as modified,
so that your function can be used to insert the current object
into the output stream directly as shown below; and
- an insertion operator that inserts the flight number
followed by the flight description into an output stream.
Your operator displays the flight number left-justified in the
current field width, followed by a trailing space and the
flight description. Your operator accepts
cascading.
For example, consider the following program that uses your
class
#include <iostream>
using namespace std;
#include "Flight.h"
void display(Flight flight) {
cout << flight.width(2) << endl;
}
int main ( ) {
Flight f, g(857, "Toronto");
cout << g.width(9) << endl;
f = g;
display(f);
cout << f.number() << endl;
cout << f.description() << endl;
return 0;
}
|
This program produces the following output
857 Toronto
857 Toronto
857
Toronto
|
|
|
|