Kat H.

asked • 07/01/21

C++. Exercise 1: Basic Car class

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:

  1. a make (C string)
  2. a model(C string)
  3. a year(int).

Create two files in your directory:

  1. 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.
  2. 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

  1. All member variables need to be private. Only functions can be pub- lic.
  2. All member variables have a corresponding getter and setter member function. See carmain1.cpp for what these should be called.
  3. There should be a default constructor for the class. Set C strings to empty strings by using:
  4. make[0] = '\0';
  5. Also, set year to 0.
  6. 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'.
  7. 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;  

 }

   

  

1 Expert Answer

By:

Patrick B. answered • 07/02/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.