运算符+-*/的重载代码
来源:互联网 发布:苍云成男捏脸数据 编辑:程序博客网 时间:2024/06/10 21:17
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double real = 0,double imag = 0);
Complex operator +(const Complex & c2);
Complex operator -(const Complex & c2);
Complex operator *(const Complex & c2);
Complex operator /(const Complex & c2);
void display(){cout << "real:" << m_real << ",imag" << m_imag << endl;}
private:
double m_real;
double m_imag;
};
Complex::Complex(double real,double imag)
{
m_real = real;
m_imag = imag;
}
Complex Complex::operator +(const Complex & c2)
{
Complex result;
result.m_real = m_real + c2.m_real ;
result.m_imag = m_imag + c2.m_imag;
return result;
}
Complex Complex::operator -(const Complex & c2)
{
Complex result;
result.m_real = m_real - c2.m_real ;
result.m_imag = m_imag - c2.m_imag;
return result;
}
Complex Complex::operator *(const Complex & c2)
{
Complex result;
result.m_real = m_real * c2.m_real - m_imag * c2.m_imag;
result.m_imag = m_real * c2.m_imag + m_imag * c2.m_real;
return result;
}
Complex Complex::operator /(const Complex & c2)
{
Complex result;
double d = c2.m_imag * c2.m_imag + c2.m_real * c2.m_real;
result.m_real = (m_real * c2.m_real + m_imag * c2.m_imag)/d ;
result.m_imag = (-m_real * c2.m_imag + m_imag * c2.m_real)/d;
return result;
}
int main()
{
Complex c1(3,4),c2(4,7);
Complex c3 = c1 + c2 * c2;
Complex c4 = c1 - c2;
Complex c5 = c1 * c2;
Complex c6 = c1 / c2;
c3.display();
c4.display();
c5.display();
c6.display();
system("pause");
return 0;
}
using namespace std;
class Complex
{
public:
Complex(double real = 0,double imag = 0);
Complex operator +(const Complex & c2);
Complex operator -(const Complex & c2);
Complex operator *(const Complex & c2);
Complex operator /(const Complex & c2);
void display(){cout << "real:" << m_real << ",imag" << m_imag << endl;}
private:
double m_real;
double m_imag;
};
Complex::Complex(double real,double imag)
{
m_real = real;
m_imag = imag;
}
Complex Complex::operator +(const Complex & c2)
{
Complex result;
result.m_real = m_real + c2.m_real ;
result.m_imag = m_imag + c2.m_imag;
return result;
}
Complex Complex::operator -(const Complex & c2)
{
Complex result;
result.m_real = m_real - c2.m_real ;
result.m_imag = m_imag - c2.m_imag;
return result;
}
Complex Complex::operator *(const Complex & c2)
{
Complex result;
result.m_real = m_real * c2.m_real - m_imag * c2.m_imag;
result.m_imag = m_real * c2.m_imag + m_imag * c2.m_real;
return result;
}
Complex Complex::operator /(const Complex & c2)
{
Complex result;
double d = c2.m_imag * c2.m_imag + c2.m_real * c2.m_real;
result.m_real = (m_real * c2.m_real + m_imag * c2.m_imag)/d ;
result.m_imag = (-m_real * c2.m_imag + m_imag * c2.m_real)/d;
return result;
}
int main()
{
Complex c1(3,4),c2(4,7);
Complex c3 = c1 + c2 * c2;
Complex c4 = c1 - c2;
Complex c5 = c1 * c2;
Complex c6 = c1 / c2;
c3.display();
c4.display();
c5.display();
c6.display();
system("pause");
return 0;
}
阅读全文
0 0
- 运算符+-*/的重载代码
- 高精度重载运算符的C++代码
- 运算符重载的实现代码
- 运算符重载相关代码
- c++的运算符的重载的代码
- 运算符的重载:[],(),->;
- 运算符的重载
- 运算符的重载
- 运算符的重载
- 运算符的重载
- “[]”运算符的重载
- 运算符的重载
- 运算符的重载
- 运算符的重载
- 运算符的重载
- 重载的<<运算符
- 运算符的重载
- 重载的+运算符
- nodejs之require方法
- chartJS+ajax+ssm简单整合
- 存储设备管理大全,你值得拥有
- 数据库SQL优化大总结之 百万级数据库优化方案
- Spring--java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource问题解决
- 运算符+-*/的重载代码
- maven因为测试类打包报错Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
- 基类与派生类的赋值
- python中random模块基础用法(利用random生成验证码)
- smart ptr 学习理解笔记
- 单例模式
- IE/Firefox/Chrome等浏览器保存Cookie的位置
- tomcat nginx调优
- 理解TCP/IP协议栈(2)