Reputation: 23
So what I want to do is
initialize my subclass's constructor with my base class's constructor.
this is what my base class constructor looks like.
Appointment::Appointment(string description, int month, int day, int year, int hour, int minute):date(month, day, year){
this-> hour = hour;
this-> minute =minute;
this-> description = description;
}
this is what my subclass constructor looks like
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : Appointment(description, month, day, year, hour, minute){
}
^in my subclass's constructor (daily) there is an error that states that I need to explicitly initialize the member 'date', which does not have a default constructor.
How do I explicitly initialize both 'date' and 'Appointment' in my subclass constructor's initializer list?
Does it look something like this?
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : Appointment(description, month, day, year, hour, minute):Date(month, day, year)
Thanks
Upvotes: 0
Views: 150
Reputation: 45
if Daily class has a (Date) and has an (Appointment) which meanse it's a composition then you have to use the initializer list as shown below
Daily::Daily(string description, int month, int day, int year, int hour, int minute) :
Appointment(description, month, day, year, hour, minute),
Date(month, day, year) {}
but if Daily has an (Appointment) only -composition- and as i can see the Appointment has a (Date) -composition- then the Daily constructor should be like this
Daily::Daily(string description, int month, int day, int year, int hour, int minute) :
Appointment(description, month, day, year, hour, minute) {}
and the Appoiintment parameterized constructor will call the Date parameterized constructor
Upvotes: 0
Reputation: 101
It should be comma separated
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : Appointment(description, month, day, year, hour, minute), Date(month, day, year)
Upvotes: 0
Reputation: 76308
Considering date
and appointment
, using this other question you posted, to be as:
Date date;
Appointment appointment;
You can use this constructor syntax:
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : appointment(description, month, day, year, hour, minute), date(month, day, year) { ... }
Usually in a definition like:
A::A(...) : x(...), y(...), ... {...}
x(...), y(...), ...
is an initializer-list which purpose is to initialize member objects of the class.
Upvotes: 1