DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   classes and inheritance problems (http://www.daniweb.com/forums/thread107941.html)

robotnixon Feb 6th, 2008 8:10 pm
classes and inheritance problems
 
My professor gave us a set of class specifications for three classes. Vehicle, Car, and Truck. My vehicle class works fine, and utilizing it in the other classes is fine as well but I'm having trouble implementing the individual class specifications in the other two. Really the only thing I have to do is somehow say whether or not the car is a race car. I've never worked with boolean functions (just never had to so far) so another problem is that I'm not sure about how to write the driver in main either. Any help would be appreciated.

Code posted below. I'm going one class at a time so the truck class is empty but I'll fill that in once Car is taken care of. Any input anywhere though would be very helpful. Thanks in advance.

  1. #include<iostream>

  2. using namespace std;

  3. class Vehicle

  4. {

  5. public:

  6. Vehicle();

  7. ~Vehicle();

  8. void setAge(int i){Age = i;};

  9. void setPrice(float i) {Price = i;};

  10. int getAge()const;

  11. float getPrice()const;

  12.    

  13. private:

  14.         int Age;

  15. protected:

  16.         float Price;

  17. };

  18. Vehicle::Vehicle()

  19. {

  20.         Age = 0;

  21.         Price = 0;

  22. }

  23. Vehicle::~Vehicle()

  24. {

  25.         Age = 0;

  26.         Price = 0;

  27. }

  28. int Vehicle::getAge() const

  29. {

  30.         return Age;

  31. }

  32. float Vehicle::getPrice() const

  33. {

  34.         return Price;

  35. }

  36. class Car : public Vehicle

  37. {

  38. public:
          bool RaceCarStatus()const;  //boolean, yes or no

  39.   Car();  //default constructor, sets RaceCarStatus=false

  40.   //need copy constructor, takes one parameter of class car

  41.   ~Car();                    //destructor

  42.   void setRaceCarStatus(int i);//takes boolean parameter, returns nothing

  43.   bool getRaceCarStatus()const;//no parameters, returns status

  44. };

  45. Car::Car()

  46. {

  47.         int i;

  48.         i = 0;

  49. }

  50. Car::~Car()

  51. {

  52.         int i;

  53.         i = 0;

  54. }

  55. void Car::setRaceCarStatus(int i)

  56. {

  57.         RaceCarStatus = i;

  58. }

  59. bool Car::RaceCarStatus()

  60. {

  61.         int i;

  62.         if (i = 1)

  63.                 return true;

  64.         else return false;

  65. }

  66. bool Car::getRaceCarStatus() const

  67. {

  68.         return RaceCarStatus;

  69. }

  70. class Truck : public Vehicle

  71. {

  72.         //DieselTypeStatus, boolean, private, yes or no

  73.         //default constructor, set DieselTypeStatus=false

  74.         //parametric constructor, takes boolean,sets DieselTypeStatus=boolean

  75.         //destructor, DieselTypeStatus=false

  76.         //setDieselTypeStatus() takes boolean parameter, return nothing

  77.         //getDieselTypeStatus() no parameters, return DieselTypeStatus

  78. };

  79. int main()

  80. {

  81. Vehicle x;

  82. cout << "Initial value for x: " << endl;

  83. cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;

  84. x.setAge(40);

  85. x.setPrice(20000);

  86. cout << "Modified value for x: " << endl;

  87. cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;

  88. cout << endl;

  89. Car y;

  90. cout << "Initial value for y: " << endl;

  91. cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;

  92. y.setAge(70);

  93. y.setPrice(130);

  94. cout << "Modified value for y: " << endl;

  95. cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;

  96. cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;

  97. //y.setRaceCarStatus(1);

  98. //cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;

  99. cout << endl;

  100. Truck z;

  101. cout << "Initial value for z: " << endl;

  102. cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;

  103. z.setAge(10);

  104. z.setPrice(10000);

  105. cout << "Modified value for z: " << endl;

  106. cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;

  107. return 0;

  108. }


John A Feb 6th, 2008 9:47 pm
Re: classes and inheritance problems
 
bool Car::RaceCarStatus()
{
        int i;
        if (i = 1)
                return true;
        else return false;
}
Why the heck is
RaceCarStatus
a function? Just do something like
bool RaceCarStatus;
in your class definition.

Natique Feb 6th, 2008 9:54 pm
Re: classes and inheritance problems
 
Ok I looked through your program, and I'm not sure I understood your question, but either way these comments should help a little.

57. RaceCarStatus = i;

You didn't define RaceCarStatus as a variable at any time. I think you meant to put it in the definition of Car.

Secondly,
line 62 I think you mean if (i==1)
the way it currently is, it will always return 1. However even after you change it, you'll have a problem. the code will look like this:
bool Car::RaceCarStatus()
{        int i;       
if (i == 1)               
return true;       
else return false;}
here you haven't assigned a value to i.
Hope this helps a little!

robotnixon Feb 6th, 2008 11:49 pm
Re: classes and inheritance problems
 
So RaceCarStatus is no longer a function, and along with a couple minor changes it compiles. However I'm getting the same result for RaceCarStatus before and after input. Part of the spec is having a copy constructor which may be affecting the result but I'm not sure what it does in this particular program. Here's the modified code. Thanks so far everybody.

  1. #include<iostream>

  2. using namespace std;

  3. class Vehicle

  4. {

  5. public:

  6.         Vehicle();          //constructor, sets Age and Price to 0

  7.         ~Vehicle();        //destructor, sets Age andPrice to 0

  8.         void setAge(int i){Age = i;}; //takes integer and sets Age to int value

  9.         void setPrice(float i) {Price = i;};//takes float value, sets Price to value

  10.         int getAge()const;

  11.         float getPrice()const;

  12.    

  13. private:

  14.         int Age;

  15. protected:

  16.         float Price;

  17. };

  18. Vehicle::Vehicle()

  19. {

  20.         Age = 0;

  21.         Price = 0;

  22. }

  23. Vehicle::~Vehicle()

  24. {

  25.         Age = 0;

  26.         Price = 0;

  27. }

  28. int Vehicle::getAge() const

  29. {

  30.         return Age;

  31. }

  32. float Vehicle::getPrice() const

  33. {

  34.         return Price;

  35. }

  36. class Car : public Vehicle

  37. {

  38. public: bool RaceCarStatus;//boolean, yes or no

  39.         Car();                  //default constructor, sets RaceCarStatus=false

  40.                     //need copy constructor, takes one parameter of class car

  41.         ~Car();              //destructor

  42.         void setRaceCarStatus(bool);//takes boolean parameter, returns nothing

  43.         bool getRaceCarStatus()const;//no parameters, returns status

  44. };

  45. Car::Car()

  46. {

  47.         int i;

  48.         i = 0;

  49. }

  50. Car::~Car()

  51. {

  52.         int i;

  53.         i = 0;

  54. }

  55. void Car::setRaceCarStatus(bool)

  56. {

  57.         RaceCarStatus = false;

  58. }

  59. bool Car::getRaceCarStatus() const

  60. {

  61.         int i;

  62.         if (i = 1)

  63.                 return  true;

  64.         else return false;

  65. }

  66. class Truck : public Vehicle

  67. {

  68.         //DieselTypeStatus, boolean, private, yes or no

  69.         //default constructor, set DieselTypeStatus=false

  70.         //parametric constructor, takes boolean,sets DieselTypeStatus=boolean

  71.         //destructor, DieselTypeStatus=false

  72.         //setDieselTypeStatus() takes boolean parameter, return nothing

  73.         //getDieselTypeStatus() no parameters, return DieselTypeStatus

  74. };

  75. int main()

  76. {

  77. Vehicle x;

  78. cout << "Initial value for x: " << endl;

  79. cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;

  80. x.setAge(40);

  81. x.setPrice(20000);

  82. cout << "Modified value for x: " << endl;

  83. cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;

  84. cout << endl;

  85. Car y;

  86. cout << "Initial value for y: " << endl;

  87. cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;

  88. y.setAge(70);

  89. y.setPrice(130);

  90. cout << "Modified value for y: " << endl;

  91. cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;

  92. cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;

  93. y.setRaceCarStatus(1);

  94. cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;

  95. cout << endl;

  96. Truck z;

  97. cout << "Initial value for z: " << endl;

  98. cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;

  99. z.setAge(10);

  100. z.setPrice(10000);

  101. cout << "Modified value for z: " << endl;

  102. cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;

  103. return 0;

  104. }


VernonDozier Feb 7th, 2008 5:01 am
Re: classes and inheritance problems
 
You only have one data member in the Car class, right? That variable is called "RaceCarStatus" and it is a boolean type? RaceCarStatus is true if it is a race car and false otherwise, correct? As far as I can tell there is no data member called "i".

You use the integer variable i a lot and seem to make it 0 or 1. In C, where there is no boolean type, that's a common way to do things (0 for false, 1 for true), but in C++ there is a boolean type so you don't need to do that. Given that, unless I am missing something, there is no "i" data member of any class, it seems to me that a function like this:

Car::Car()
{
  int i;
  i = 0;
}

does nothing. If the Car constructor's job is to set RaceCarStatus to false, then the constructor should be this:
Car::Car()
{
    RaceCarStatus = false;
}

I'd change this function:
bool Car::getRaceCarStatus() const
{
    int i;
    if (i = 1)
    return true;
    else return false;
}
to this:
bool Car::getRaceCarStatus() const
{
    return RaceCarStatus;
}
This function:
void Car::setRaceCarStatus(bool)
{
    RaceCarStatus = false;
}
has a bool in its prototype but no variable name to go with it. Anything you try to pass to this function will have no effect since it results in RaceCarStatus being assigned to false regardless. Either change the function so it does something with a parameter that is passed to it or make it take no parameters.

robotnixon Feb 7th, 2008 7:56 pm
Re: classes and inheritance problems
 
Made some changes with VernonDozier's recommendations (thanks for all the information) but still getting a logic error with setRaceCarStatus My specs have it as taking a boolean parameter and returning nothing, so I've tried putting an if-else statement but it doesn't seem to be working since the answer is always true. Here's the relevant code section:

void Car::setRaceCarStatus(bool)
{

if(true)
RaceCarStatus = true;
else if (false)
RaceCarStatus = false;
}


And here's the call in main



cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;
y.setRaceCarStatus(true);
cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;


I've played with it for about an hour but can't get it to work right.

John A Feb 7th, 2008 8:14 pm
Re: classes and inheritance problems
 
It's actually pretty simple. Try something like
void Car::setRaceCarStatus(bool newStatus)
{
  RaceCarStatus = newStatus;
}


All times are GMT -4. The time now is 10:14 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC