Multiple Bools

I can't figure out why my bool values aren't getting passed on correctly. I have two bools, one for "sunRoof" and another for "autoTrans".

Client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
using namespace std;

#include "vehtypes.h"

int main()
{
   Car   c1( "Ford", 10000, "Mustang", false ),
         c2( "Chevy", 15000, "Camaro", true );
   Truck t1( "Ford", 12000, 1.5, true ),
         t2( "Dodge", 14000, 2.0, false );

   cout << "Dealer inventory:" << endl << endl;
   c1.showVehicle();
   c2.showVehicle();
   t1.showVehicle();
   t2.showVehicle();

   system("PAUSE");
   return 0;
}


Vehicle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef _VEHICLE
#define _VEHICLE

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Vehicle
{
   public:
      Vehicle( string mfg, double cost )
         { mfgName = mfg;
           dealerCost = cost;
         }
      ~Vehicle()
         {  } 
      double retailPrice(void)
         { return dealerCost * 1.25; }
      void showVehicle(void)
         { cout << "Manufacturer:   " << mfgName << endl
                << "Customer price: " 
                << fixed << showpoint << setprecision(2)
                << retailPrice() << endl;
         }      
   private:
      string mfgName;
      double dealerCost;
};

#endif 


Vehtypes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <string>
#include "Vehicle.h"
using namespace std;

#ifndef VEHICLE_H
#define VEHICLE_H

class Car : public Vehicle{
   private:
      string modelName;
      int sunRoof;
      
   public:
      Car(string, double, string, int);
      void showVehicle(void);
};

class Truck : public Vehicle{
   private:
      double capacityTons;
      bool autoTrans;
      
   public:
      Truck(string, double, double, bool);
      void showVehicle(void);
};

#endif

Car::Car(string mfg, double cost, string model, int sun)
   :Vehicle(mfg, cost)
   {
      modelName = model;
      int sunRoof = sun;
   }

void Car::showVehicle(){
   Vehicle::showVehicle();
   cout << "Model:          " << modelName << endl
        << "Sunroof:        " << (sunRoof ? "Yes" : "No") << endl
        << endl;
} 

Truck::Truck(string mfg, double cost, double capacity, bool)
     :Vehicle(mfg, cost)
     {
         capacityTons = capacity;
         bool autoTrans;
      }
      
void Truck::showVehicle(){
   Vehicle::showVehicle();
   cout << "Capacity-tons:  " << capacityTons << endl
        << "Auto Trans:     " << (autoTrans ? "Yes" : "No" << endl
        << endl;
}
closed account (z05DSL3A)
1
2
3
4
5
6
Car::Car(string mfg, double cost, string model, int sun)
   :Vehicle(mfg, cost)
   {
      modelName = model;
      int sunRoof = sun;
   }

sunRoof here is a local variable so sun is never assigned to the class member. (also sunroof in not a bool)

You have the same with truck.
Try:
1
2
3
4
5
Car::Car(string mfg, double cost, string model, int sun)
   :Vehicle(mfg, cost), modelName(model),sunRoof(sun)
   {

   }
and
1
2
3
4
5
Truck::Truck(string mfg, double cost, double capacity, bool autoTrans)
     :Vehicle(mfg, cost), capacityTons(capacity),autoTrans(autoTrans)
     {

      }

Last edited on
Topic archived. No new replies allowed.