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. trident-deploy自动部署命令

    sh ./iot-api/install.sh  ./iot-api/values.yaml

  2. Azure基础(二)- 核心云服务 - Azure简介

    Azure fundamentals - Core Cloud Services - Introduction to Azure Learn what Microsoft Azure is and h ...

  3. git merge后如何撤销

    merge后发现冲突太多,或者合并的分支代码并不是最新,那就直接撤销再合并好了. git reset --hard HEAD 用来撤销还没commit 的merge,其实原理就是放弃index和工作区 ...

  4. IDEA搭建scala开发环境开发spark应用程序

    通过IDEA搭建scala开发环境开发spark应用程序   一.idea社区版安装scala插件 因为idea默认不支持scala开发环境,所以当需要使用idea搭建scala开发环境时,首先需要安 ...

  5. ShellExecute 打开网页、目录、邮箱

    #include <Windows.h> #include <tchar.h> int WINAPI _tWinMain(HINSTANCE hInstance, HINSTA ...

  6. Linux部署Java环境

    一. yum安装jdk (1) 搜索jdk安装包 yum search java|grep jdk (2) 下载jdk1.8,下载之后默认的目录为: /usr/lib/jvm/ yum install ...

  7. 转载 usb_alloc_coherent 和 usb_free_coherent

    今天做移植的时候,随手记录一下,今天所遇到的问题解决方法. 在linux2.6.34和之前的代码中还可以使用usb_buffer_alloc 和 usb_buffer_free 这两个函数,在2.6. ...

  8. dos模式下切换电脑用户

    启用管理员运行dos 然后输入net user adminstrator /active.yes 然后点击打开按钮 就可以切换电脑用户了

  9. (Review cs231n) Spatial Localization and Detection(classification and localization)

     重在图像的定位和检测的内容. 一张图片中只有一种给定类别标签的对象,定位则是图像中有对象框:再这些类中,每一个训练目标都有一个类和许多的图像内部对应类的位置选框. 猜想的仅是类标签,不如说它们是位置 ...

  10. 小米系统获取root权限的完整教程

    小米系统通过什么方法拥有root超级权限?我们都清楚,安卓机器有root超级权限,如果手机拥有root相关权限,能够实现更好的功能,打个比方我们企业的营销部门的同事,使用大多数营销应用都需要在root ...