Postfix Operator help.

So i believe i got my prefix working, now i'm trying to get my postfix working so that the class object happy should have its day increased by 1. Not sure whats wrong. Maybe the way it receives the month and day in the constructor?

main.cpp
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
#include <iostream>
#include <string>

#include "DayOfYear.h"
using namespace std;

void print(int);
int main()
{
	DayOfYear dYear, happy;
	int day;
	string month;

    do{
	cout << "\nEnter the day number and Enter 911 to stop the program";
	cout << endl << "Day Entered: ";
	cin >> day;
    dYear.setDay(day);
    if(day==911){
        cout << "The Program has Ended" << endl;
    }
    
	//++dYear;
	//dYear.print(day);
	
    //Demonstrating overloaded prefix ++ operator
    cout << "Demonstrating overlaoded prefix ++ operator.\n";
    
    ++happy;
    cout<< happy.getDay();
    happy.print(day);
    
    //Demonstrating overloaded postfix ++ operator
    cout << "\nDemonstrating overloaded postfix ++ operator.\n";
    happy++;
    cout<< happy.getDay();
    happy.print(day);

    }while(day!=911);


	return 0;

}
//Send entered day number to the print() function
void DayOfYear::print(int day)
{
	int index = 0;
    
	while (DayOfYear::MonthDay[index] < day)
		index = (index + 1) %12;

		//Display month and day
		cout << DayOfYear::Month[index] << " " << day - DayOfYear::MonthDay[index-1];
};


DayOfYear.cpp
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
#include "DayOfYear.h"
#include <cmath>
#include <iostream>
using namespace std;

//Set days of each month into an array
const int DayOfYear::MonthDay[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

//Set the name of each month into an array
const string DayOfYear::Month[] = {"January", "February", "March",
                                    "April", "May", "June",
                                    "July", "August", "September",
                                    "October", "November", "December"};

//************************************************
// Overloaded prefix ++ operator. Causes the     *
// inched member to be incremented. Returns the  *
// incremented  object.                          *
//************************************************
DayOfYear DayOfYear::operator++()
{
    ++day;
    simplify();
    return *this;
}


//*************************************************
// Overloaded postfix ++ operator. Causes the     *
// inches member to be incremented. Returns the   *
// value of the object before the increment.      *
//*************************************************
DayOfYear DayOfYear::operator++(int)
{
    DayOfYear temp(months, day);
    day++;
    simplify();
    //cout<<day;
    return temp;
	}

//*******************************************************
// Overloaded postfix -- operator.					  *
//*******************************************************
DayOfYear DayOfYear::operator--(int)
{
	string month;
	DayOfYear temp(month, day);
	day--;
	if (day == -1) // <-------------- fix to ... if (day ==-1)
	{ day = day + 365; }
	return temp;
}
void DayOfYear::simplify()
{
    string month;

	//Incrementors
	for(int index=0;index<12;index++){
	month=DayOfYear::Month[index];
	if (month== "April" ||month== "June" || month=="September" ||month== "November" && day > 30)
	{
		month += (day / 30);
		day = (day % 30);
	}
	else if ( month =="January"||month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October" && day > 31)
	{
		month += (day / 31);
		day = day % 31;
	}
	else if ( month == "Feburary" && day>28 )
	{
		month += (day / 28);
		day = day % 28;
	}

	else if (month == "December" && day > 31)
	{
		month = (day / 31);
		day = day % 31;
	}
	}
}
/*
	//Decrementors
	else if (month== "April" ||month== "June" || month=="September" ||month== "November" && day < 1)
	{
		month -= ((abs(day) / 30) + 1);
		day = day - (abs(day) % 30);
	}
	else if (month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October"||month == "December" && day < 1)
	{
		month -= ((abs(day) / 31) +1);
		day = 31 - (abs(day) % 31);
	}
	else if (month == "Feburary" && day < 1)
	{
		month -= ((abs(day) / 28) +1);
		day = 28 - (abs(day) % 28);
	}
	else if (month =="January" && day < 1)
	{
		month -= ((abs(day) / 31) + 1);
		day = 31 - (abs(day) % 31);
	}

 }
}
*/


DayOfYear.h
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

#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H

#include <string>
using namespace std;

class DayOfYear
{
    private:
        int day;
        string months;
        static const string Month[12];                            //static member variables
        static const int MonthDay[12];


    public:
    void print(int);
    void simplify();
    void setDay(int d){day = d;}
    int getDay(){return day;}
 

        DayOfYear()
        {
            day = 0;
        }
        DayOfYear(int d)
        {
            day = d;
        }

    DayOfYear (string m,int d)
    {
        months=m;
        day= d;
        simplify();
    }


    // Overloaded operator function.
    DayOfYear operator++();
    DayOfYear operator++(int);
    DayOfYear operator--(int);
};
#endif 
1
2
3
4
5
6
DayOfYear DayOfYear::operator++(int)
{
    DayOfYear temp( *this );
    operator ++();
    return temp;
}
I made the change but doesn't seem to work unless im not using it correctly in the main. Where in the change does it add the day++ cause the day should be the one that increases.
I do not understand what you are talking about. I showed you how the correct postfix operator ++ shall look. If you have some doubts then check your prefix operator ++.
You don't do anything with the values returned by your operators in main, so of course you don't see any difference.
Heres the changes in made in main, for the prefix, is the value in class obj2 suppose be what class obj1 is?

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
#include <iostream>
#include <string>

#include "DayOfYear.h"
using namespace std;

void print(int);
int main()
{
	DayOfYear dYear, happy;
	int day;
	string month;
	
    while(day!=911){
    cout << "\nEnter the day number and Enter 911 to stop the program";
	cout << endl << "Day Entered: ";
	cin >> day;
    dYear.setDay(day);
    if(day==911){
        cout << "The Program has Ended" << endl;
        return 0;
    }
    
	//++dYear;
	dYear.print(day);
	cout<< endl;
	
    //Demonstrating overloaded prefix ++ operator
    cout << "Demonstrating overlaoded prefix ++ operator.\n";
    
    ++happy;
    cout<< "first: ";dYear.print(day);
    cout<< "\tSecond: "; happy.print(happy.getDay());
  
    
    //Demonstrating overloaded postfix ++ operator
    cout << "\nDemonstrating overloaded postfix ++ operator.\n";
    happy=dYear;
    happy++;
    cout<< "first: ";dYear.print(day);
    cout<< "\tSecond: "; happy.print(happy.getDay());

    }
	return 0;
}
//Send entered day number to the print() function
void DayOfYear::print(int day)
{
	int index = 0;
    
	while (DayOfYear::MonthDay[index] < day)
		index = (index + 1) %12;

		//Display month and day
		cout << DayOfYear::Month[index] << " " << day - DayOfYear::MonthDay[index-1];
};
That code still does nothing with the value returned by the operators.

1
2
    cout << (++happy).getDay() ;
    cout << (happy++).getDay() ; 
got the program working, thanks for the help

Thanks vald from moscow for the postfix help, it actually did what it was suppose to just that I didn't quite understand what operators do but now i know.
Last edited on
Topic archived. No new replies allowed.