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类的设计与改进的更多相关文章

  1. complex类的设计实现

    #include <iostream> #include <cmath> using namespace std; class Complex{ ,); Complex(Com ...

  2. 设计、定义并实现Complex类

    设计.定义并实现Complex类 #include <iostream> #include <cmath> using namespace std; class MyCompl ...

  3. C++之不带指针类的设计——Boolean

    经典的类设计分类 带指针类 不带指针类 Header文件的布局 #ifndef __COMPLEX__ #define __COMPLEX__ #include <iostream.h> ...

  4. Unity3D 游戏开发构架篇 ——角色类的设计与持久化

    在游戏开发中,游戏角色占了很大的篇幅,可以说游戏中所有的内容都是由主角所带动.这里就介绍一下角色类的设计和持久化. 一.角色类应用场景和设计思想 游戏中的角色类型不一而足,有不同的技能,有不同的属性等 ...

  5. “乐”动人心--2017年10款最佳音乐类APP设计盘点

    在上下班的路上,听几首自己喜欢的音乐来打发无聊的等公交车和地铁的时间是现代年轻人的常态.音乐作为最能鼓动人心的"语言",也成为了人们在互联网生活里占比例最高的消费活动之一,一款好看 ...

  6. Java SE 之 数据库操作工具类(DBUtil)设计

    JDBC创建数据库基本连接 //1.加载驱动程序 Class.forName(driveName); //2.获得数据库连接 Connection connection = DriverManager ...

  7. 学员会诊之02:SVN协作以及Page类的设计

    三层架构的学生管理系统是我们第一个稍微大型的项目:分层.一个解决方案多个Project,所以值得我们停下来好好审查审查. 1.测试SVN服务器地址 我们的作业要求学员创建自己的SVN服务器,并且将代码 ...

  8. 课堂练习Complex类

    Complex类 #include<iostream> #include<cmath> using namespace std; class Complex { public: ...

  9. Date类为什么设计为可变的,而不是像String一样?

    首先,不得不承认,这确实是类库设计的一个错误,所以"为什么"进行了这个错误设计并没有意义.但没有事物一诞生就是完美的,我们的Java只是反应的慢了一点,再慢了一点. 更何况,Dat ...

随机推荐

  1. js导航栏单击事件背景颜色变换

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  2. Redis的持久化之AOF方式

    AOF方式:将以日志,记录每一个操作 优势:安全性相对RDB方式高很多: 劣势:效率相对RDB方式低很多: 配置: [root@localhost redis]# vi redis.conf 编辑re ...

  3. 红黑树与AVL特性

    红黑树:比较平衡的二叉树,没有一条路径会比其他路径长2倍,因而是近似平衡的.所以相对于严格要求平衡的AVL树来说,它的旋转保持平衡次数较少.插入删除次数多的情况下我们就用红黑树来取代AVL. 红黑树规 ...

  4. python tkinter entry

    """小白随笔,大佬勿喷""" '''Entry编辑框 收集数据''' import tkinter as tk import tkinte ...

  5. 【Idea】-NO.162.Idea.1 -【Idea Unable to import maven project: See logs for details】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  6. table的thead,tbody,tfoot

    为了让大表格(table)在下载的时候可以分段的显示,就是说在浏览器解析HTML时,table是作为一个整体解释的,使用tbody可以优化显示. 如果表格很长,用tbody分段,可以一部分一部分地显示 ...

  7. Oracle 10.2.0.5升级至11.2.0.4

    参照MOS 官方文档Complete Checklist for Manual Upgrade to Oracle Database 11gR2 (11.2) (Doc ID 837570.1)一.升 ...

  8. sklearn中报错ValueError: Expected 2D array, got 1D array instead:

    from sklearn.linear_model import LinearRegression lr = LinearRegression() print(tr_x.shape,tr_y.shap ...

  9. ubuntu16.04下g++安装及使用

    1)首先在虚拟机中安装Ubuntu16.04,网络模式设置为NAT模式,安装完成后在虚拟机中测试是否能够上网. 2)进入Ubuntu,按Ctrl+alt+T,调出终端,输入sudo su,输入密码切换 ...

  10. 关于git的认证方式

    之前对github的使用,形成了两种观点.就是有两种url的模式,一种是http或https的,另一种是git专属的.然后git专属的url方式可以配置公钥认证,http(s)的则需要输入密码. 近期 ...