qfedu-cpp-level/day5/d2.cpp

42 lines
601 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// () 函数调用重载
// 当类对象作为函数调用时,会执行 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;
}