Practice

Car


Design and code a class named Car that holds sales information about a car.  Place your class declaration in a header file named Car.h and your function definitions in an implementation file named Car.cpp.  Include in your coding all of the statements necessary for your code to compile under a standard C++ compiler.

Upon instantiation, a Car object may receive no parameters or may receive an int holding the odometer reading, a double holding the market value and a null-terminated C-style string holding the make.  The odometer reading should be positive-valued but not greater than 999999.  The market value should be greater than $499.99.  If the object receives a valid number, a valid value and a non-empty string, the object accepts all three values.  Otherwise, the object assumes a safe empty state.  You may assume that the string holding the make of the car shall not be greater than 30 characters in length. 

Your class also includes the following member functions:

  • void set(int o, double v, const char* s) - a modifier that changes the data stored in the object only if this function receives valid parameter values.  The first parameter receives the odometer reading.  The second parameter receives the market value.  The third parameter receives the make.  If any of the values received is invalid, the object stores a safe empty state.  The criteria for validity are listed in the constructor description above. 
  • int valid() const - a query that returns 1 if the data stored is valid and 0 if there is no valid data stored;
  • int valid(const char* s) const - a query that returns 1 if the string received is the same as the make of the car or as the first part of the make of the car.  This function returns 0 otherwise; and
  • void display() const - a query that inserts into the standard output stream the odometer reading, the market value, and the make of the car.  The odometer reading displays in a field of six, right justified, and zero-filled.  The market value displays in a field of ten, right justified, and blank filled.  The make of the car displays left-justified in a field of 30.  This function flushes the output buffer and starts a new line. 
For example, consider the following program that uses your class

 #include "Car.h"

 int main ( ) {
     Car car[5];

     car[0].set(   12345, 20000., "2005 Camry");
     car[1].set(     -45, 20000., "2005 Ford");
     car[2].set(   12345,   200., "2005 BMW");
     car[3] = Car(456123,  2000., "1995 Mercedes");
     car[4] = Car(  1234, 40000., "2005 BMW");

     for (int i = 0; i < 5; i++)
         if (car[i].valid() != 0)
             car[i].display();

     for (int i = 0; i < 5; i++)
         if (car[i].valid("2005") != 0)
             car[i].display();

     return 0;
 }

This program produces the following output

  012345  20000.00 2005 Camry
  456123   2000.00 1995 Mercedes
  001234  40000.00 2005 BMW
  012345  20000.00 2005 Camry
  001234  40000.00 2005 BMW







  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License