- #include <iostream>
- using namespace std;
- class Sample
- {
- private:
- int x;
- public:
- Sample() {}
- Sample (int a){x=a;}
- void disp(){cout<<"x="<<x<<endl;}
- Sample operator+(const Sample &s); //成员函数
- };
- Sample Sample:: operator+(const Sample &s)//运算符+的重载
- {
- return Sample(x+s.x);
- }
- int main()
- {
- Sample obj1(20);
- Sample obj2(20);
- Sample obj3;
- obj3=obj1+obj2;
- obj3.disp();
- return 0;
- }