Here is my code:
//---------------------------------------------------------------------------
#pragma hdrstop
#include <tchar.h>
#include <string>
#include <iostream>
#include <sstream>
#include <conio.h>
using namespace std;
//---------------------------------------------------------------------------
class Wheel
{
public:
Wheel()
{
pressure = 32;
ptrSize = new int(30);
}
Wheel(int s, int p)
{
ptrSize = new int(s);
pressure = p;
}
~Wheel()
{
delete ptrSize;
}
void pump(int amount)
{
pressure += amount;
}
private:
int *ptrSize;
int pressure;
};
class RacingCar
{
public:
RacingCar()
{
speed = 0;
Wheel carWheels = new Wheel[3];
}
RacingCar(int s)
{
speed = s;
}
void Accelerate()
{
speed = speed + 10;
}
private:
int speed;
};
I using this code to create a RacingCar object:
RacingCar test();
Yet am getting the following error:
[BCC32 Error] Question 4.cpp(48): E2285 Could not find a match for 'Wheel::Wheel(const Wheel&)'
At line:
Wheel carWheels = new Wheel[3];
I am wanting to create an array of 4 wheels as an array of objects on the heap.
What am I doing wrong?
UPDATE
I am wanting to use a copy constructor for class RacingCar that will create a deep copies of RacingCar objects and then write code to prove that the copy of the RacingCar object is a deep copy.
Can I please have some help to do this?
Here is my code:
class RacingCar
{
public:
RacingCar()
{
speed = 0;
Wheel* carWheels = new Wheel[3];
}
RacingCar(int s)
{
speed = s;
}
RacingCar(const RacingCar &oldObject)
{
//I am not sure what to place here.
Wheel* carWheels = new Wheel[3];
}
void Accelerate()
{
speed = speed + 10;
}
private:
int speed;
};
* 2nd UPDATE
Here is my current code:
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;
}
int getSize()
{
return *ptrSize;
}
int getPressure()
{
return pressure;
}
private:
int *ptrSize;
int pressure;
};
class RacingCar
{
public:
RacingCar()
{
speed = 0;
*carWheels = new Wheel[4];
}
RacingCar(int s)
{
speed = s;
}
RacingCar(const RacingCar &oldObject)
{
for ( int i = 0; i < sizeof(carWheels)/sizeof(carWheels[0]); ++i)
{
Wheel oldObjectWheel = oldObject.getWheel(i);
carWheels[i]=new Wheel(oldObjectWheel.getSize(),oldObjectWheel.getPressure());
}
}
void Accelerate()
{
speed = speed + 10;
}
Wheel getWheel(int id)
{
return *carWheels[id];
}
private:
int speed;
Wheel *carWheels[4];
};
The copy constructor is not working correctly. I am getting an error at:
Wheel oldObjectWheel = oldObject.getWheel(i);
What am I doing wrong?