c++primer plus 第11章 编程题第7题
#pragma once
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
#include<iostream>class Complex
{
private:double real;double imgn;
public:Complex();Complex(double a, double b);~Complex();Complex operator+(const Complex & a) const;Complex operator-(const Complex & a) const;Complex operator~();Complex operator*(double c);Complex operator*(const Complex & a) const;//friendfriend Complex operator*(const double c, const Complex & a);friend std::ostream & operator<<(std::ostream & os, const Complex &a);//operator后面别打空格friend std::istream & operator>>(std::istream & os, Complex &a);//cin是在istream里

};#endif // !COMPLEX0_H_//总结,如果在第二个构造函数里写了double n =0, double b = 0;就不用写complex(),两个是等价的,会存在歧义
//所以尽量不要在构造函数参数里写 = 0;因为会产生歧义,老实用complex()和第二个构造函数就好
//凡是不用改变的都设成const---------------------------------
#include"complex0.h"Complex::Complex()
{real = imgn = 0;
}Complex::Complex(double n1, double n2)
{real = n1;imgn = n2;
}Complex Complex::operator+(const Complex & a) const
{Complex temp;temp.real = real + a.real;temp.imgn = imgn + a.imgn;return temp;/*return Complex(real + a.real, imgn + a.imgn);*///直接调用构造函数//要习惯于用这个简化语句//一般就用最后一条语句,等价于前面三条,调用构造函数会产生一个临时对象,赋值给左边
}Complex Complex::operator-(const Complex & a) const
{Complex temp;temp.real = real - a.real;temp.imgn = imgn - a.imgn;return temp;
}Complex Complex::operator*(const Complex & a) const
{Complex temp;temp.real = real * a.real + imgn * a.imgn;temp.imgn = real * a.imgn + imgn * a.real;return temp;
}Complex Complex::operator~()
{imgn = 0 - imgn;return *this;
}Complex Complex::operator*(double c)
{return Complex(c * real, c * imgn);
}
//friend
Complex operator*(const double c, const Complex & a)//别打::因为友元不在类里面,虽然是在类里面声明
{return Complex(c * a.real, c * a.imgn);
}std::ostream & operator<< (std::ostream & os, const Complex &a)
{os << "is " << "( " << a.real << ", " << a.imgn << "i )";return os;
}
std::istream & operator >> (std::istream & os, Complex & a)//cin在istream里
{std::cout << "real: ";os >> a.real;std::cout << "imaginary: ";os >> a.imgn;return os;}
Complex::~Complex()
{}

 

posted on 2018-06-28 20:50 syne 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/syne-cllf/p/9240615.html

c++primer plus 第11章 编程题第7题相关推荐

  1. c++primer plus 第13章 编程题第2题

    c++primer plus 第13章 编程题第2题 #pragma once #ifndef CD_H_ #define CD_H_ //base classclass Cd { private:c ...

  2. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  3. C++ Primer Plus 第七章编程题练习

    C++ Primer Plus 第七章编程题练习 第一题 题目描述 编写一个程序,不断要求用户输入两个数,直到其中的一个为0.对于每两个数,程序将使用一个 函数来计算它们的调和平均数,并将结果返回给m ...

  4. c primer plus 第五章编程练习

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 目录 文章目录 前言 ##1.编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创 ...

  5. C++primer plus第四章编程练习自编程序

    //第四章编程练习 //eg.1显示信息 #include <iostream> using namespace std; const int SIZE = 20; int main() ...

  6. C Primer Plus 第十一章 编程练习 1-15题

    第1题 #include<stdio.h> void readChar(char* words , int len); int main(void) {int LEN;printf(&qu ...

  7. 记录——《C Primer Plus (第五版)》第十二章编程练习第1-8题

    1.不使用全局变量,重写12.4的程序 . # include <stdio.h>int critic(void); int main(void) {int units = 0;print ...

  8. 记录——《C Primer Plus (第五版)》第十一章编程练习第5-12题

    5.编写一个函数is_within(),它接受两个参数,一个是字符,另一个是字符串指针.其功能是如果字符在字符串中,就返回一个非0值(真):如果字符不在字符串中,就返回0值(假).在一个使用循环语句为 ...

  9. 记录——《C Primer Plus (第五版)》第九章编程练习第十题

    第十题:编写并测试一个函数Fibonacci(),在该函数中使用循环替代 递归完成斐波纳契数列的计算. # include <stdio.h>void Fibonacci(int);int ...

最新文章

  1. Java随机数控制范围
  2. 前端学习(3099):vue+element今日头条管理-使用富文本比编辑器
  3. 英语----专业单词
  4. LeetCode(709)——转换成小写字母(JavaScript)
  5. getnumdevices.c setgetdevicetype例程
  6. 本博客自排名1000到400的各项数据变化
  7. 压力测试工具JMeter入门教程
  8. 同济大学高数第七版下册对梯度的解释
  9. 融思杯 第三届 部分wp
  10. 小程序:canvas绘制网络图片
  11. 全志8916平台MIPI_LCD调试方法
  12. java中的nio是啥,java中的NIO
  13. 网易互联网产品运营管培生面试经历--从群面到终面面试经验分享
  14. 内网安全-域横向PTHPTKPTT哈希票据传递
  15. 223_动态设置页面指示器indicators
  16. python ogr创建shp
  17. 注册表功能大全(转)
  18. 修改element ui tree 搜索功能,实现分级搜索,关键字高亮
  19. ubuntu下使用 RabbitVCSRapidSVN
  20. SQL注入攻击以及防护

热门文章

  1. Cracking Wifi Wpa-Wpa2 in 5 second——Dumpper V.80.8 +JumpStart+WinPcap
  2. 【百度地图API】自行获取区域经纬度的工具
  3. 分享:嵌入式Linux入门学习指导
  4. Python re module的使用
  5. Android 中点击某个按钮实现 返回键 的功能
  6. Redis 单例、主从模式、sentinel 以及集群的配置方式及优缺点对比(转)
  7. JavaWeb基础—项目名的写法
  8. Android开发遇到手机无法弹出Toast
  9. Android 项目中常用到的第三方组件
  10. MongoDB副本集配置系列四:节点的关闭顺序