CIS 22B
Assignment C

Use the style guide.

In Problem C1 we will start building the Car class. We will use this class throughout the rest of the assignments.

In Problem C2 we will add constructors to the Car class.

In Problem C3 we will add an operator== friend function for the Car class.

Problem C1

Order of functions in the code:

Within the Car class:

  1. setup
  2. output

Global functions, outside of the Car class:

  1. main
  2. input

Define a class named: Car
containing the following private data:

reportingMark   a string with the AAR reporting mark
carNumber   an int
kind   a string which could contain "business" "maintenance" or "other"
loaded   a bool
destination   a string with a destination or the word "NONE"

Note:
A destination is required if the car is loaded.
If it is not loaded the destination may be either a destination or the word "NONE".
Be sure to allow the user the option of entering a destination when the car is not loaded.

Within the class create the following member functions:

setup

output

After the class, create the following global functions:

main

input

Test your program with the following data:

reportingMark   SP
carNumber   34567
kind   business
loaded   true
destination   Salt Lake City

Problem C2

Copy the solution from problem C1.

Order of functions in the code:
Note that the setup and output functions have been moved to a different location.

  1. Car constructors and destructor within the Car class definition
    1. default constructor
    2. copy constructor
    3. other constructors
    4. destructor
  2. main
  3. Car member functions declared within the Car class but defined later
    1. setup
    2. output
  4. input

Make the following additions and changes:

  1. Build three constructors and a destructor for your Car class:
    • A default constructor. Build this constructor with only one line of code that calls the setup member function. Set the following values:
      reportingMark   (an empty string)
      carNumber   0
      kind   other
      loaded   false
      destination   NONE
    • A copy constructor. Build this constructor with only one line of code that calls the setup member function.
    • A constructor that accepts five parameters. Build this constructor with only one line of code that calls the setup member function.
    • A destructor that does nothing.
  2. Put a declaration in the Car class for the setup and output functions. (The declaration is just the prototype)
    Put the definition of these functions later. (The definition of a function contains the code)
  3. Remove the call to the setup function from the main function.
  4. Remove new and delete from the main function.
  5. Revise the main function to create three Car objects in the stack by using the three constructors:
    • car1 which uses the values read from the user. Use the same values used to test assignment C1.
    • car2 which is copied from car1.
    • car3 which is created with the default constructor.

Then add code in the main function to print these headings and use the output member function to print the data for each of the three cars:

Contents of car1:
reportingMark:   SP
carNumber:   34567
kind:   business
loaded:   true
destination:   Salt Lake City

Contents of car2:
( - Same as car 1 - )

Contents of car3:
reportingMark:  
carNumber:   0
kind:   other
loaded:   false
destination:   NONE

Problem C3

Copy the solution from problem C2.

Order of functions in the code:
Note that the order is not changed, but with the operator== function added.

  1. Car constructors and destructor within the Car class definition
    1. default constructor
    2. copy constructor
    3. other constructors
    4. destructor
  2. main
  3. Car member functions declared within the Car class but defined later
    1. setup
    2. output
  4. operator==   with Car parameters
  5. input

Create a friend function for the function operator== which tests to see if two objects are equivalent. The two objects are equivalent if they have the same reportingMark and carNumber (do not look at the kind, loaded, and destination fields).

Add the following code in main:

if (car1 == car2)
  cout << "car1 is the same car as car2\n";
else
  cout << "car1 is not the same car as car2\n";
if (car2 == car3)
  cout << "car2 is the same car as car3\n";
else
  cout << "car2 is not the same car as car3\n";

Test with the same data as in problem C2.