Overloading

Overload the prefix increment operator++ (implemented as a friend function) to return the current instance of the class Voter after incrementing the nr_times_voted by 1. Use the following prototype:

Voter operator++(Voter& V);

I do not understand this at all and it's all I have been given, any help?


First of all the operator-function declaration is not correct enough. It would be better to declare it as

Voter & operator++(Voter& V);

If I have understood your description then the definition can look as

1
2
3
4
5
Voter & operator++(Voter& V)
{
   ++V.nr_times_voted;
   return V;
}  


Also in the class definition there shall be line

1
2
3
4
5
class V
{
   friend Voter & operator++(Voter& V);
   // other declarations
};
Last edited on
Topic archived. No new replies allowed.