qfedu-cpp-level/day5/d2.cpp

42 lines
601 B
C++
Raw Permalink Normal View History

2023-08-14 17:20:39 +08:00
// () 函数调用重载
// 当类对象作为函数调用时,会执行 operator()(参数列表) 函数
//
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class A
{
private:
int x, y;
public:
A(int x, int y) : x(x), y(y) {}
public:
void operator()(int a, int b)
{
this->x += a;
this->y += b;
show();
}
void show()
{
cout << x << ", " << y << endl;
}
};
int main()
{
A a1(10, 9);
a1.show();
a1(2, 3); // 类对象作为函数使用,实现 2+x3+y
// a1.show();
return 0;
}