mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-03 02:24:11 +08:00
Update 计数器前后自增.cpp
This commit is contained in:
committed by
GitHub
parent
9f58fef2ce
commit
374867b3f0
@@ -1,4 +1,4 @@
|
||||
//设计一个计数器counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。
|
||||
//设计一个计数器counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class Counter{
|
||||
@@ -17,16 +17,18 @@ Counter Counter::operator++(){
|
||||
return *this;
|
||||
}
|
||||
Counter Counter::operator++(int){
|
||||
Counter t=*this;
|
||||
n++;
|
||||
return *this;
|
||||
return t;
|
||||
}
|
||||
Counter operator--(Counter &c){
|
||||
--c.n;
|
||||
return c;
|
||||
}
|
||||
Counter operator--(Counter &c,int){
|
||||
Counter t=*this;
|
||||
c.n--;
|
||||
return c;
|
||||
return t;
|
||||
}
|
||||
void Counter::display(){
|
||||
cout<<"counter number="<<n<<endl;
|
||||
|
||||
Reference in New Issue
Block a user