Declare the class as such, using either Date
and Time
or const Date &
and const Time &
for the date and time parameters. In general const &
is appropriate for large objects to prevent them from being unnecessarily copied. Small objects can use const &
or not, whichever you prefer.
// Onetime.h
class Onetime {
public:
Onetime(const std::string &description,
const Date &date,
const Time &startTime,
const Time &endTime);
private:
std::string description;
Date date;
Time startTime, endTime;
};
Then define the constructor in your .cpp
file. Use :
to denote an initializer list to initialize the member variables.
// Onetime.cpp
Onetime::Onetime(const std::string &description,
const Date &date,
const Time &startTime,
const Time &endTime)
: description(description),
date(date), startTime(startTime), endTime(endTime)
{
}
Finally, you can create a Onetime
object exactly as you wrote. You could even omit the new
keyword if you wish. new
is for allocating objects on the heap, which you don't always need to do in C++ (unlike Java or C#, say).
schedule[0] = new Onetime("see the dentist",
Date(2013, 11, 4),
Time(11, 30, 0),
Time(12, 30, 0));