定义一个计数器类counter,包含私有成员int n,重载运算符“+”
时间: 2023-05-29 18:04:05 浏览: 88
class Counter {
private:
int n;
public:
Counter() {
n = 0;
}
Counter(int num) {
n = num;
}
int get() const {
return n;
}
void set(int num) {
n = num;
}
Counter operator++() {
++n;
return *this;
}
Counter operator++(int) {
Counter temp = *this;
++(*this);
return temp;
}
Counter operator--() {
--n;
return *this;
}
Counter operator--(int) {
Counter temp = *this;
--(*this);
return temp;
}
Counter operator+(const Counter& c) {
return Counter(n + c.n);
}
Counter operator-(const Counter& c) {
return Counter(n - c.n);
}
bool operator==(const Counter& c) const {
return n == c.n;
}
bool operator!=(const Counter& c) const {
return !(*this == c);
}
bool operator>(const Counter& c) const {
return n > c.n;
}
bool operator<(const Counter& c) const {
return n < c.n;
}
bool operator>=(const Counter& c) const {
return !(*this < c);
}
bool operator<=(const Counter& c) const {
return !(*this > c);
}
};
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)