Exercise 1: Basic Car class
Inside your 'exercise1' directory, put all required files (the carmain1.cpp, your car.cpp and car.h files).
Download the carmain1.cpp file. Use this to test your class files with. The final class should compile and run with the original file albeit you can change it while writing and debugging your code.
What To Code
A Car object will have three member variables:
- a make (C string)
- a model(C string)
- a year(int).
Create two files in your directory:
- a car.h file with the definition of the Car class inside. Only put function pro- totypes in the class definition; don't implement the member functions in the class definition.
- a car.cpp file with the member function definitions. This should include the car.h file.
NOTE: You will need a 'getter' and a 'setter' for each member variable plus a de- fault constructor.
Download the carmain1.cpp file from d2l and test your class by putting all files in- side one directory and compiling them with a makefile (see example makefile in this week's module).
Requirement
-
All member variables need to be private. Only functions can be pub- lic.
- All member variables have a corresponding getter and setter member function. See carmain1.cpp for what these should be called.
- There should be a default constructor for the class. Set C strings to empty strings by using:
- make[0] = '\0';
- Also, set year to 0.
- There should be TWO files for the Car class: a car.cpp file and a car.h file. Put these in a directory called 'hwk3exer1'.
- Also add the original (unmodified) carmain1.cpp file and the makefile.
Use carmain1.cpp for testing as I will be using this file to test your submission
(don't write your own).
-------------------------------
EXAMPLE OUTPUT #1
--------------------------------
Sedan before initialization:
Make:
Model:
Year: 0
Setting values for sedan:
Enter make: Honda
Enter model: Civic
Enter year: 2003
printing values for sedan:
Make: Honda
Model: Civic
Year: 2003
carmain1.cpp
/* carmain1.cpp
* Please don't rewrite this file
* I will be using the exact
* same file below when grading
*
*/
#include <iostream>
#include "car.h"
using namespace std;
int main()
{
string cmake = "Honda";
string cmodel = "Civic";
int cyear = 2017;
Car sedan; // calls the default constructor
// calls parameterized constructor.
Car compact(cmake, cmodel, cyear);
// input
string imake;
string imodel;
int iyear;
// output
string omake;
string omodel;
int oyear;
//Print out values before setting them
cout << "Sedan before initialization: " << endl;
omake = sedan.getMake();
cout << "Make: " << omake << endl;
omodel = sedan.getModel();
cout << "Model: " << omodel << endl;
oyear = sedan.getYear();
cout << "Year: " << oyear << endl;
cout << "Compact values " << endl;
omake = compact.getMake();
cout << "Make: " << omake << endl;
omodel = compact.getModel();
cout << "Model: " << omodel << endl;
oyear = compact.getYear();
cout << "Year: " << oyear << endl;
//set the values for sedan
cout << "Setting values for sedan: " << endl;
cout << "Enter make: ";
getline(cin, imake);
sedan.setMake(imake);
cout << "Enter model: ";
getline(cin, imodel);
sedan.setModel(imodel);
cout << "Enter year: ";
cin >> iyear;
cin.ignore(200, '\n');
sedan.setYear(iyear);
// getting the data back out
cout << "printing values for sedan: " << endl;
omake = sedan.getMake();
cout << "Make: " << omake << endl;
omodel = sedan.getModel();
cout << "Model: " << omodel << endl;
oyear = sedan.getYear();
cout << "Year: " << oyear << endl;
return 0;
}