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:
- setup
- output
Global functions, outside of the Car class:
- main
- 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
- Is a member function of the class Car
- Takes the five parameters by value: reportingMark, carNumber, kind, loaded, and destination
- Puts the data in the object
- Has a return type of void
output
- Is a member function of the class Car
- Has no parameters
- Prints the member data in a neat format
- Has a return type of void
After the class, create the following global functions:
main
- Contains six variables:
- a pointer named ptr which points to a Car object
- a string named reportingMark to contain two to four characters
- an int named carNumber
- a string named kind which could contain: "business" "maintenance" or "other"
- a bool named loaded
- a string named destination containing a destination or the word NONE
- Uses new to obtain space for an object of type Car
- Calls the input, setup, and output functions
- Deletes the space obtained using new
input
- Is a global function, not a member of the class
- Takes the reportingMark, carNumber, kind, loaded, and destination as reference parameters
- Reads the reportingMark, carNumber, kind, loaded, and destination from the user
- Has a return type of void
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.
-
Car constructors and destructor within the Car class definition
- default constructor
- copy constructor
- other constructors
- destructor
- main
-
Car member functions declared within the Car class but defined later
- setup
- output
- input
Make the following additions and changes:
-
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.
-
A default constructor.
Build this constructor with only one line of code that calls the setup member function.
Set the following values:
-
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) - Remove the call to the setup function from the main function.
- Remove new and delete from the main function.
- 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.
-
Car constructors and destructor within the Car class definition
- default constructor
- copy constructor
- other constructors
- destructor
- main
-
Car member functions declared within the Car class but defined later
- setup
- output
- operator== with Car parameters
- 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.