Operator Overloading Prefix Postfix ++

Hi Guys:

What I am trying to achieve is to create a postfix and prefix overloaded ++ function that increments monthnumber and set name to the name of the next month. If monthnumber is set to 1 when the function execute, they should set monthNumber to 12 and name to "December". Any ideas? Please help me. Thanks in advance.

Months.cpp
//Don't Know if it's a correct prefix ++ operator overloading.
Month Month::operator++()
{ int temp=++monthnumber;
if(temp>12)
{
Month::setMonthNumber(1);
Month::setMonthName("January");
return *this;
}
else
{
Month::setMonthNumber(temp);
return *this;
}
}
//Don't Know if it's a correct postfix ++ operator overloading.
Month Month::operator++(int)
{
//Don't know what to do here.
}

Month.h
class Month
{
private:
string name;
int monthnumber;
public:
Month(string);
Month(int);
const char* getMonthName();
int getMonthNumber();
void setMonthName(string);
void setMonthNumber(int);
Month operator++();
Month operator++(int);
}

Main.cpp

int main()
{
Month m("April");
// Test the prefix ++ operator.
cout << ++m << endl;

// Test the postfix ++ operator.
cout << m++ << endl;
cout << m << endl << endl;
}
Month& operator++ (); // prefix ++
Month operator++ (int); // postfix ++

Source: http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15

Note the '&'. Hope this helps.
Last edited on
Nice it worked. Thanks professor.
Haha ;) No problem
Topic archived. No new replies allowed.