Question: Part 1) a) First, write a C++ class named Car that has the following private member variables year (int) make (string) speed (int) Add the
Part 1)
a) First, write a C++ class named Car that has the following private member variables
year (int)
make (string)
speed (int)
Add the following public members functions to class Car
Default Constructor with no parameters: This constructor should set the year to -1, make to "none" and speed to 0.
Constructor with parameters: The constructor should accept the make and year as arguments and use these arguments values to set the year and make member variables of the class. Speed should be initialized to 0 by this constructor.
Accessors (getters): add these member functions for year, make and speed member variables.
Mutators (setters): add these member functions for year, make and speed member variables.
accelerate: this member function should add 5 to speed member variable each time it is called.
brake: this member function should subtract 5 from speed member variable each time it is called.
print: this member function should print the values of make, model, and speed of the car in to the screen.
b) Then, in your main function create an two objects of class Car: one with using the default constructor and the other one using the constructor with parameters.
call print function for each Car objects.
call accelerate function for each Car object and followed by a call to print function for each Car object.
call brake function for each Car object and followed by a call to print function for each Car object.
Part 2)
a) Design and implement a C++ class called Date that has the following private member variables
month (int)
day (int)
year (int)
Add the following public member functions to the class.
Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is between 1-31, month is between 1-12 and year is >0). If not, the constructor should use default values 1, 1, 2000 for month, day , and year respectively.
Accessors (getters): write one for each member variable month, day, and year.
Mutators (setters): write one for each member variable month, day, and year. The mutators should check if the values passed are valid (that is day is between 1-31, month is between 1-12 and year is >0) and if not, do not update the member variable with an invalid value.
print1: this function should print the date in "month/day/year" format to the screen, where each month, day and year are int's. For example, "1/2/2012"
print2: this function should print the date in "month day, year" format to the screen where day and year are int's and month is a string. For example, "January 2, 2012"
b) Demonstrate the use of Date class by writing code in your main function. Make sure to create at least two different Date objects and call both print1 and print2 functions, etc.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
