Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1. Write a Date class that has following functionality
▪ A parameterized constructor
▪ IsValid() that checks if the date is valid or not
▪ ShowDate()
▪ GetDate()

Define following overloaded operators for this class:

▪ (>, <, <=, ==, !=) compares two dates and returns a Boolean value

- operator that calculates the number of days between two dates

▪ ++ (postfix) increments date by a day
▪ -- (prefix) decrements date by a day

What I have tried:

C++
class date
{
private:
	int day;
	int month;
	int year;
public:
bool isvalid()const;
bool date::isvalid()const
{
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		if (day >= 1 && day <= 31)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		if (day >= 1 && day <= 30)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else if (month == 2)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			if (day >= 1 && day <= 29)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (day >= 1 && day <= 28)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
}
Posted
Updated 18-Dec-20 13:44pm
v2
Comments
KarstenK 17-Dec-20 10:21am    
you can optimize the code by checking at first for 28, 29 (in february), 30 and 31 days.

 
Share this answer
 
Comments
CPallini 17-Dec-20 9:15am    
5.
In addition Richard's answer. I'm going to show you how to implement the constructor and the overloading of the < operator. The remaining stuff is up to you.
C++
#include <iostream>
#include <memory>
using namespace std;
class Date
{
  int day, month, year;
public:
  Date ( int day, int month, int  year): day(day), month(month), year(year){}
  friend bool operator < ( const Date & da, const Date & db);
  //...
};


bool operator < ( const Date & da, const Date & db)
{
  if ( da.year == db.year)
  {
    if ( da.month == db.month)
    {
      return (da.day < db.day);
    }
    // here da.month != db.month
    return ( da.month < db.month);
  }
  // here da.year != db.year
  return (da.year < db.year);
}


Please note:
  • You mispelled the isValid method name.
  • In the isValid implementation you missed to check the correctness of both the year and the month values.
 
Share this answer
 
You need to convert your year, month, and day into number of days, then all your operators will be one line of code.
Of course if you track the hours minutes and seconds it will have to be converted to the lowest denominator i.e. if you include hours then everything must be converted to hours and so on
 
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