Merge pull request #235 from Falling-in-W-sweetty/patch-6

Update 计数器前后自增.cpp
This commit is contained in:
Francis 2022-10-28 15:33:55 +08:00 committed by GitHub
commit 75cef2c464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
//设计一个计数器counter用类成员重载自增运算符实现计数器的自增用友元重载实现计数器的自减。 //设计一个计数器counter用类成员重载自增运算符实现计数器的自增用友元重载实现计数器的自减。
#include<iostream> #include<iostream>
using namespace std; using namespace std;
class Counter{ class Counter{
@ -17,16 +17,18 @@ Counter Counter::operator++(){
return *this; return *this;
} }
Counter Counter::operator++(int){ Counter Counter::operator++(int){
Counter t=*this;
n++; n++;
return *this; return t;
} }
Counter operator--(Counter &c){ Counter operator--(Counter &c){
--c.n; --c.n;
return c; return c;
} }
Counter operator--(Counter &c,int){ Counter operator--(Counter &c,int){
Counter t=*this;
c.n--; c.n--;
return c; return t;
} }
void Counter::display(){ void Counter::display(){
cout<<"counter number="<<n<<endl; cout<<"counter number="<<n<<endl;