creating a class called date and a child class called time

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
I'm not sure why your would need three constructors.

Edit: Zhuge is right i forgot about the copy constructor


But for the equality operator it would look something like...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class date
{
public:
	bool operator==(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())
	{
               return true;
	}
        else
        return false;
}


You would just have to check all the elements of your class to see if their equal in a similar manner as above.
Last edited on
I imagine the three constructors would be a default constructor, a copy constructor, and a constructor to initialize with given values.
Thank you very much. I will start working on it.
So you know the assignment operator is similar to that of the equality.
Since I'm going to bed I figured i'd post this before I go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//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;
}


Last edited on
By any chance, could you post the finished copy of the program? I'm curious to see what is looks like.
To solve such problem you need some basic understanding of OPP in C++

Search for terms like constructor, destructor, overloading operator and inheritance in C++. Once you have the theory you can do the rest.
Topic archived. No new replies allowed.