Practice

Bus


Design and code a class named Bus that holds information about a TTC bus.  Upon instantiation, a Bus object may receive a null-terminated C-style string holding the route number followed by the route name.  If the object receives a string, the object accepts the number and the name only if the number is positive and less than 1000.  If the object does not receive a string or the number portion of the string is outside the acceptable range, the object assumes a safe empty state.  Your class does not impose any limitation on the number of characters in the name of a Bus 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:

  • int route() const - a query that returns the route number of the Bus object;
  • const char* name() const - a query that returns the address of the first character of the name of the Bus object; and
  • void change(const char*) - a modifier that receives a null-terminated C-style string and changes the route number and route name of the Bus object to the data received;
  • an insertion operator that inserts into an output stream the route number as a three digit number enclosed within parentheses and followed by the name of the bus as shown in the example below.
For example, consider the following program that uses your class

 #include <iostream>
 using namespace std;
 #include "Bus.h"

 int main ( ) {
     Bus bus1("196 Rocket"), bus2 = bus1;

     cout << bus1 << endl << bus2 << endl;
     bus2.change("000 Not in Service");
     bus1 = bus2;
     cout << (bus1.route() != 0 ? bus.route() : '*') << ' ' << bus1.name() << endl;

     return 0;
 }

This program produces the following output

 (196) Rocket
 (196) Rocket
 * Not In Service







  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License