Complex类的设计与改进

Complex类
源码
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class Complex
{
private:
double real, imaginary;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imaginary(i){};
Complex(const Complex &c);
void add(const Complex m);
void show();
double mod();
};
Complex::Complex(const Complex &c)
{
real = c.real;
imaginary = c.imaginary;
}
void Complex::add(const Complex m)
{
real += m.real;
imaginary += m.imaginary;
}
void Complex::show()
{
cout << real << std::showpos << imaginary << "i" << std::noshowpos << endl;
}
double Complex::mod()
{
return sqrt(real * real + imaginary * imaginary);
}
int main()
{
Complex c1(3, 5);
Complex c2 = 4.5;
Complex c3(c1);
c1.add(c2);
c1.show();
cout << c1.mod();
return 0;
}
运行截图

按照要求写出的Comloex类有点问题,add函数的设计不合理。
改进
源码
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class Complex
{
private:
double real, imaginary;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imaginary(i){};
Complex(const Complex &c);
Complex add(const Complex m);
void show();
double mod();
};
Complex::Complex(const Complex &c)
{
real = c.real;
imaginary = c.imaginary;
}
Complex Complex::add(const Complex m)
{
Complex a;
a.real = real+m.real;
a.imaginary = imaginary+m.imaginary;
return a;
}
void Complex::show()
{
cout << real << std::showpos << imaginary << "i" << std::noshowpos << endl;
}
double Complex::mod()
{
return sqrt(real * real + imaginary * imaginary);
}
int main()
{
Complex c1(3, 5);
Complex c2 = 4.5;
Complex c3(c1);
c1 = c1.add(c2);
c1.show();
cout << c1.mod();
cin.get();
return 0;
}
像这样把结果return回来才比较好。
感想
无
Complex类的设计与改进的更多相关文章
- complex类的设计实现
#include <iostream> #include <cmath> using namespace std; class Complex{ ,); Complex(Com ...
- 设计、定义并实现Complex类
设计.定义并实现Complex类 #include <iostream> #include <cmath> using namespace std; class MyCompl ...
- C++之不带指针类的设计——Boolean
经典的类设计分类 带指针类 不带指针类 Header文件的布局 #ifndef __COMPLEX__ #define __COMPLEX__ #include <iostream.h> ...
- Unity3D 游戏开发构架篇 ——角色类的设计与持久化
在游戏开发中,游戏角色占了很大的篇幅,可以说游戏中所有的内容都是由主角所带动.这里就介绍一下角色类的设计和持久化. 一.角色类应用场景和设计思想 游戏中的角色类型不一而足,有不同的技能,有不同的属性等 ...
- “乐”动人心--2017年10款最佳音乐类APP设计盘点
在上下班的路上,听几首自己喜欢的音乐来打发无聊的等公交车和地铁的时间是现代年轻人的常态.音乐作为最能鼓动人心的"语言",也成为了人们在互联网生活里占比例最高的消费活动之一,一款好看 ...
- Java SE 之 数据库操作工具类(DBUtil)设计
JDBC创建数据库基本连接 //1.加载驱动程序 Class.forName(driveName); //2.获得数据库连接 Connection connection = DriverManager ...
- 学员会诊之02:SVN协作以及Page类的设计
三层架构的学生管理系统是我们第一个稍微大型的项目:分层.一个解决方案多个Project,所以值得我们停下来好好审查审查. 1.测试SVN服务器地址 我们的作业要求学员创建自己的SVN服务器,并且将代码 ...
- 课堂练习Complex类
Complex类 #include<iostream> #include<cmath> using namespace std; class Complex { public: ...
- Date类为什么设计为可变的,而不是像String一样?
首先,不得不承认,这确实是类库设计的一个错误,所以"为什么"进行了这个错误设计并没有意义.但没有事物一诞生就是完美的,我们的Java只是反应的慢了一点,再慢了一点. 更何况,Dat ...
随机推荐
- 接口测试工具-Jmeter使用笔记(八:模拟OAuth2.0协议简化模式的请求)
背景 博主的主要工作是测试API,目前已经用Jmeter+Jenkins实现了项目中的接口自动化测试流程.但是马上要接手的项目,API应用的是OAuth2.0协议授权,并且采用的是简化模式(impli ...
- Oracle中exp导出与imp导入的参数(full,owner/formuser/touser)测试
1.exp导出的参数(FULL,OWNER)测试 先知道的一点是full不能与owner共存,还有都是以用户的方式导出(在这里),其中不仅仅包括表,这可能就是下面报warnings的原因,因为Orac ...
- Mysql+keepalived双主
搭建环境说明: master1:192.168.175.210 主 master2:192.168.175.211 备 keepalived的vip: 192.168.175.11(注意这是虚拟IP, ...
- 干了这杯Java之transient关键字
看源码的时候,发现transient这个关键字,不甚理解,查找资料发现:不被序列化 疑问: 静态变量是不是不被序列化? public class User implements Serializabl ...
- 六、latex中的特殊字符
- string函数详解(配案例)
多说无益上码~ #include<iostream> #include<algorithm> #include<cmath> #include<cstring ...
- #WEB安全基础 : HTTP协议 | 0x16 HTTPS:加密的秘密
公开秘钥加密&&共享秘钥加密 这两个冗长的短语,让我拿什么理解? 我们知道HTTPS有加密功能,以上的两个短语很常用.先摆在这,接下来开始尝试理解它们. 共享秘钥加密(对称秘钥加密): ...
- mongodb细讲
一. 关系型数据库(sql) 1.建表 二.非关系型数据库(nosql 98提出的概念) 1.不用建库建表数据直接存入就可 优缺点: 关系型:节约资源(学生姓名和课程名不重复出现),开发不方便(需先 ...
- 安装linux虚拟机
虚拟机安装流程 1. 安装系统 安装完成 2. 安装VMware tools linux(ubuntu 18.04 Desktop) 手动安装 1) 加载光驱 2) 双击进入光驱,在光驱的目录下,打开 ...
- git远程管理