第12周-阅读程序(1)
2016-05-16 19:38
381 查看
问题及代码:
运行结果:
![](http://img.blog.csdn.net/20160516192944409?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
知识点总结:
友元类可以存取另一个类的私有成员
operator+重载运算符函数进行想要进行的运算
#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; }
运行结果:
知识点总结:
友元类可以存取另一个类的私有成员
operator+重载运算符函数进行想要进行的运算
相关文章推荐
- 使用BenchmarkSQL测试PostgreSQL(http://blog.sina.com.cn/s/blog_448574810101a276.html)
- HDU 1116.Play on Words【并查集+欧拉图】【5月16】
- HDU 1754 I Hate It
- (8)绝不在构造和析构过程中调用virtual函数
- close()和shutdown()函数
- Sublime Text (3) for PHP Developers
- PHP下foreach的作用域,对数组的操作影响
- scrollview嵌套viewpager不能上下滑动的问题
- 并查集的解题集合
- (7)别让异常逃离析构函数
- Kali Rolling 2016.1 安装无线网卡驱动
- LeetCode题解:345. Reverse Vowels of a String
- ACM新手水题解答
- Mybatis中#和$的区别
- Android NDK总结
- PAT 天梯赛 L2-002 链表去重
- (6)为多态基类声明virtual析构函数
- android studio文件名颜色分别表示含义
- shell脚本的使用---特殊变量及脚本的综合使用
- 二维数组及冒泡排序