Compile and run your program and notice the output of member function print() before calling read(). It should look something like:

@ 2.32211e-322 with MPG: 6.89746e-308
Enter the name of the car: Civic
Enter the price for a Civic: 19500
Enter the MPG for a Civic: 31
Civic @ 19500 with MPG: 31

User input is shown in italics for the example only, NOT your program. Notice that before calling read(), the print() function shows garbage output. After calling read(), the output is correct. Why do you think the first call to print() produces garbage output?

Because the member variables name, price and mpg have NOT been initialized.

Add a constructor declaration to the class specification. Your class structure should now look like:

Compile and run your program and verify you see the car information printed like:

none @ 0 with MPG: 0
Enter the name of the car: Civic
Enter the price for a Civic: 19500
Enter the MPG for a Civic: 31
Civic @ 19500 with MPG: 31
Other cars for comparison:
Toyota Corolla @ 18500 with MPG: 30

User input is shown in italics for the example only, NOT in your program. If you have problems, ask a classmate or the instructor for help.

Identify which constructor is called for each object and add a comment in your source code where the object is created.

Save your car.cpp file as we will add to it in the next exercise.

When finished please help those around you.

Check Yourself

C++ determines which constructor to call by matching the order of the parameter ________.

Of the following, the wrong way to construct an object of class Person is ________.

Person bruce;

Person bruce();

Person bruce("Bruce");

Person bruce("Bruce", 39);

True or false: the following is a correct way to construct an object of class Person.

Compile and run your program and verify you see the car information printed like:

none @ 0 with MPG: 0
Enter the name of the car: Civic
Enter the price for a Civic: 19500
Enter the MPG for a Civic: 31
Civic @ 19500 with MPG: 31
Other cars for comparison:
Toyota Corolla @ 18500 with MPG: 30
Junker @ 350 with MPG: 10

User input is shown in italics for the example only, NOT in your program. If you have problems, ask a classmate or the instructor for help.

If you see output like the following for the junker, then you have a problem with shadowing.

@ 1.25384e+224 with MPG: 10

Correct the shadowing problem by changing the names of the parameters in the two-parameter constructor.

Write an accessor and mutator function for each member variable following the "standard" get and set naming protocol. For example, here are the prototypes for the name variable: