| | |
classes and inheritance problems
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li>#include<iostream></li> <li>using namespace std;</li> <li>class Vehicle</li> <li>{</li> <li>public:</li> <li> Vehicle();</li> <li> ~Vehicle();</li> <li> void setAge(int i){Age = i;};</li> <li> void setPrice(float i) {Price = i;};</li> <li> int getAge()const;</li> <li> float getPrice()const;</li> <li> </li> <li>private:</li> <li> int Age;</li> <li>protected:</li> <li> float Price;</li> <li>};</li> <li>Vehicle::Vehicle()</li> <li>{</li> <li> Age = 0;</li> <li> Price = 0;</li> <li>}</li> <li>Vehicle::~Vehicle()</li> <li>{</li> <li> Age = 0;</li> <li> Price = 0;</li> <li>}</li> <li>int Vehicle::getAge() const</li> <li>{</li> <li> return Age;</li> <li>}</li> <li>float Vehicle::getPrice() const</li> <li>{</li> <li> return Price;</li> <li>}</li> <li>class Car : public Vehicle</li> <li>{</li> <li>public: bool RaceCarStatus()const; //boolean, yes or no</li> <li> Car(); //default constructor, sets RaceCarStatus=false</li> <li> //need copy constructor, takes one parameter of class car</li> <li> ~Car(); //destructor</li> <li> void setRaceCarStatus(int i);//takes boolean parameter, returns nothing</li> <li> bool getRaceCarStatus()const;//no parameters, returns status</li> <li>};</li> <li>Car::Car()</li> <li>{</li> <li> int i;</li> <li> i = 0;</li> <li>}</li> <li>Car::~Car()</li> <li>{</li> <li> int i;</li> <li> i = 0;</li> <li>}</li> <li>void Car::setRaceCarStatus(int i)</li> <li>{</li> <li> RaceCarStatus = i;</li> <li>}</li> <li>bool Car::RaceCarStatus()</li> <li>{</li> <li> int i;</li> <li> if (i = 1)</li> <li> return true;</li> <li> else return false;</li> <li>}</li> <li>bool Car::getRaceCarStatus() const</li> <li>{</li> <li> return RaceCarStatus;</li> <li>}</li> <li>class Truck : public Vehicle</li> <li>{</li> <li> //DieselTypeStatus, boolean, private, yes or no</li> <li> //default constructor, set DieselTypeStatus=false</li> <li> //parametric constructor, takes boolean,sets DieselTypeStatus=boolean</li> <li> //destructor, DieselTypeStatus=false</li> <li> //setDieselTypeStatus() takes boolean parameter, return nothing</li> <li> //getDieselTypeStatus() no parameters, return DieselTypeStatus</li> <li>}; </li> <li>int main()</li> <li>{</li> <li>Vehicle x;</li> <li>cout << "Initial value for x: " << endl;</li> <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li> <li>x.setAge(40);</li> <li>x.setPrice(20000);</li> <li>cout << "Modified value for x: " << endl;</li> <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li> <li>cout << endl; </li> <li>Car y;</li> <li>cout << "Initial value for y: " << endl;</li> <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li> <li>y.setAge(70);</li> <li>y.setPrice(130);</li> <li>cout << "Modified value for y: " << endl;</li> <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li> <li>cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;</li> <li>//y.setRaceCarStatus(1);</li> <li>//cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;</li> <li>cout << endl; </li> <li>Truck z;</li> <li>cout << "Initial value for z: " << endl;</li> <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;</li> <li>z.setAge(10);</li> <li>z.setPrice(10000);</li> <li>cout << "Modified value for z: " << endl;</li> <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl; </li> <li>return 0;</li> <li>}</li> </ol>
Last edited by robotnixon; Feb 6th, 2008 at 8:13 pm. Reason: looks sloppy
C++ Syntax (Toggle Plain Text)
bool Car::RaceCarStatus() { int i; if (i = 1) return true; else return false; }
RaceCarStatus a function? Just do something like C++ Syntax (Toggle Plain Text)
bool RaceCarStatus;
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
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:
here you haven't assigned a value to i.
Hope this helps 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:
C++ Syntax (Toggle Plain Text)
bool Car::RaceCarStatus() { int i; if (i == 1) return true; else return false;}
Hope this helps a little!
Last edited by Natique; Feb 6th, 2008 at 9:58 pm. Reason: just realised my reply was late
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.
C++ Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li>#include<iostream></li> <li>using namespace std;</li> <li>class Vehicle</li> <li>{</li> <li>public:</li> <li> Vehicle(); //constructor, sets Age and Price to 0</li> <li> ~Vehicle(); //destructor, sets Age andPrice to 0</li> <li> void setAge(int i){Age = i;}; //takes integer and sets Age to int value</li> <li> void setPrice(float i) {Price = i;};//takes float value, sets Price to value</li> <li> int getAge()const;</li> <li> float getPrice()const;</li> <li> </li> <li>private:</li> <li> int Age;</li> <li>protected:</li> <li> float Price;</li> <li>};</li> <li>Vehicle::Vehicle()</li> <li>{</li> <li> Age = 0;</li> <li> Price = 0;</li> <li>}</li> <li>Vehicle::~Vehicle()</li> <li>{</li> <li> Age = 0;</li> <li> Price = 0;</li> <li>}</li> <li>int Vehicle::getAge() const</li> <li>{</li> <li> return Age;</li> <li>}</li> <li>float Vehicle::getPrice() const</li> <li>{</li> <li> return Price;</li> <li>}</li> <li>class Car : public Vehicle</li> <li>{</li> <li>public: bool RaceCarStatus;//boolean, yes or no</li> <li> Car(); //default constructor, sets RaceCarStatus=false</li> <li> //need copy constructor, takes one parameter of class car</li> <li> ~Car(); //destructor</li> <li> void setRaceCarStatus(bool);//takes boolean parameter, returns nothing</li> <li> bool getRaceCarStatus()const;//no parameters, returns status</li> <li>};</li> <li>Car::Car()</li> <li>{</li> <li> int i;</li> <li> i = 0;</li> <li>}</li> <li>Car::~Car()</li> <li>{</li> <li> int i;</li> <li> i = 0;</li> <li>}</li> <li>void Car::setRaceCarStatus(bool)</li> <li>{</li> <li> RaceCarStatus = false;</li> <li>}</li> <li>bool Car::getRaceCarStatus() const</li> <li>{</li> <li> int i;</li> <li> if (i = 1)</li> <li> return true;</li> <li> else return false;</li> <li>}</li> <li>class Truck : public Vehicle</li> <li>{</li> <li> //DieselTypeStatus, boolean, private, yes or no</li> <li> //default constructor, set DieselTypeStatus=false</li> <li> //parametric constructor, takes boolean,sets DieselTypeStatus=boolean</li> <li> //destructor, DieselTypeStatus=false</li> <li> //setDieselTypeStatus() takes boolean parameter, return nothing</li> <li> //getDieselTypeStatus() no parameters, return DieselTypeStatus</li> <li>}; </li> <li>int main()</li> <li>{</li> <li>Vehicle x;</li> <li>cout << "Initial value for x: " << endl;</li> <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li> <li>x.setAge(40);</li> <li>x.setPrice(20000);</li> <li>cout << "Modified value for x: " << endl;</li> <li>cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;</li> <li>cout << endl; </li> <li>Car y;</li> <li>cout << "Initial value for y: " << endl;</li> <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li> <li>y.setAge(70);</li> <li>y.setPrice(130);</li> <li>cout << "Modified value for y: " << endl;</li> <li>cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;</li> <li>cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;</li> <li>y.setRaceCarStatus(1);</li> <li>cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;</li> <li>cout << endl; </li> <li>Truck z;</li> <li>cout << "Initial value for z: " << endl;</li> <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;</li> <li>z.setAge(10);</li> <li>z.setPrice(10000);</li> <li>cout << "Modified value for z: " << endl;</li> <li>cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl; </li> <li>return 0;</li> <li>}</li> </ol>
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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:
does nothing. If the Car constructor's job is to set RaceCarStatus to false, then the constructor should be this:
I'd change this function:
to this:
This function:
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.
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
Car::Car() { RaceCarStatus = false; }
I'd change this function:
C++ Syntax (Toggle Plain Text)
bool Car::getRaceCarStatus() const { int i; if (i = 1) return true; else return false; }
C++ Syntax (Toggle Plain Text)
bool Car::getRaceCarStatus() const { return RaceCarStatus; }
C++ Syntax (Toggle Plain Text)
void Car::setRaceCarStatus(bool) { RaceCarStatus = false; }
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:
And here's the call in main
I've played with it for about an hour but can't get it to work right.
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.
It's actually pretty simple. Try something like
C++ Syntax (Toggle Plain Text)
void Car::setRaceCarStatus(bool newStatus) { RaceCarStatus = newStatus; }
"Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- Differences Between Java and C/C++ (C++)
- Parent / child class problems (C++)
- Object Oriented Programming (Computer Science)
- I'd like to team up with another beginner (C++)
- Inheritance problems (Java)
Other Threads in the C++ Forum
- Previous Thread: formated file reading problem
- Next Thread: converting numbers
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






