Hello everyone, I am trying to learn c++ and I am having a very hard time understanding this problem.
I am supposed to create a class called date that has the following requirements:
3 constructors
1 destructor
getMonth
getDay
getYear
setMonth
setDay
setYear
A function print with no arguments
An equality overload
An assignment overload
A copy constructor
For the child time it must have
3 constructors
1 destructor
getHours
getMinutes
getSeconds
setHours
setMinutes
setSeconds
A function print with no arguments
An equality overload
An assignment overload
A copy constructor
I know how to do do the simple code like getHours or getDate or setHours etc. However, I am not exactly sure where to go for the 3 constructors, equality overload, assignment overload and the copy constructor. If anyone can point me in the right direction I would appreciate it. Thanks
class date
{
public:
booloperator==(date& otherDate);
int getDay();
private:
int day;
};
int date::getDay()
{
return day;
}
bool date::operator==(date& otherDate)
{
if(date.getDay() == otherDate.getDay()) //check to see whether the days are equal
//I believe you can also do if(this->date == otherDate.getDay())
{
returntrue;
}
elsereturnfalse;
}
You would just have to check all the elements of your class to see if their equal in a similar manner as above.
//Overloading the assignment operator
date& date::operator=(date& rightObject) // this sets it up so you can return an object of type date
{
if(this != &rightObject) // this just avoids self-assignment
{
day = rightObject.getDay(); //just continue with all your variables basically
}
return *this; //returns your object
}
//Copy Constructor is similiar except you want
date::date(date& otherDate) //constructors dont return anything but this
//will allow you to copy contents of the object otherDate into your new object
{
day = otherDate.getDay();
}
//default constructor
date::date()
{
}
//3rd constructor with paramters
date::date(int dayOfWeek...etc)
{
day=dayOfWeek;
}