. h files, or header files, are used to list the publicly accessible instance variables and methods in the class declaration. .cpp files, or implementation files, are used to actually implement those methods and use those instance variables.
Cpp files don't always have to have a header file associated with it but it usually does as the header file acts like a bridge between cpp files so each cpp file can use code from another cpp file. One thing that should be strongly enforced is the no use of code within a header file!
C++ classes (and often function prototypes) are normally split up into two files. The header file has the extension of . h and contains class definitions and functions. The implementation of the class goes into the . cpp file.
The default constructor with argument has a default parameter x, which has been assigned a value of 0.
Defaults should always go in the header file, if the function is declared in a header file.
This is because the compiler will use the header file for ALL compile units that use your class [unless you are being "naughty" and don't use the header file everywhere it should go].
Since the compiler adds default arguments when it compiles the code that CALLS the function (in this case the constructor), it won't matter what the defaults are in the .cpp file.
Of course, in this case, there are only one "user" of the headerfile, and only one place where the constructor is called. But having defaults in the .cpp file is generally wrong [unless it's a local function].
You get very "interesting" bugs if you "mix" defaults - e.g. if your .cpp has one default, and the headefile a different one. If you are really skilled, you can even get the compiler to generate different defaults for different calls to the function, which will almost certainly lead to some headscratching if the code relies on the default being some particular value. And don't be tempted to copy the defaults from the header to the .cpp file "just to make it easier to see". If someone ever changes the default, then it's almost certainly either not going to change in both places, and possibly worse: change the wrong defaults, so it doesn't do what was intended.
This only works because your main function is also in your test.cpp
file, so it sees the default argument specified in your class' implementation. If you put your main
function in a separate file that only includes test.h
, this code will not compile.
Another way of looking at it is, when some other includes test.h
, all that code sees is what is declared in test.h
, so default arguments put elsewhere will not be used.
.h vs. .cpp is a red herring. The rule is that default arguments can be used in function declarations and in the function's definition. You are not allowed to redefine a default argument, not even to the same value. So this is not legal:
void f(int, int = 3);
void f(int, int = 3); // error: redefinition of default argument
However, subsequent declarations can add default arguments:
void f(int, int = 3);
void f(int = 4, int = 3);
f(); // calls f(4, 3);
Further, at any point where the function is called, the default arguments that have been seen at that point can be used:
void f(int, int =3);
f(1); // calls f(1, 3);
void f(int = 4, int = 3);
f(1); // calls f(1, 3);
f(); // calls f(4, 3);
In the original example, the .h file defines one default argument, and any translation unit that uses that header can use that default argument:
Class c3(1, 2, 3);
Class c2(1, 2);
Further, the .cpp file defines an additional default argument, so after that declaration the constructor can be called with one, two, or three arguments:
Class c3(1, 2, 3);
class c2(1, 2);
class c1(1);
There is no such thing as a default parameter for the definition of a file in C++ - it only exists in the declaration.
What happens is the compiler sees a function that is missing the latter parameters. If those are default it can fill in the blanks to construct the object code to call the function as if the function call had those parameters.
PS: Item 38/Scott Myers/Effective C++ - Never redefine an inherited default parameter value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With