c++运算符重载
来源:互联网 发布:数据新闻网 编辑:程序博客网 时间:2024/06/15 10:08
#include "iostream.h"
#include "string.h"
class complex //复数类声明
{
public: //外部接口
complex(double r=0.0,double i=0.0) {real=r;imag=i;} //构造函数
complex operator+(complex c2); //+重载为成员函数
complex operator-(complex c2); //-重载为成员函数
void display(); //输出复数
private: //私有数据成员
double real; //复数实部
double imag; //复数虚部
};
complex complex::operator+(complex c2) //重载运算符函数的实现
{
complex c;
c.real=c2.real+real;
c.imag=c2.imag+imag;
return complex(c.real,c.imag);
}
complex complex::operator-(complex c2) //重载运算符-函数实现
{
complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return complex(c.real,c.imag);
}
void complex::display()
{cout<<"("<<real<<","<<imag<<")"<<endl;}
int main()
{complex c1(5,4),c2(2,10),c3; //定义复数类的对象
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
c3=c1-c2; //使用重载运算符完成复数减法
cout<<"c3=c1-c2="; c3.display();
c3=c1+c2; //使用重载运算符完成复数加法
cout<<"c3=c1+c2="; c3.display();
return 0;
}
输出结果:
c1=(5,4)
c2=(2,10)
c3=c1-c2=(3,-6)
c3=c1+c2=(7,14)
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- C++:运算符重载
- C#:运算符重载
- C++:运算符重载
- 我在兰亭这三年之升职
- MKMapView上获取自身当前位置并将地图显示范围控制在自身位置周围
- Hibernate,session.save(tu),报错不能将null值插入列id
- Join array elements depending on the element's index
- PHP 安装memcache后,使用时遇到的错误 解决方法
- c++运算符重载
- ubuntu下创建第一个rails应用程序
- 蓝牙常用的profile
- VEH,VCH,UEF Windows向量化异常处理机制详解
- linux 数学运算
- GLog 使用 vs2010
- 推动互联网金融创新,助力深港经济发展
- Spark1.0.0 分布式环境搭建
- 糯米玉米渣牛奶糕