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.

[LIST=1]
[*]#include<iostream>

[*]using namespace std;

[*]class Vehicle
[*]{
[*]public:
[*]	
[*]	
[*] Vehicle();                 
[*] ~Vehicle();                
[*] void setAge(int i){Age = i;}; 
[*] void setPrice(float i) {Price = i;};
[*] int getAge()const;
[*] float getPrice()const;
[*]     

[*]private:
[*]	int Age;

[*]protected:
[*]	float Price;

[*]};

[*]Vehicle::Vehicle()
[*]{
[*]	Age = 0;
[*]	Price = 0;
[*]}

[*]Vehicle::~Vehicle()
[*]{
[*]	Age = 0;
[*]	Price = 0;
[*]}

[*]int Vehicle::getAge() const
[*]{
[*]	return Age;
[*]}

[*]float Vehicle::getPrice() const
[*]{
[*]	return Price;
[*]}

[*]class Car : public Vehicle
[*]{

[*]public: 
       bool RaceCarStatus()const;  //boolean, yes or no
[*]   Car();  //default constructor, sets RaceCarStatus=false                  
[*]   //need copy constructor, takes one parameter of class car
[*]   ~Car();                     //destructor
[*]   void setRaceCarStatus(int i);//takes boolean parameter, returns nothing
[*]   bool getRaceCarStatus()const;//no parameters, returns status
[*]};

[*]Car::Car()
[*]{
[*]	int i;
[*]	i = 0;
[*]}

[*]Car::~Car()
[*]{
[*]	int i; 
[*]	i = 0;
[*]}

[*]void Car::setRaceCarStatus(int i)
[*]{
[*]	RaceCarStatus = i;
[*]}
[*]bool Car::RaceCarStatus()
[*]{
[*]	int i;
[*]	if (i = 1)
[*]		return true; 
[*]	else return false;
[*]	
[*]}
[*]bool Car::getRaceCarStatus() const
[*]{
[*]	return RaceCarStatus;
[*]}

[*]class Truck : public Vehicle

[*]{
[*]	//DieselTypeStatus, boolean, private, yes or no
[*]	//default constructor, set DieselTypeStatus=false
[*]	//parametric constructor, takes boolean,sets DieselTypeStatus=boolean
[*]	//destructor, DieselTypeStatus=false
[*]	//setDieselTypeStatus() takes boolean parameter, return nothing
[*]	//getDieselTypeStatus() no parameters, return DieselTypeStatus

[*]};


[*]int main()

[*]{

[*]Vehicle x;
[*]cout << "Initial value for x: " << endl;
[*]cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;
[*]x.setAge(40);
[*]x.setPrice(20000);
[*]cout << "Modified value for x: " << endl;
[*]cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;
[*]cout << endl;


[*]Car y;
[*]cout << "Initial value for y: " << endl;
[*]cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;
[*]y.setAge(70);
[*]y.setPrice(130);
[*]cout << "Modified value for y: " << endl;
[*]cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;
[*]cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;
[*]//y.setRaceCarStatus(1);
[*]//cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;
[*]cout << endl;


[*]Truck z;
[*]cout << "Initial value for z: " << endl;
[*]cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;
[*]z.setAge(10);
[*]z.setPrice(10000);
[*]cout << "Modified value for z: " << endl;
[*]cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;


[*]return 0;
[*]}
[/LIST]

Recommended Answers

All 6 Replies

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.

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!

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.

[LIST=1]
[*]#include<iostream>

[*]using namespace std;

[*]class Vehicle
[*]{
[*]public:
[*]	
[*]	
[*]	Vehicle();           //constructor, sets Age and Price to 0
[*]	~Vehicle();        //destructor, sets Age andPrice to 0
[*]	void setAge(int i){Age = i;}; //takes integer and sets Age to int value
[*]	void setPrice(float i) {Price = i;};//takes float value, sets Price to value
[*]	int getAge()const;
[*]	float getPrice()const;
[*]     

[*]private:
[*]	int Age;

[*]protected:
[*]	float Price;

[*]};

[*]Vehicle::Vehicle()
[*]{
[*]	Age = 0;
[*]	Price = 0;
[*]}

[*]Vehicle::~Vehicle()
[*]{
[*]	Age = 0;
[*]	Price = 0;
[*]}

[*]int Vehicle::getAge() const
[*]{
[*]	return Age;
[*]}

[*]float Vehicle::getPrice() const
[*]{
[*]	return Price;
[*]}

[*]class Car : public Vehicle
[*]{

[*]public: bool RaceCarStatus;//boolean, yes or no
[*]	Car();                  //default constructor, sets RaceCarStatus=false                  
[*]		     //need copy constructor, takes one parameter of class car
[*]	~Car();               //destructor
[*]	void setRaceCarStatus(bool);//takes boolean parameter, returns nothing
[*]	bool getRaceCarStatus()const;//no parameters, returns status
[*]};

[*]Car::Car()
[*]{
[*]	int i;
[*]	i = 0;
[*]}

[*]Car::~Car()
[*]{
[*]	int i; 
[*]	i = 0;
[*]}

[*]void Car::setRaceCarStatus(bool)
[*]{
[*]	
[*]	RaceCarStatus = false;
[*]}

[*]bool Car::getRaceCarStatus() const
[*]{
[*]	int i;
[*]	if (i = 1)
[*]		return  true;
[*]	else return false;
[*]}

[*]class Truck : public Vehicle

[*]{
[*]	//DieselTypeStatus, boolean, private, yes or no
[*]	//default constructor, set DieselTypeStatus=false
[*]	//parametric constructor, takes boolean,sets DieselTypeStatus=boolean
[*]	//destructor, DieselTypeStatus=false
[*]	//setDieselTypeStatus() takes boolean parameter, return nothing
[*]	//getDieselTypeStatus() no parameters, return DieselTypeStatus

[*]};


[*]int main()

[*]{

[*]Vehicle x;
[*]cout << "Initial value for x: " << endl;
[*]cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;
[*]x.setAge(40);
[*]x.setPrice(20000);
[*]cout << "Modified value for x: " << endl;
[*]cout << "Age = " << x.getAge() << " Price= " << x.getPrice() << endl;
[*]cout << endl;


[*]Car y;
[*]cout << "Initial value for y: " << endl;
[*]cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;
[*]y.setAge(70);
[*]y.setPrice(130);
[*]cout << "Modified value for y: " << endl;
[*]cout << "Age = " << y.getAge() << " Price= " << y.getPrice() << endl;
[*]cout << "Initial race car status for y: " << y.getRaceCarStatus()<< endl;
[*]y.setRaceCarStatus(1);
[*]cout << "Modified race car status for y " << y.getRaceCarStatus() << endl;
[*]cout << endl;


[*]Truck z;
[*]cout << "Initial value for z: " << endl;
[*]cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;
[*]z.setAge(10);
[*]z.setPrice(10000);
[*]cout << "Modified value for z: " << endl;
[*]cout << "Age = " << z.getAge() << " Price= " << z.getPrice() << endl;


[*]return 0;
[*]}
[*]
[/LIST]

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.

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.

It's actually pretty simple. Try something like

void Car::setRaceCarStatus(bool newStatus)
{
   RaceCarStatus = newStatus;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.