博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++运算符重载
阅读量:7071 次
发布时间:2019-06-28

本文共 713 字,大约阅读时间需要 2 分钟。

#include<iostream>
using 
namespace std;
class complex{
    
double real,imag;
    
public:
    complex(
double r=
0,
double i=
0){
        real=r;
        imag=i;
    }
    complex 
operator+ (complex &c2);
    
void display();
};
complex complex::
operator+(complex &c2)
{
    complex c;
    c.real=real+c2.real;
    c.imag=imag+c2.imag;
    
return c;
}
void complex::display(){
    cout<<real<<
"
+
"<<imag<<
"
i
"<<endl;
}
int main()
{
    complex a(
1,
2),b(
2,
3),c;
    c=a+b;
    c.display();
}

还有种更简单的方法,重载函数还可以这样写

complex complex::operator+(complex &c2)

{

    return complex(real+c2.real,imag+c2.imag);
}


博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处

对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com


本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2011/12/18/2291605.html
,如需转载请自行联系原作者
你可能感兴趣的文章
《时间的朋友》跨年演讲金句
查看>>
移动前端UI选择
查看>>
SAP MIGO to Cancel Material Doc., Error Msg - Transaction code MBST not defined.
查看>>
食品安全中的那些事故
查看>>
[20150205]关于位图索引7.txt
查看>>
20150213关于共享池4-SQL内存结构父子游标
查看>>
井底之蛙
查看>>
careercup-扩展性和存储限制10.3
查看>>
合并多个工作薄workbooks到一个工作薄workbook
查看>>
公司的一个面试题:如何用css让一个容器水平垂直居中?
查看>>
Linux概念架构的理解(转)
查看>>
.Net 转战 Android 4.4 日常笔记目录
查看>>
Xamarin体验:使用C#开发iOS/Android应用
查看>>
GitLab版本管理(转)
查看>>
SpringMVC源码解析- HandlerAdapter - ModelFactory(转)
查看>>
[20171127]dual.txt
查看>>
JCombobox组合框效果实现(转)
查看>>
提取重复代码不应该只从代码角度,可以从业务角度看看(转)
查看>>
Spring Web工程web.xml零配置即使用Java Config + Annotation
查看>>
英语术语
查看>>