classes and inheritance problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

classes and inheritance problems

 
0
  #1
Feb 6th, 2008
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.  
  2. <ol style="list-style-type: decimal"><li>#include<iostream></li>
  3. <li>using namespace std;</li>
  4. <li>class Vehicle</li>
  5. <li>{</li>
  6. <li>public:</li>
  7. <li> Vehicle();</li>
  8. <li> ~Vehicle();</li>
  9. <li> void setAge(int i){Age = i;};</li>
  10. <li> void setPrice(float i) {Price = i;};</li>
  11. <li> int getAge()const;</li>
  12. <li> float getPrice()const;</li>
  13. <li> </li>
  14. <li>private:</li>
  15. <li> int Age;</li>
  16. <li>protected:</li>
  17. <li> float Price;</li>
  18. <li>};</li>
  19. <li>Vehicle::Vehicle()</li>
  20. <li>{</li>
  21. <li> Age = 0;</li>
  22. <li> Price = 0;</li>
  23. <li>}</li>
  24. <li>Vehicle::~Vehicle()</li>
  25. <li>{</li>
  26. <li> Age = 0;</li>
  27. <li> Price = 0;</li>
  28. <li>}</li>
  29. <li>int Vehicle::getAge() const</li>
  30. <li>{</li>
  31. <li> return Age;</li>
  32. <li>}</li>
  33. <li>float Vehicle::getPrice() const</li>
  34. <li>{</li>
  35. <li> return Price;</li>
  36. <li>}</li>
  37. <li>class Car : public Vehicle</li>
  38. <li>{</li>
  39. <li>public:
  40. bool RaceCarStatus()const; //boolean, yes or no</li>
  41. <li> Car(); //default constructor, sets RaceCarStatus=false</li>
  42. <li> //need copy constructor, takes one parameter of class car</li>
  43. <li> ~Car(); //destructor</li>
  44. <li> void setRaceCarStatus(int i);//takes boolean parameter, returns nothing</li>
  45. <li> bool getRaceCarStatus()const;//no parameters, returns status</li>
  46. <li>};</li>
  47. <li>Car::Car()</li>
  48. <li>{</li>
  49. <li> int i;</li>
  50. <li> i = 0;</li>
  51. <li>}</li>
  52. <li>Car::~Car()</li>
  53. <li>{</li>
  54. <li> int i;</li>
  55. <li> i = 0;</li>
  56. <li>}</li>
  57. <li>void Car::setRaceCarStatus(int i)</li>
  58. <li>{</li>
  59. <li> RaceCarStatus = i;</li>
  60. <li>}</li>
  61. <li>bool Car::RaceCarStatus()</li>
  62. <li>{</li>
  63. <li> int i;</li>
  64. <li> if (i = 1)</li>
  65. <li> return true;</li>
  66. <li> else return false;</li>
  67. <li>}</li>
  68. <li>bool Car::getRaceCarStatus() const</li>
  69. <li>{</li>
  70. <li> return RaceCarStatus;</li>
  71. <li>}</li>
  72. <li>class Truck : public Vehicle</li>
  73. <li>{</li>
  74. <li> //DieselTypeStatus, boolean, private, yes or no</li>
  75. <li> //default constructor, set DieselTypeStatus=false</li>
  76. <li> //parametric constructor, takes boolean,sets DieselTypeStatus=boolean</li>
  77. <li> //destructor, DieselTypeStatus=false</li>
  78. <li> //setDieselTypeStatus() takes boolean parameter, return nothing</li>
  79. <li> //getDieselTypeStatus() no parameters, return DieselTypeStatus</li>
  80. <li>};
  81. </li>
  82. <li>int main()</li>
  83. <li>{</li>
  84. <li>Vehicle x;</li>
  85. <li>cout << "Initial value for x: " << endl;</li>
  86. <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li>
  87. <li>x.setAge(40);</li>
  88. <li>x.setPrice(20000);</li>
  89. <li>cout << "Modified value for x: " << endl;</li>
  90. <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li>
  91. <li>cout << endl;
  92. </li>
  93. <li>Car y;</li>
  94. <li>cout << "Initial value for y: " << endl;</li>
  95. <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li>
  96. <li>y.setAge(70);</li>
  97. <li>y.setPrice(130);</li>
  98. <li>cout << "Modified value for y: " << endl;</li>
  99. <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li>
  100. <li>cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;</li>
  101. <li>//y.setRaceCarStatus(1);</li>
  102. <li>//cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;</li>
  103. <li>cout << endl;
  104. </li>
  105. <li>Truck z;</li>
  106. <li>cout << "Initial value for z: " << endl;</li>
  107. <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;</li>
  108. <li>z.setAge(10);</li>
  109. <li>z.setPrice(10000);</li>
  110. <li>cout << "Modified value for z: " << endl;</li>
  111. <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;
  112. </li>
  113. <li>return 0;</li>
  114. <li>}</li>
  115. </ol>
Last edited by robotnixon; Feb 6th, 2008 at 8:13 pm. Reason: looks sloppy
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: classes and inheritance problems

 
0
  #2
Feb 6th, 2008
  1. bool Car::RaceCarStatus()
  2. {
  3. int i;
  4. if (i = 1)
  5. return true;
  6. else return false;
  7. }
Why the heck is RaceCarStatus a function? Just do something like
  1. bool RaceCarStatus;
in your class definition.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: Natique is an unknown quantity at this point 
Solved Threads: 0
Natique Natique is offline Offline
Newbie Poster

Re: classes and inheritance problems

 
1
  #3
Feb 6th, 2008
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:
  1. bool Car::RaceCarStatus()
  2. { int i;
  3. if (i == 1)
  4. return true;
  5. else return false;}
here you haven't assigned a value to i.
Hope this helps a little!
Last edited by Natique; Feb 6th, 2008 at 9:58 pm. Reason: just realised my reply was late
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: classes and inheritance problems

 
0
  #4
Feb 6th, 2008
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. <ol style="list-style-type: decimal"><li>#include<iostream></li>
  2. <li>using namespace std;</li>
  3. <li>class Vehicle</li>
  4. <li>{</li>
  5. <li>public:</li>
  6. <li> Vehicle(); //constructor, sets Age and Price to 0</li>
  7. <li> ~Vehicle(); //destructor, sets Age andPrice to 0</li>
  8. <li> void setAge(int i){Age = i;}; //takes integer and sets Age to int value</li>
  9. <li> void setPrice(float i) {Price = i;};//takes float value, sets Price to value</li>
  10. <li> int getAge()const;</li>
  11. <li> float getPrice()const;</li>
  12. <li> </li>
  13. <li>private:</li>
  14. <li> int Age;</li>
  15. <li>protected:</li>
  16. <li> float Price;</li>
  17. <li>};</li>
  18. <li>Vehicle::Vehicle()</li>
  19. <li>{</li>
  20. <li> Age = 0;</li>
  21. <li> Price = 0;</li>
  22. <li>}</li>
  23. <li>Vehicle::~Vehicle()</li>
  24. <li>{</li>
  25. <li> Age = 0;</li>
  26. <li> Price = 0;</li>
  27. <li>}</li>
  28. <li>int Vehicle::getAge() const</li>
  29. <li>{</li>
  30. <li> return Age;</li>
  31. <li>}</li>
  32. <li>float Vehicle::getPrice() const</li>
  33. <li>{</li>
  34. <li> return Price;</li>
  35. <li>}</li>
  36. <li>class Car : public Vehicle</li>
  37. <li>{</li>
  38. <li>public: bool RaceCarStatus;//boolean, yes or no</li>
  39. <li> Car(); //default constructor, sets RaceCarStatus=false</li>
  40. <li> //need copy constructor, takes one parameter of class car</li>
  41. <li> ~Car(); //destructor</li>
  42. <li> void setRaceCarStatus(bool);//takes boolean parameter, returns nothing</li>
  43. <li> bool getRaceCarStatus()const;//no parameters, returns status</li>
  44. <li>};</li>
  45. <li>Car::Car()</li>
  46. <li>{</li>
  47. <li> int i;</li>
  48. <li> i = 0;</li>
  49. <li>}</li>
  50. <li>Car::~Car()</li>
  51. <li>{</li>
  52. <li> int i;</li>
  53. <li> i = 0;</li>
  54. <li>}</li>
  55. <li>void Car::setRaceCarStatus(bool)</li>
  56. <li>{</li>
  57. <li> RaceCarStatus = false;</li>
  58. <li>}</li>
  59. <li>bool Car::getRaceCarStatus() const</li>
  60. <li>{</li>
  61. <li> int i;</li>
  62. <li> if (i = 1)</li>
  63. <li> return true;</li>
  64. <li> else return false;</li>
  65. <li>}</li>
  66. <li>class Truck : public Vehicle</li>
  67. <li>{</li>
  68. <li> //DieselTypeStatus, boolean, private, yes or no</li>
  69. <li> //default constructor, set DieselTypeStatus=false</li>
  70. <li> //parametric constructor, takes boolean,sets DieselTypeStatus=boolean</li>
  71. <li> //destructor, DieselTypeStatus=false</li>
  72. <li> //setDieselTypeStatus() takes boolean parameter, return nothing</li>
  73. <li> //getDieselTypeStatus() no parameters, return DieselTypeStatus</li>
  74. <li>};
  75. </li>
  76. <li>int main()</li>
  77. <li>{</li>
  78. <li>Vehicle x;</li>
  79. <li>cout << "Initial value for x: " << endl;</li>
  80. <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li>
  81. <li>x.setAge(40);</li>
  82. <li>x.setPrice(20000);</li>
  83. <li>cout << "Modified value for x: " << endl;</li>
  84. <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li>
  85. <li>cout << endl;
  86. </li>
  87. <li>Car y;</li>
  88. <li>cout << "Initial value for y: " << endl;</li>
  89. <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li>
  90. <li>y.setAge(70);</li>
  91. <li>y.setPrice(130);</li>
  92. <li>cout << "Modified value for y: " << endl;</li>
  93. <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li>
  94. <li>cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;</li>
  95. <li>y.setRaceCarStatus(1);</li>
  96. <li>cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;</li>
  97. <li>cout << endl;
  98. </li>
  99. <li>Truck z;</li>
  100. <li>cout << "Initial value for z: " << endl;</li>
  101. <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;</li>
  102. <li>z.setAge(10);</li>
  103. <li>z.setPrice(10000);</li>
  104. <li>cout << "Modified value for z: " << endl;</li>
  105. <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;
  106. </li>
  107. <li>return 0;</li>
  108. <li>}</li>
  109. </ol>
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: classes and inheritance problems

 
1
  #5
Feb 7th, 2008
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:

  1. Car::Car()
  2. {
  3. int i;
  4. i = 0;
  5. }

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

I'd change this function:
  1. bool Car::getRaceCarStatus() const
  2. {
  3. int i;
  4. if (i = 1)
  5. return true;
  6. else return false;
  7. }
to this:
  1. bool Car::getRaceCarStatus() const
  2. {
  3. return RaceCarStatus;
  4. }
This function:
  1. void Car::setRaceCarStatus(bool)
  2. {
  3. RaceCarStatus = false;
  4. }
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 35
Reputation: robotnixon is an unknown quantity at this point 
Solved Threads: 0
robotnixon's Avatar
robotnixon robotnixon is offline Offline
Light Poster

Re: classes and inheritance problems

 
0
  #6
Feb 7th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: classes and inheritance problems

 
0
  #7
Feb 7th, 2008
It's actually pretty simple. Try something like
  1. void Car::setRaceCarStatus(bool newStatus)
  2. {
  3. RaceCarStatus = newStatus;
  4. }
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC