Reputation: 93
I am defining a constructor in a class that takes in 5 arguments to initialize the fields. Two of those arguments/field come from a Date
structure.
Information(string NewName, Date start, Date finish,
double newNumber, double newLevel);
Is this the proper format for doing so?
Also, how would I go about inputting the arguments in int main ()
? Something like this? How do I go about putting values into the structure arguments? Do they go into quotes?
Information arguments("Susan Jones", "3/5/5", "3/5/5", 15, 58);
Date structure:
struct Date
{
int month;
int day;
int year;
};
Upvotes: 0
Views: 106
Reputation: 66981
The Information
constructor you show looks reasonable. For medium or large objects, you might want to pass by const Date&
instead of just Date
, but for small/simple/fast objects like you have, making copies shouldn't be that big of a deal. Here's the "normal" way:
Information(const string& NewName, const Date& start, const Date& finish,
double newNumber, double newLevel);
However, "3/5/5"
is not the right way to construct an instance of Date
. There's two ways, both shown below. The first is easier to understand, but the second is far less code.
Date start;
start.month = 3;
start.day = 5;
start.year = 5;
Date finish;
start.month = 3;
start.day = 5;
start.year = 5;
Information arguments("Susan Jones", start, finish, 15, 58);
or
Information arguments("Susan Jones", {3,5,5}, {3,5,5}, 15, 58);
Upvotes: 0
Reputation: 71009
It is better to pass const references to non-basic types used in the constructor. So make Data const&
instead of just copies. To be more specific:
Information(const string& NewName,const Date& start,const Date& finish,
double newNumber, double newLevel);
Also assuming that Date has a constructor taking an std::string
or const char*
the way you call the constructor is correct(although it seems you are calling the constructor of another class called Paycheck
).
Otherwise the code seems reasonable.
Upvotes: 1