|
-
April 19th, 1999, 11:29 AM
#1
Default Constructor
How do you add parameters to a default constructor of a class?
-
April 19th, 1999, 12:37 PM
#2
Re: Default Constructor
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. 
-
April 20th, 1999, 04:54 AM
#3
Re: Default Constructor
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|