overload constructors and increment/decrementors with classes

I'm new to C++, currently taking a class and desperately need help.

Here are the programs requirements

Design a class called Date. The class should store a date in three integers: month, day, and year.

Create a member function to display the date in the following format: December 25, 2005

Create both a default constructor and an overloaded constructor. The default one should initialize the date to January 1, 2000, and the overloaded one should take arguments for the month, day, and year.

Create accessors and mutators (gets and sets) for the member variables.

Overload the prefix and postfix ++ operators to increment the date. When a date is set to the last day of the month and incremented, it should become the first day of the following month. If the date is December 31, the incremented date should become January 1 of the following year.

Overload the prefix and postfix -- operators to decrement the date. When a date is set to the first day of the month, and decremented, it should become the last day of the previous month. If the date is January 1, the decremented date should become December 31 of the previous year.

Overload the stream insertion operator (<<) to display the date in the format: December 25, 2005.

Overload the stream extraction operator (>>) to prompt the user for a date to be stored.

Design a class called FutureDate that is derived from the Date class you have created. This class should not allow the user to store a date earlier than today's date. If the user enters a date earlier than today, display a message and let the user re-enter the date.

Create only one constructor, an overloaded constructor that accepts values for the month, day, and year, in that order, and initializes the object to that date. The constructor should prompt the user if the date is before today's date.

The decrement operators (prefix and postfix) should be overridden. Do not allow the date to be decremented so it is before today's date. Just keep the current date if decrementing it would put it in the past.

The following code snippet displays today's date.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <ctime>
#include <iostream>
using namespace std;

int main() 
{
    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    cout << (now->tm_year + 1900) << '-' 
         << (now->tm_mon + 1) << '-'
         <<  now->tm_mday
         << endl;
}

Demonstrate the classes by using the provided driver program, This file should not need to be modified except where commented. If your classes don't work with the driver, modify your classes to work with it.

Input Validation: Do not accept values for the day greater than the number of days in the current month, or less than 1. Do not accept values for the month outside of the values 1-12.

here is the code I'm required to use

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <cstdlib>
#include <iostream>
#include"Date.h"
#include"FutureDate.h"
using namespace std;

int main()
{
    int menuChoice; //used for menu
    int tempMonth;
    int tempDay;
    int tempYear;
    cout<<"Creating Date object with default constructor..."<<endl;
    Date date1;
    cout<<"Stream insertion operator..."<<endl;
    cout<<date1<<endl;
    cout<<"\nStream extraction operator..."<<endl;
    cin>>date1;
    cout<<"Stream insertion operator..."<<endl;
    cout<<date1<<endl;
    
    //Ask user to enter data for next 2 objects
    cout<<"\n\nYou will be asked to enter a date to be used for the next 2 object."<<endl;
    cout<<"Try entering months and days outside the valid range."<<endl;
    cout<<"This program does not try to validate leap years."<<endl;
    cout<<"\n  Enter a month: ";
    cin>>tempMonth;
    cout<<"  Enter a day: ";
    cin>>tempDay;
    cout<<"  Enter a year:";
    cin>>tempYear;
    
    cout<<"\nCreating a Date object with the overloaded constructor..."<<endl;
    Date date2(tempMonth, tempDay, tempYear);
    cout<<"Stream insertion operator..."<<endl;
    cout<<date2<<endl;
    
    cout<<"\nCreating a FutureDate object using the overloaded constructor..."<<endl;    
    FutureDate aFutureDate(tempMonth, tempDay, tempYear);
    cout<<"Stream insertion operator..."<<endl;
    cout<<aFutureDate<<endl;
    
    cout<<"\n\nYou will now use the in/decrement operators on the FutureDate"<<endl;
    cout<<"Press (1) to use the increment operator"<<endl;
    cout<<"Press (2) to use the decrement operator"<<endl;
    cout<<"Press (0) to exit"<<endl;
    cin>>menuChoice;
    while(menuChoice)
    {
        switch(menuChoice)
        {
        case 1:
            aFutureDate++;
            cout<<aFutureDate<<endl;
            break;
        case 2: 
            aFutureDate--;
            cout<<aFutureDate<<endl;
        }
        cout<<"Press (1) to use the increment operator"<<endl;
        cout<<"Press (2) to use the decrement operator"<<endl;
        cout<<"Press (0) to exit"<<endl;
        cin>>menuChoice;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


I am struggling a lot, I don't even know if my header file is right cause my every time i try to compile i get an error Date.h not found.

here's my header file
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
33
34
35
36
37
38
39
40
41
42
43
#ifndef DATE_H
#define DATE_H

#include<iostream>
using namespace std;

class Date
{
private:
	int day_;
	int month_;
	int year_;
public:
	Date();
	Date(const int& d, const int& m, const int& y);

bool valid(void)const;

int day() const;
int month() const;
int year() const;

void set_day(const int& day);
void set_month(const int& month);
void set_year(const int& year);

Date operator ++();// prex
Date operator ++(int);// postx
Date operator --();// prex
Date operator--(int);// postx
};


bool operator == (const Date&, const Date&);// comparison operators
bool operator != (const Date&, const Date&);
bool operator < (const Date&, const Date&);
bool operator > (const Date&, const Date&);
bool operator <= (const Date&, const Date&);
bool operator >= (const Date&, const Date&);

ostream& operator << (ostream& os, const Date& d); // output operator

#endif 



I dont know why it wont access my header file, I wrote it the exact way my book explained it.
Last edited on
Topic archived. No new replies allowed.