第12周课后实践(1)

来源:互联网 发布:淘宝卖家微淘入口 编辑:程序博客网 时间:2024/05/01 22:10


/*
All rights reserced.
文件名称:main.cpp
作者:李鑫
 完成日期:2016.5.31

问题描述 :阅读程序,写出程序运行结果!

*/

#include<iostream>using namespace std;class Sample{private:    int x;public:  Sample () { }   //1    Sample(int a){x=a;}   // 2    void disp(){cout<<"x="<<x<<endl;}   //3   friend Sample operator+( Sample &s1, Sample &s2);};Sample operator+( Sample &s1, Sample &s2)    //4{return Sample(s1.x+s2.x);}int main(){    Sample obj1(10);   Sample obj2(20);    Sample obj3;    obj3=obj1+obj2;    obj3.disp();    return 0;}

运行结果:


心得:过程为运算符重载,x=10+20=30.

0 0
原创粉丝点击