Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Develop a class called Date which will involve a variety of operator overloads. A Date object will
store a date in terms of day of month, month and year. You will overload some basic operators for use
with these objects, including arithmetic, comparison, and insertion/extraction (I/O) operators.

Details and Requirements:
the in February is not possible. You should create appropriate member data
in your class. All member data must be private (you will get a zero otherwise).
ii. [5 marks]Constructors:
• The class should have a default constructor (no parameters), which should initialize
the Date as 1-1-1900
• The Date class should also have a constructor with 3 parameters representing day of
month, month and year
e.g.
Date d; //creates a date object initialized to 1-1-1900
Date d2(1, 12, 2017)//create a date object corresponding to 1st
iii. Operator Overloading:
December 2017
I. [3 marks] Overload the stream insertion operator << for outputting the Date object
II. [3 marks] Overload the stream extraction operator>> for reading Date object from an
input stream
III. Overload the following comparison operators (<, >, ==, !=) which should return true or
false. [4 marks, 1 for each operator]
Write the main function that would create objects of Date and demonstrates use of all the
operators discussed above. A sample main function is given below:
int main()
{ Date d1,
Date d2(1,2,2017);
cin>>d1;
if(d1<d2)
cout<<”date”<<d1<<”is before="" the="" date="" ”<<d2<<endl;
if(d1="=d2)
cout<<”both" dates="" are="" same”="" <<endl;
="" cout<<d2<<endl;
return="" 0;
}

<b="">What I have tried:

I tried doing it but unfortunately didn't understand it's concept
Posted
Updated 23-May-21 16:00pm

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You must Learn C++ and write a simple class with these constructors.
For the third subtask you must understand operator implementation.
 
Share this answer
 
Here's what you're being asked to implement. This is just a shell, so you have to fill in all the details yourself. That includes adding comments! Good luck.

C++
#include <iostream>
using namespace std;

class Date
{
public:
   Date();
   Date(int day, int month, int year);
   bool operator==(const Date& that) const;
   bool operator!=(const Date& that) const;
   bool operator<(const Date& that) const;
   bool operator>(const Date& that) const;
private:
   int day_;
   int month_;
   int year_;
   static const int DaysPerMonth[12];
};

//  You may find this useful!
//
const int Date::DaysPerMonth[12] =
   {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Date::Date() : year_(1900), month_(1), day_(1) { }

Date::Date(int day, int month, int year) :
   day_(day),
   month_(month),
   year_(year)
{
   //  TBC (to be coded).  What if the date doesn't exist?
}

//  Think about operators that can be implemented just by calling other ones.
//
bool Date::operator==(const Date& that) const
{
   //  TBC.
   //
   return false;
}

bool Date::operator!=(const Date& that) const
{
   //  TBC.
   //
   return false;
}

bool Date::operator<(const Date& that) const
{
   //  TBC.
   //
   return false;
}

bool Date::operator>(const Date& that) const
{
   //  TBC.
   //
   return false;
}

//  Investigate why operator<< and operator>> (when used for output and input)
//  are implemented outside the class, not as members.
//
ostream& operator<<(ostream& stream, const Date& date)
{
   //  TBC.  Display DATE in STREAM.  How will you read Date's private data?
   //
   return stream;
}

istream& operator>>(istream& stream, Date& date)
{
   //  TBC.  Update DATE by reading input from STREAM.  How will you change
   //  Date's private data?  What if the input specifies an invalid date?
   //
   return stream;
}

int main()
{
   Date d1, d2(1,2,2017);

   cin >> d1;

   if(d1 < d2)
      cout << d1 << " is before " << d2;
   else if(d1 > d2)
      cout << d2 << " is before " << d1;
   else
      cout << "The dates are equal.";

   cout << endl;
   return 0;
}
 
Share this answer
 
Usually date classes and libraries work by counting days from a given starting year. The year 1600 is sometimes used. With 64-bit integers you can use practically any year you want to, such as zero. To implement the operators, you first determine how many days you have from the start for each value and then you can compare those. As an example, let's say 2020 was the starting year. Today is the 143 day of this year so it is 409 days since the starting date because 2020 was a leap year. You might be asked to compare today with the date 1 January, 2021. It would be day 366 so you end up comparing 366 with 409. This is what your implementations of the operators will have to do.

You will probably want to write yourself a function that determines whether you have a leap year or not. Here is the wikipedia page on the topic that includes some pseudo-code for the algorithm : Leap year - Wikipedia[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900