Help with Postfix++ and Prefix++

I need some help with prefix and postfix.

I had a program which basically takes in a number 1 - 365 and converts it into month and day in which i had no problem. Now this is a second program which takes in a month and a day and convert it to a number 1 - 365 (opposite of the other program). The task says to use prefix++ and postfix++ (which i dont know anything about) to solve it. Please give me at least some idea how solve this problem

Here is the program that i have previously written ( What should be added to retrieve the number according to the entered month and day?? ):

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//Main.cpp

#include <iostream>
#include <cstdlib>

#include "DayOfYear.h"

using namespace std;

int main()
{
    int _day;
    // choose a day to print
    cout << "Please enter a number from 1 to 365" << endl;
    cin >> _day;
    
    DayOfYear d(_day);
    // print day 78 in Month and Day format
    d.print();
    
    system("pause");
    return 0;
    
}

//DayOfYear.h

#include <iostream>

#ifndef _MYCLASS_
#define _MYCLASS_


using namespace std;

class DayOfYear{
    
public:
    
    int day;
    string Month;
    
    DayOfYear(int dayEntered);
    
    void print();
};
#endif


//DayOfYear.cpp
#include "DayOfYear.h"

DayOfYear::DayOfYear(int dayEntered){
    
    day = dayEntered;
    
};

void DayOfYear::print()
{
    if(day >= 1 && day <= 31)
    {
        cout << "January " << day << endl;
    }
    
    if(day >= 32 && day <= 59)
    {
        cout << "February " << (day - 31) << endl;
    }
    
    if(day >= 60 && day <= 90)
    {
        cout << "March " << (day - 59)  << endl;
    }
    
    if(day >= 91 && day <= 120)
    {
        cout << "April " << (day - 90)  << endl;
    }
    
    if(day >= 121 && day <= 151)
    {
        cout << "May " << (day - 120)  << endl;
    }
    
    if(day >= 152 && day <= 181)
    {
        cout << "June " << (day - 151)  << endl;
    }
    
    if(day >= 182 && day <= 212)
    {
        cout << "July " << (day - 181)  << endl;
    }
    
    if(day >= 213 && day <= 243)
    {
        cout << "August " << (day - 212)  << endl;
    }
    
    if(day >= 244 && day <= 273)
    {
        cout << "September " << (day - 243)  << endl;
    }
    
    if(day >= 274 && day <= 304)
    {
        cout << "October " << (day - 273)  << endl;
    }
    
    if(day >= 305 && day <= 334)
    {
        cout << "November " << (day - 304)  << endl;
    }
    if(day >= 335 && day <= 365)
    {
        cout << "December " << (day - 334)  << endl;
    }
}


Task:
Modify the DayOfYear class, written in Programming Challenge 2, to add a constructor
that takes two parameters: a string representing a month and an integer in the range 0
through 31 representing the day of the month. The constructor should then initialize the
integer member of the class to represent the day specified by the month and day of month
parameters. The constructor should terminate the program with an appropriate error
message if the number entered for a day is outside the range of days for the month given.

Add the following overloaded operators:

++ prefix and postfix increment operators. These operators should modify the
DayOfYear object so that it represents the next day. If the day is already the end of the
year, the new value of the object will represent the first day of the year.

-- prefix and postfix decrement operators. These operators should modify the DayOfYear
object so that it represents the previous day. If the day is already the first day of the year,
the new value of the object will represent the last day of the year.
Last edited on
The goal is to overload 4 operators:

-- prefix (--x)
-- postfix (x--)
++ prefix (++x)
++ postfix (x++)


So that you can do something like this with your class:

1
2
3
4
5
6
7
    DayOfYear d(_day);
    // print day in Month and Day format
    d.print();

    // make 'd' the next day in the year
    ++d;
    d.print();  // <- this should print the following day 



You should have gone over the syntax for operator overloading in class (or else this assignment wouldn't make sense). But as a refresher:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class YourClass
{
public:
    // this first version of the ++ operator has an 'int' parameter
    //   this means it is the postfix (x++) version:
    YourClass operator ++ (int)
    {
        YourClass temp(*this);

        // DO WHATEVER YOU WANT TO INCREASE THE OBJECT HERE

        return temp;
    }

    // this second version of the ++ operator has no parameters
    //   this means it is the prefix (++x) version:
    YourClass& operator ++ ()
    {
        // DO WHATEVER YOU WANT TO INCREASE THE OBJECT HERE

        return *this;
    }
};
For
to add a constructor that takes two parameters: a string representing a month and an integer in the range 0 through 31 representing the day of the month.

you should not have any problems.

I would say
The constructor should terminate the program with an appropriate error
message if the number entered for a day is outside the range of days for the month given.
is not a good thing to do. The constructor does not return any values hence cannot indicate error. To terminate you will have to call something like exit from the constructor itself, which is not a good idea.
I would suggest add a member, something like bool isValid to the class. Have the constructor set this to false if the day entered (for that matter if the month entered as well) is not correct. Pass the string and the number from the user to the constructor, then check for isValid and end the program if its false.

For prefix and postfix operator overloading read http://en.cppreference.com/w/cpp/language/operators (at least the basics and "Increment and decrement" part)
Topic archived. No new replies allowed.