C++ 运算符重载小练习

#include <iostream> 
using namespace std;
class MyInt
{
	int nVal;
public:
	MyInt(int n) { nVal = n; }
	// 在此处补充你的代码
	friend int Inc(const MyInt & s)
	{
		return s.nVal + 1;
	}
	MyInt& operator -(int a)
	{
		nVal -= a;
		return *this;
	}
};
int Inc(int n) {
	return n + 1;
}


int main() {
	int n;
	while (cin >> n) {
		MyInt objInt(n);
		objInt - 2 - 1 - 3;
		cout << Inc(objInt);
		cout << ",";
		objInt - 2 - 1;
		cout << Inc(objInt) << endl;
	}
	return 0;
}

其中this指针的使用是个难点

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/82839752