Programming Assignment #6
Due: Mon, Dec 7
Objective: Upon completing this assignment,
you should be able to implement a simple class, as well as gain a better
understanding of the building and use of classes and objects.
Task:
Write a class called Car, which has the features and member functions
described below. This is based on Programming Challenge 13.3 in
your textbook. You should write the Car class in the files
car.h and car.cpp.
A car object will store information about the car's make, model, and
year, as well as its current travel speed. Public member functions will
allow access to the private information, the ability to set the car
information, and the ability to speed up and slow down.
Details:
- Member data: The Car class should store the following
information as private member data variables
- The car's make (examples: Toyota, Ford, Honda). This will be a
string
- The car's model (examples: Camry, Tempo, Accord). This is also a
string
- The year of this car model, stored as an integer. The class
will need to enforce this invariant rule -- year cannot be a
negative number
- The current speed that the car is traveling (an integer). The
assumed units are miles-per-hour, so if the speed is 35, this means
35 MPH. The speed must be a non-negative number
- The Car class should have a constructor that takes in three
arguments, in this order:
- the make
- the model
- the year (as an optional parameter)
Note that the first two arguments (make, model) should always be
provided when creating a car object, but the year may be optional.
The constructor should initialize the data of a Car object to store the
data provided in the arguments, and it should start the car's speed at 0
MPH.
If a car object is created without the year provided, default the
year to
the current year (i.e. 2015).
If the provided year is invalid (i.e. negative), default the year
to the current year (2015)
Examples of usage:
Car hisCar("Ford", "Tempo", 1997); // car set to be a 1997 Ford Tempo
Car herCar("Honda", "Accord"); // car set to be a 2015 Honda Accord
Car badCar("Toyota", "Camry", -50); // car set to be a 2015 Toyota Camry
- The Car class should have a function called Set, which
takes in three arguments -- the make, the model, and the year.
This function should reset the car's internal information to the data
passed in as long as the provided data is valid. IF the provided year
is invalid (i.e. negative), this constitutes bad data, so do not make
any changes to the car, and return false for failure. Otherwise,
set the data accordingly and return true for success.
Sample calls, based on initial objects above:
hisCar.Set("Hyundai", "Sonata", 2006); // hisCar is now a 2006 Hyundai Sonata
herCar.Set("Horse Drawn", "Cart", -45); // herCar is still a 2015 Honda Accord
- The Car should have accessor functions called:
- GetMake -- returns the car's make
- GetModel -- returns the car's model
- GetYear -- returns the car's year
- GetSpeed -- returns the current speed
Each of these member functions should simply return a copy of the
appropriate internal data item. Make sure to use appropriate return
types for each. These functions will not change the object's internal
data in any way.
- Create a member function called Display that prints out the
data about the car to standard output, in this format:
Your car is a 2005 Toyota Camry
And it is currently going 17 MPH
Note, the above is just an example. Your output should print the
appropriate information for the given car. Using the previous examples,
this call:
herCar.Display();
would print the following:
Your car is a 2015 Honda Accord
And it is currently going 0 MPH
- Create a function called Accelerate, which takes in a
single character argument. The character will represent a level of
pressure to apply to the accelerator. The choices are:
- Heavy acceleration ('H' or 'h')
- Medium acceleration ('M' or 'm')
- Light acceleration ('L' or 'l')
The function should increase the car's speed based on the level of
acceleration requested.
- Heavy acceleration increases speed by 10 MPH
- Medium acceleration increases speed by 5 MPH
- Light acceleration increases speed by 1 MPH
If the level provided is invalid (i.e. some character other than
the valid ones), do not change the car's speed at all, and return false
for failure. Otherwise, modify the car's speed appropriately, and
return true for success
- Create a function called Brake, which also takes in a
single character parameter, representing the brake levels. These levels
are the same ones as for acceleration, using the same characters (H, M,
L, upper or lowercase).
- Heavy braking should decrease the speed by 10 MPH
- Medium braking should decrease the speed by 5 MPH
- Light braking should decrease the speed by 1 MPH
Note that the slowest possible speed for the car is 0. So if braking
would cause the car to stop (i.e. 0 or below), set the speed to 0. (The
car's speed can never be negative).
As with the previous function, if the level is invalid (a character
other than the valid ones), do not change the car's speed, and return
false for failure. Otherwise, modify the car's speed, and return true
for success.
- I have providing a sample driver program (called
driver.cpp)
that uses objects of type Car and illustrates some of the usage of the
member
functions. You can get the driver.cpp file
at this link.
I have also provided the output from the sample execution of my driver.cpp program at
this link. Your class declaration and definition files must work
with my main program, as-is (do not change my program to make your code
work!).
Important Notes:
General Requirements
-
No global variables, other than constants!
-
All member data of your class must be private
- All member functions described in this specification should be
public
- For storing strings, you may use string objects or C-strings -- your
choice. (I'd recommend using string objects, as they will be easier to
work with).
- You may use any of these libraries:
- iostream
- iomanip
- cctype
- cstring
- string
-
Your source code should be readable and well-documented.
-
Your car.h file should contain the class declaration only. The
car.cpp file should contain the member function definitions.
Submitting:
Submit the following files through Blackboard in the usual way:
car.h
car.cpp
Make sure your filenames are these exact names, and do not submit the driver.cpp file.