Answer to Question #302049 in C++ for Hassan

Question #302049

In a car race game, when a car object is created it should be filled with fuel, and placed at certain x,y coordinates. Once the game is over, all cars should be deleted ? How do you handle it using c++ classes. Write suitable c++ code


1
Expert's answer
2022-02-24T04:19:34-0500
using namespace std;


/*
	In a car race game, when a car object is created it should be filled with fuel, 
	and placed at certain x,y coordinates. Once the game is over, all cars should be deleted ? 
	How do you handle it using c++ classes. Write suitable c++ code
*/


#define NO_OF_CARS	10
class Car
{
	public:
		int ID;
		int Fuel;
		int x;
		int y;	
		
		void SetCar(int id, int fuel, int X, int Y)
		{
			ID = id;
			Fuel = fuel;
			x = X;
			y = Y;
		}
		// Destructor
    	~Car()
    	{
        	cout << "Destructor is called!\n";
    	}
};


int main()
{
	int x,y,n;
	class Car C[NO_OF_CARS];
	
	x = 0;
	y = 0;
	
	for(n=0;n<NO_OF_CARS;n++)	C[n].SetCar(n,100,x,y);		//Create Objects
	
	delete C;	//Delele all objects
	
	return(0);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog