space_turtle
space_turtle

Reputation: 3

Constructor with initialized parameter in C++

My instructor requires us to fill out the constructors associated with these prototypes:

YearToMonth(short years=0);
YearToMonth(short years, short months);

My question is: with the 1 parameter constructor, will the value passed in always = 0? I've never seen a function that assigned value to a parameter before. Seems kinda strange to require input when the value is just going to be set to zero but then I wasn't given any documentation on this class so I don't really know what it does yet.

Upvotes: 0

Views: 43

Answers (2)

Xiaotian Pei
Xiaotian Pei

Reputation: 3260

It's default value for the parameter. if you pass your own value to it, it will be set to your value.

Upvotes: 0

Barry
Barry

Reputation: 304112

It's a default argument. Its effect is to provide a default value if the user doesn't explicitly specify one. For example:

YearToMonth();   // calls YearToMonth(short ); with years=0
YearToMonth(2);  // calls YearToMonth(short ); with years=2 

Upvotes: 2

Related Questions