Hey guys I've been asked to create an array of objects on the heap for my assignment but I cant seem to find any examples that explain it well enough on the web.

So what I got is:

//Heabder File
class Wheel{

public:
	Wheel() : pressure(32)   {
		 ptrSize = new int(30);
	}

	Wheel(int s, int p) : pressure(p)   {
		ptrSize = new int(s);
	}

	~Wheel() {   delete ptrSize;   }

	void pump(int amount)   {
		pressure += amount;
	}

private:
	int *ptrSize;
    int pressure;
};

Ok so thats the wheel class and I have to coppy the objects into my "RacingCar" class through an Array on the heap.

class RacingCar{
public:

	RacingCar()   {
		speed = 0;
		acceleration = 0;
		direction = 0;
	}

	// Modifiers
	//---------------------------------------------
	void accelerate()   {
		speed = speed + acceleration;
	}

	void setSpeed(int s)   {
		acceleration = s;
	}

	void brake(int braking)   {
		speed = speed - braking;
	}

	void turn(int newDirection)   {
		direction = direction + newDirection;
	}

	//Return Variables
	//---------------------------------------------
	int getSpeed()   { return speed;   }

	int getDirection()   {

		if (direction > 180) 
		{
			direction = -180 + (direction - 180 );
		}

		return direction;
	}

private:
	//Predetermined Variables
	int speed;
	int acceleration;
	int direction;
};

Does Anyone know how to do this?

Thanks :)

Recommended Answers

All 5 Replies

Google "Dynamic Memory Allocation C++"

int bufferSize = 512;
char *buffer = new char[bufferSize];
strcpy(buffer,"This fits in my buffer.");

//When finished you must delete the memory
//or it will be a memory "leak"
delete [] buffer;

Thanks it did me some help but not alot I'm still fairly lost with the construction so far I've gotten this

// lab_environment.cpp : Defines the entry point for the console application.
//
//--------------------------------------------------------------------------
#include "stdafx.h"
//#include <tchar.h>
#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;
//--------------------------------------------------------------------------
//Wheel Class
//--------------------------------------------------------------------------

class Wheel{

public:
	Wheel() : pressure(32)   {
		 ptrSize = new int(30);
	}

	Wheel(int s, int p) : pressure(p)   {
		ptrSize = new int(s);
	}

	~Wheel() {   delete ptrSize;   }

	void pump(int amount)   {
		pressure += amount;
	}

private:
	int *ptrSize;
    int pressure;
};

The "wheel class" above is what needs to be implemented as an array on the heap into the "RacingCar Class" below and then be able to be assigned to the racingcar in main (this code is from the header file).

//--------------------------------------------------------------------------
//Racing Car Class
//--------------------------------------------------------------------------
class RacingCar {
public:

	RacingCar()   {
		speed = 0;
		acceleration = 0;
		direction = 0;
	}

The section Below is what i Have so far but im almost 100% certain I'm overlooking something important as you can see I've assigned it onto the heap and deleted it but I still am not sure if the Wheel Class is being given the data and the objects are recieving it through inheritance OR if its being assigned it directly and the Wheel class has just been given the data

Wheel wheels(int s, int p) {
		
		int wheelAmount = 4;
		int *pWheel = new (nothrow) int(s);

		if (pWheel == 0)
		{
			cout << "Error: memory could not be allocated";
		}
		else
		{
			for (int i = 0; i < wheelAmount; i ++)
			{
				pWheel[i] = p;
				cout << " Wheel 1: " << pWheel[i] << endl;
			}
		}
		delete [] pWheel;

		return Wheel();
	}

Code below is the rest of Racing Car

// Modifiers
	//---------------------------------------------
	void accelerate()   {
		speed = speed + acceleration;
	}

	void setSpeed(int s)   {
		acceleration = s;
	}

	void brake(int braking)   {
		speed = speed - braking;
	}

	void turn(int newDirection)   {
		direction = direction + newDirection;
	}

	//Return Variables
	//---------------------------------------------
	int getSpeed()   { return speed;   }

	int getDirection()   {

		if (direction > 180) 
		{
			direction = -180 + (direction - 180 );
		}

		return direction;
	}
	
private:
	//Predetermined Variables
	
	int speed;
	int acceleration;
	int direction;
};

//--------------------------------------------------------------------------

The section Below is what i Have so far but im almost 100% certain I'm overlooking something important

What exactly is the point of your wheels function? Aside from returning a default constructed object of Wheel in all cases, you don't use the Wheel class at all. Essentially all you're doing is printing the p argument four times in an excessively inefficient manner.

On a side note, I think now is the time to bring up the rule of three. The rule of three at its simplest states that if you have to define one of the following for managing resources, you should provide all of them:

  • Copy constructor
  • Copy assignment operator
  • Destructor

In your Wheel class you need a destructor to release the dynamic memory, so both a copy constructor and a copy assignment operator are highly recommended to avoid memory leaks and shared pointer state during those operations. It might look something like this:

#include <algorithm>

class Wheel {
    friend void swap(Wheel& lhs, Wheel& rhs);
public:
    Wheel();
    Wheel(int size, int pressure);
    Wheel(const Wheel& rhs);
    ~Wheel();

    Wheel& operator=(Wheel rhs);

    void pump(int amount);
private:
    int *ptrSize;
    int pressure;
};

void swap(Wheel& lhs, Wheel& rhs)
{
    std::swap(lhs.pressure, rhs.pressure);
    std::swap(lhs.ptrSize, rhs.ptrSize);
}

Wheel::Wheel()
    : pressure(32), ptrSize(new int(30))
{}

Wheel::Wheel(int size, int pressure)
    : pressure(pressure), ptrSize(new int(size))
{}

Wheel::Wheel(const Wheel& rhs)
    : pressure(rhs.pressure), ptrSize(new int(*rhs.ptrSize))
{}

Wheel::~Wheel()
{
    delete ptrSize;
}

Wheel& Wheel::operator=(Wheel rhs)
{
    swap(*this, rhs);
    return *this;
}

void Wheel::pump(int amount)
{
    pressure += amount;
}

With the coming C++0x standard and move semantics, the rule of three becomes the rule of five, and things start getting more complicated. But the basic rule of three will still work the same way, so I won't get into further details. :)

well basically in the end we need to have the wheels "Act" upon the car and we going to have to add "Track class" which too has to "act" upon the class racingcar which must inherit atributes (im guessing) from the wheels to that you can pump individual wheels and so that the individual stages of the track can also have an effect on the racingcar such as slope, dampness, grip etc.

The wheel class needs to stay as it is, but the racingcar class must contain four wheel objects as defined by the wheel class. The wheels must be implemented as an array.

Then i will write a copy constructor for the racingcar object which makes deep copies of "racingcar" and thats the code done for that section

if you still dont understand I have a class session now and might be able to finish this atleast one section

Hey thanks for your advice guys but I was able to finalise it in class today final code:

// lab_environment.cpp : Defines the entry point for the console application.
//
//--------------------------------------------------------------------------
//#include "stdafx.h"
#include <tchar.h>
#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

//--------------------------------------------------------------------------
class Wheel{

public:
//------------------------------------------------------
//construct data
//------------------------------------------------------
	Wheel() : pressure(32)   {
		 ptrSize = new int(30);
	}

	Wheel(int s, int p) : pressure(p)   {
		ptrSize = new int(s);
	}
	
	//-----------------------------------
	//Copy Constructor
	//-----------------------------------
	/*Wheel (const Wheel &oldWheels)
	{
		ptrSize = new int(30);
		pressure = oldWheels.pressure;
		
	}*/
	
//------------------------------------------------------
//Modify data
//------------------------------------------------------
	void pump(int amount)   {
		pressure += amount;
	}

//------------------------------------------------------
//Return data
//------------------------------------------------------	
	int getPressure() { return pressure; }

//------------------------------------------------------
//Delete Memory space
//------------------------------------------------------
	~Wheel() {   delete ptrSize;   }

private:
	int *ptrSize;
    int pressure;
};

//------------------------------------------------------
class RacingCar{
public:
//------------------------------------------------------
//construct data
//------------------------------------------------------
	RacingCar()
	{
		//Defaults
		topSpeed = 200;
		horsePower = 350;
		torque = 40;

		if (pWheels == 0)
		{
			cout << "Error: memory could not be allocated";
		}
		else
		{
			for (int i = 0; i < 4; i++)
			{
				pWheels[i] = new (nothrow) Wheel();
			}
		}
		
		//Modifiable
		speed = 0;
		acceleration = 0;
		direction = 0;
	}

	//-----------------------------------
	//Copy Constructor
	//-----------------------------------
	RacingCar(const RacingCar &oldCar)
	{
		//constructing copy
		for (int i = 0; i < 4; i++)
		{
			pWheels[i] = new (nothrow) Wheel();
		}
		speed = oldCar.speed;
		acceleration = oldCar.acceleration;
		direction = oldCar.direction;
		topSpeed = oldCar.topSpeed;
		horsePower = oldCar.horsePower;
		torque = oldCar.torque;
	}

//------------------------------------------------------
//Modify data
//------------------------------------------------------
	void accelerate()   {
		speed = speed + acceleration;
	}

	void brake(int braking)   {
		speed = speed - braking;
	}

	void setSpeed(int s)   {
		acceleration = s;
	}

	void turn(int newDirection)   {
		direction = direction + newDirection;
	}

	void wheelPump(int pres, int wheelNum)   {
		pWheels[wheelNum]->pump(pres);
	}
	
//------------------------------------------------------
//Return data
//------------------------------------------------------	
	int getSpeed()   {  return speed;  }

	int getDirection()   {  return direction;  }

	int getTopSpeed()   {  return topSpeed;  }

	int getHorsePower()   {  return horsePower;  }
	
	int getTorque()  {  return torque;  }

	int getWheelPressure(int i) {
		return pWheels[i]->getPressure();  }

//------------------------------------------------------
//Delete  Memory space
//------------------------------------------------------
	~RacingCar()   { delete [] pWheels; }

//------------------------------------------------------

//------------------------------------------------------
//Predefined variables
//------------------------------------------------------
private:

	//wheel decleration
	Wheel *pWheels[4];

	//defaults
	int topSpeed;
	int horsePower;
	int torque;

	//modifications
	int speed;
	int acceleration;
	int direction;
};
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.