当前位置:网站首页>【复数 重载运算符】
【复数 重载运算符】
2022-07-23 20:20:00 【李在奋斗……】
【复数的加法【非重载运算符型】】

#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex() {}
Complex(double r, double i) :real(r), imag(i) {}
Complex add(Complex& c1)
{
Complex c;
c.real = c1.real + real;
c.imag = c1.imag + imag;
return c;
}
void show()
{
cout << "(" << real << "," << imag << ")" << endl;
}
};
int main()
{
Complex c(4, 5), c1(6, 8), c2;
c.show();
c1.show();
c2 = c1.add(c);
c2.show();
return 0;
}

【复数【重载运算符】】

#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex() {}
Complex(double r, double i)
{
real = r;
imag = i;
}
Complex operator+(Complex& c1)
{
Complex c;
c.real = real + c1.real;
c.imag = imag + c1.imag;
return c;
}
void show()
{
cout << "(" << real << "," << imag << ")" << endl;
}
};
int main()
{
Complex c(3, 4), c1(5, 5), c2;
c.show();
c1.show();
c2 = c1.operator+(c);
c2 = c + c1;
c2.show();
c2.show();
}

边栏推荐
- Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)
- 链表——203. 移除链表元素
- Complex data processing of multi subsystem and multi business modules -- project development practice based on instruction set Internet of things operating system
- When using polymorphism, two ideas to judge whether it can be transformed downward
- The latest version of conflict+docker+mysql8 deployment tutorial
- Lingo 基本使用
- 数组——704. 二分查找
- EXCEL的密码相关
- Model loading of assimp Library under QT
- JDK installation package and MySQL installation package sorting
猜你喜欢
随机推荐
关于网段CIDR的笔记
剑指 Offer II 115. 重建序列
121. 买卖股票的最佳时机
如何合理地估算线程池大小
第十一天:续第十天BGP的基本配置
LyScriptTools 扩展Script模块
EXCEL的密码相关
Drools(1):Drools简介
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
Discussion on the usage of scanf () and getchar ()
20. Ref and props
OpenLayers实例-Animated GIF-GIF动画
数组——27. 移除元素
D2Admin框架基本使用
利用ENVI对TROPOMI(哨兵5P)数据预处理
ApplicationContext 介绍
The latest version of conflict+docker+mysql8 deployment tutorial
实践数据湖iceberg 第三十七课 kakfa写入iceberg的 icberg表的 enfource ,not enfource测试
网上开通证券账户安全吗?
Leetcode 219. duplicate Element II exists (yes, resolved)









