Click to See Complete Forum and Search --> : Default Constructor


delbert Harry
April 19th, 1999, 11:29 AM
How do you add parameters to a default constructor of a class?

nordyj
April 19th, 1999, 12:37 PM
You declare the parameters as you would any other function, except that the constructor may NOT have a return type.

//In the header file...
class TestParams
{
public:
TestParams(int a, int b); //Constructor
.
.
.
};

In the .cpp file:

#include "TestParams.h"
TestParams::TestParams(int a, int b)
{
.
.
.
}

And to use the variable:

TestParams tp(4, 5);

Hope that was what you were looking for. :)

Dave Lorde
April 20th, 1999, 04:54 AM
A default constructor is one that can be called without passing any arguments.

If you want to add arguments and still have it as the default constructor, each argument must have a default value:

MyClass(int arg1 = 3, string str1 = "hello");

Any other user-defined constructor taking arguments will hide the compiler-provided default constructor, which must be explicitly re-introduced if required.

Dave