add the functions to overload the increment and decrement operators to increase the date by a day and decrease the date by a day, respectively; relational operators to compare two dates; and stream operators for easy input and output. (Assume that the date is input and output in the form MM-DD-YYYY.) Also write a program to test your class.
The file is suppose to read/write to/from a file.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ostream>
#include <ifstream>

using namespace std;

class dateType

{

public:

void setDate(int month, int day, int year);

//Function to set the date.

//The member variables dMonth, dDay, and dYear are set

//according to the parameters.

//Postcondition: dMonth = month; dDay = day; dYear = year


int getDay() const;

//Function to return the day.

//Postcondition: The value of dDay is returned.


int getMonth() const;

//Function to return the month.

//Postcondition: The value of dMonth is returned.


int getYear() const;

//Function to return the year.

//Postcondition: The value of dYear is returned.


void printDate() const;

//Function to output the date in the form mm-dd-yyyy.


dateType(int month = 1, int day = 1, int year = 2000);

//Constructor to set the date

//The member variables dMonth, dDay, and dYear are set

//according to the parameters.

//Postcondition: dMonth = month; dDay = day; dYear = year. If

// no values are specified, the default values are used to

// initialize the member variables.

bool isLeapYear(int);

private:

int dMonth; //variable to store the month

int dDay; //variable to store the day

int dYear; //variable to store the year

};



void dateType::setDate(int month, int day, int year)

{bool error=false;

int max=28;

switch(month)

{ case 1 :

case 3 :

case 5 :

case 7 :

case 8 :

case 10:

case 12: if (day > 31)

error=true;

break;

case 4 :

case 6 :   

case 9 :

case 11: if (day > 30)

error=true;

break;

case 2 : if(isLeapYear(year))

max=29;

if(day>max)

error=true;

}

if(error)

{cout<<"invalid date "<<month<<"/"<<day<<"/"<<year<<" entered. Jan 1, 2000 being used\n";

month=1;

day=1;

year=2000;

}

dMonth = month;

dDay = day;

dYear = year;

}

bool dateType::isLeapYear(int year)

{ if((year%4==0&&year%100!=0)||(year%400!=0))

return true;

return false;

}

int dateType::getDay() const

{

return dDay;

}


int dateType::getMonth() const

{

return dMonth;

}


int dateType::getYear() const

{

return dYear;

}


void dateType::printDate() const

{

cout << dMonth << "-" << dDay << "-" << dYear;

}


//Constructor with parameters

dateType::dateType(int month, int day, int year)

{

setDate(month, day, year);

}

int main()

{dateType d1(2,29,2011);

cout<<"First date is ";

d1.printDate();

cout<<endl;

dateType d2(2,29,2008);

cout<<"Second date is ";

d2.printDate();

cout<<endl;

system("pause");

return 0;

} 

i'm not sure how to fix this. I wrote this code to check for leap year. But i dont want leap year instead i want the above mentioned. Thanks for any and all help.

Recommended Answers

All 2 Replies

It looks like your tutor/lecturer/teacher/mentor is trying to get you to look into operator overloading. Here is a nice starting point: http://msdn.microsoft.com/en-gb/library/5tk49fh2(v=vs.80).aspx

In particular it reads as though you should be aiming to overload '++' and '--' intially and then move on to the relational and stream operators. Once they are overloaded you should be able to achieve something like the following:

dateType myDateTypeInstance;
myDateTypeInstance++; // Increment the 'day' value
myDateTypeInstance--; // Decrement the 'day' value

Since you haven't asked for help with anything in particular I can't really offer any specific advice (DaniWeb encourages people to do their own homework/assignments etc. - its the only way you will learn!). Once you have attempted to wrap your head around the concept of operator overloading I will be more than happy to clarify anything that you may be struggling to understand. If you think that you understand but still can't make much progress with your code then post any attempts that you've made to solve the problem and I'm sure I can point you in the right direction :)

Thanks there. I solved it, finally!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.