#include<iostream>
using namespace std;
class MyInteger {
public:
int num;
MyInteger() {
num = 0;
}
MyInteger &operator++() {
num++;//先进行++运算
return *this;//再将自身的值返回
}//int表示的是参数展位,用以区分前置++和后置++
MyInteger operator++(int) {
MyInteger tmp = *this;//先将自身的值返回
num++;//在进行++运算
return tmp;
}
};
ostream& operator<<(ostream &cout,MyInteger integer) {
cout << integer.num ;
return cout;
}
void test01() {
MyInteger integer;
cout << integer << endl;
cout << ++integer << endl;
}
void test02() {
MyInteger integer;
cout << integer << endl;
cout << integer++ << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
黑马-类与对象-递增运算符重载-笔记
最新推荐文章于 2024-09-10 16:09:06 发布