Runner::Runner()
{
this->name = "?";
this->numHeats = 1;
this->heats = new int[this->numHeats];
for (int i = 0; i < this->numHeats; i++)
{
this->heats[i] = 0;
}
}
As the code tells us, numHeats is used to set the size of the heats array when creating the array.
Over in main, the user will put in the number of heats for the runner. However, I am not allowed to use another constructor, but only the default one, so I can't pass the user input as an argument to the constructor.
So - how can I let the user input decide the size of the heats array, using only the default constructor?
Thanks Disch for pointing out the possibility of passing a parameter to a default constructor that way. However, this seems to be a static parameter value, bringing about the same result as my line "this->numHeats = 1" does, or? Which wouldn't solve my trouble of passing the value given by the user in the main function to the default constructor, for setting the size of the heats array with that value.
If I'm missing your point or misinterpreting your code, bear with me, I am rather new to all this.
It means the parameter is optional, and if it is not given, a value of 1 is used.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class MyClass
{
public:
MyClass(int param = 1);
};
//...
MyClass::MyClass(int param) // default constructor
{
cout << param << endl;
}
//...
int main()
{
MyClass* a = new MyClass; // use default ctor, prints "1"
MyClass* b = new MyClass(5); // passes the optional parameter. prints "5"