Why do we need a post and pre operator overload?

Why do we need a post increment and pre increment operator overload?

In the following code, written by the designers of the course, there are four operator overloads for incrementation and decrementation, ++ and --. They have a post increment and a pre increment one there, and I don't understand what they are for. Why is there a post and pre overload for the operator?
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
#ifndef H_rectangleType
#define H_rectangleType
  
#include <iostream>
using namespace std;

class rectangleType
{
        //Overload the stream insertion and extraction operators
    friend ostream& operator<<(ostream&, const rectangleType &);
    friend istream& operator>>(istream&, rectangleType &);

public:
    void setDimension(double l, double w);
    double getLength() const;
    double getWidth() const;
    double area() const;
    double perimeter() const;

    //Overload the arithmetic operators (part b)
    rectangleType operator + (const rectangleType &) const;
    rectangleType operator - (const rectangleType &) const;
    rectangleType operator * (const rectangleType&) const;

    //Overload the increment and decrement operators (part a)
    rectangleType operator ++ ();          //pre-increment
    rectangleType operator ++ (int);       //post-increment
    rectangleType operator -- ();          //pre-decrement
    rectangleType operator -- (int);       //post-decrement

    //Overload the relational operators (part c)
    bool operator == (const rectangleType&) const;
    bool operator != (const rectangleType&) const;
    bool operator <= (const rectangleType&) const;
    bool operator < (const rectangleType&) const;
    bool operator >= (const rectangleType&) const;
    bool operator > (const rectangleType&) const;

        //constructors
    rectangleType();
    rectangleType(double l, double w);

protected:
    double length;
    double width;
};
#endif 
The following two operations use different operators, because they do different things:

1
2
++i;
i++;

You need one operator to perform the first operation, and a different operator to do the second.

Last edited on
Followup, how would I actually do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

struct GRAPH
{
    int x = 0;
    int y = 0;
    int operator++()
    {
        x = x+1;
        y = y+1;
        return->this;
    }
};


int main()
{
    GRAPH graph1;
    graph1++;
    
    return 0;
}


I wrote an example program to try to figure it out but don't know what I'm doing or what exactly "this" is capable of doing.
Last edited on
Topic archived. No new replies allowed.