Skip to content

Commit 374867b

Browse files
Update 计数器前后自增.cpp
1 parent 9f58fef commit 374867b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

practical_exercises/10_day_practice/day7/一元运算符/计数器前后自增.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//设计一个计数器counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。
1+
//设计一个计数器counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。
22
#include<iostream>
33
using namespace std;
44
class Counter{
@@ -17,16 +17,18 @@ Counter Counter::operator++(){
1717
return *this;
1818
}
1919
Counter Counter::operator++(int){
20+
Counter t=*this;
2021
n++;
21-
return *this;
22+
return t;
2223
}
2324
Counter operator--(Counter &c){
2425
--c.n;
2526
return c;
2627
}
2728
Counter operator--(Counter &c,int){
29+
Counter t=*this;
2830
c.n--;
29-
return c;
31+
return t;
3032
}
3133
void Counter::display(){
3234
cout<<"counter number="<<n<<endl;

0 commit comments

Comments
 (0)