I'm having issues doing this assignment. I'm not really sure what's wrong, so hopefully someone can help

HERE'S THE CODE

#include<iostream>

using namespace std;

class Vehicle
{
	
	private:
		int Age;
	
	protected:
		float Price;

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


	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;
			Car();  
			Car(const Car &copyCar);
			~Car();                     
			void setRaceCarStatus(bool);
			bool getRaceCarStatus()const;
	};

		Car::Car()
		{
			RaceCarStatus = false;
		}

		Car::~Car()
		{
			int i;
			i = 0;
		}
		void Car::setRaceCarStatus(bool)
		{
			RaceCarStatus = false;
		}
		bool Car::getRaceCarStatus() const
		{
			return RaceCarStatus;
		}

		class Truck: 
			public Vehicle
			{
				bool DieselTypeStatus;	
				Truck();				
				void Truck(bool);	
				~Truck();			
				void DieselTypeStatus(bool);	
				bool getDieselTypeStatus()const;
			};

		Truck::Truck()
		{
			DieselTypeStatus = false;
		}
		Truck::~Truck()
		{
			int i;
			i = 0;
		}
		void Truck::setDieselTypeStatus(bool)
		{
			setDieselTypeStatus = false;
		}
		bool Truck::setDieselTypeStatus() const
		{
			return setDieselTypeStatus;
		}

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(5);
	y.setPrice(40000);
	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(25);
	z.setPrice(30000);
	cout << "Modified value for x: " << endl;
	cout << "Age = " << z.getAge() << "Price = " << z.getPrice() << endl;
	
	return 0;
}

HERE'S THE INSTRUCTIONS
We need to use class truck and car as inheriting attributes from vehicle.

Recommended Answers

All 2 Replies

Big question what is the access specifier of all of these?

bool DieselTypeStatus;	//ok
	Truck();				//ok
	void Truck(bool);	       //wrong - no return type
	~Truck();			
	void DieselTypeStatus(bool);	//wrong-duplicate name
	bool getDieselTypeStatus()const;
        [missing - declaration for setDieselTypeStatus(bool)]

You really should describe what your problems are in greater depth. Usually I would just make that comment and quit, but I'm waiting to see if my question gets answered soon, so I'll help

class Truck: 
			public Vehicle
			{
				bool DieselTypeStatus;	
				Truck();				
				[B]void Truck(bool);	[/B]
				~Truck();			
				void DieselTypeStatus(bool);	
				bool getDieselTypeStatus()const;
			};

1. A constructor cannot return a value. This should be Truck(bool); 2. Members of a class must have unique names. You can't have a function and a variable with the same name (DieselTypeStatus). Your implementation uses setDieselTypeStatus(bool) , so the function should be declared this way in the class specification.
3. You are declaring all members of Truck to be private (by omitting the private and public identifiers). Truck() , Truck(bool) , ~Truck() , void setDieselTypeStatus(bool) , bool getDieselTypeStatus()const should all be public

void Truck::setDieselTypeStatus(bool)
        {
            setDieselTypeStatus = false;
        }
        bool Truck::setDieselTypeStatus() const
        {
            return setDieselTypeStatus;
        }

1. The variable is DieselTypeStatus, so change these two places to reflect that.
2. This one should be getDieselTypeStatus ?
3. Shouldn't you be setting DieselTypeStatus to the argument for setDieselTypeStatus(bool)? Right now, the argument is completely ignored (and unnamed).

I fixed these problems in your code, and the program compiled and ran. Fix these mistakes, and see where it goes from there.

Really, you need to be much more careful in writing your code. Make sure you think about what you are doing on every line. Your brain should walk the path your code follows in the same way that the computer will, and, if you are being careful and thoughtful, this will help you find the errors ( both logical errors and compiler/syntax errors ).

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.