Reputation: 763
My question is based on effect of the sentence void print(const string& msg = "")
,
What difference if compared with void print(const string& msg)
,
When i use the print function in main()
, could I call the function without passing a parameter for function print()
? like p.print()
?
class Point {
int i, j, k;
public:
Point(): i(0), j(0), k(0) {}
Point(int ii, int jj, int kk)
: i(ii), j(jj), k(kk) {}
void print(const string& msg = "") const {
if(msg.size() != 0) cout << msg << endl;
cout << "i = " << i << ", "
<< "j = " << j << ", "
<< "k = " << k << endl;
}
};
int main() {
Point p, q(1,2,3);
p.print("value of p");
q.print("value of q");
} ///:~
Upvotes: 0
Views: 232
Reputation: 13539
Yes, you can do that. it is known as a default value for a parameter. So, calling p.print()
is the same as calling p.print("").
Default value to a parameter while passing by reference in C++ is also relevant reading for you.
Upvotes: 2
Reputation: 258618
Yup, that's a default parameter. If you don't supply one, an empty string will be used.
So, calling p.print()
would be equivalent to calling p.print("")
.
Upvotes: 3