第12周项目1阅读程序写出运行结果1

来源:互联网 发布:mac下面的图标更改 编辑:程序博客网 时间:2024/04/16 07:08
#include <iostream>using namespace std;class Sample{private:    int x;public:    Sample () { }    Sample(int a){x=a;}    void disp(){cout<<"x="<<x<<endl;}    friend Sample operator+( Sample &s1, Sample &s2);//友元函数};Sample operator+( Sample &s1, Sample &s2)  //运算符+重载{    return Sample(s1.x+s2.x);}int main(){    Sample obj1(10);    Sample obj2(20);    Sample obj3;    obj3=obj1+obj2;    obj3.disp();    return 0;}


运行结果:



0 0