1.PNG
    2.PNG

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. //Dividing Line---------------------------------------------------------------------------------------------
    4. class demo
    5. {
    6. public:
    7. int n;
    8. demo(int i = 0) : n(i){};
    9. demo &operator++(void); //前置
    10. demo operator++(int x); //后置
    11. friend demo &operator--(demo &);
    12. friend demo operator--(demo &, int);
    13. };
    14. demo &demo::operator++(void)
    15. {
    16. ++n;
    17. return *this;
    18. }
    19. demo demo::operator++(int x)
    20. {
    21. demo tmp(*this);
    22. n++;
    23. return tmp;
    24. }
    25. demo &operator--(demo &c)
    26. {
    27. --c.n;
    28. return c;
    29. }
    30. demo operator--(demo &c, int useless)
    31. {
    32. demo tmp(c);
    33. c.n--;
    34. return tmp;
    35. }
    36. ostream &operator<<(ostream &os, const demo &c)
    37. {
    38. os << c.n;
    39. return os;
    40. }
    41. //Dividing Line-----------------------------------------------------------------------------------------------
    42. int main(void)
    43. {
    44. demo d(8);
    45. cout << d++ << endl;
    46. cout << d << endl;
    47. cout << ++d << endl;
    48. cout << d << endl;
    49. cout << d-- << endl;
    50. cout << d << endl;
    51. cout << --d << endl;
    52. cout << d << endl;
    53. return 0;
    54. }
    55. /*
    56. 8
    57. 9
    58. 10
    59. 10
    60. 10
    61. 9
    62. 8
    63. 8
    64. */