惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
腾讯CDC
G
Google Developers Blog
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
美团技术团队
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志

博客园 - 秋天的菠菜

大学计算机基础实验 2013年信1204-1-2班小学期<程序设计技能训练>作品 C++程序设计(第2版)课后习题答案--第14章 C++程序设计(第2版)课后习题答案--第13章 C++程序设计(第2版)课后习题答案--第11章 C++程序设计(第2版)课后习题答案--第8章 C++程序设计(第2版)课后习题答案--第4章 关于指针的经典例题 Java多线程读文件比单线程提高效率的实例 第一门编程语言选谁?(转) C语言程序设计实验指导书 寂寞让生命如此美丽 循环结构经典程序 C语言初学者最容易犯的错误 正则表达式 用脚本类IDS抵御针对WEB的攻击 java实验一 方法和构造方法 java实验二 类和对象 java实验四 面向对象的综合应用 java实验三 类的继承与多态
C++程序设计(第2版)课后习题答案--第12章
秋天的菠菜 · 2013-03-25 · via 博客园 - 秋天的菠菜

View Code

 1 // 文件complex.h: 复数类的定义
 2 #ifndef __COMPLEX__H__
 3 #define __COMPLEX__H__
 4 
 5 class Complex
 6 {
 7 public:
 8     Complex(double = 0.0, double = 0.0);
 9     Complex operator + (const Complex &) const;
10     Complex operator - (const Complex &) const;
11     //Complex & operator = (const Complex &);
12     void print()const;
13     
14 private:
15     double real; // 实数部分
16     double imaginary; // 虚数部分
17 };
18 
19 #endif
20 // 文件complex.cpp: 复数类的实现
21 #include <iostream.h>
22 #include "complex.h"
23 
24 Complex::Complex(double r, double i)
25 {
26     real = r;
27     imaginary = i;
28 }
29 
30 void Complex::print()const
31 {
32     cout << '(' << real << ", " << imaginary << ')';
33 }
34 
35 Complex Complex::operator + (const Complex &operand2) const
36 {
37     Complex sum;
38     sum.real = real + operand2.real;
39     sum.imaginary = imaginary + operand2.imaginary;
40     return sum;
41 }
42 
43 Complex Complex::operator - (const Complex &operand2) const
44 {
45     Complex diff;
46     diff.real = real - operand2.real;
47     diff.imaginary = imaginary - operand2.imaginary;
48     return diff;
49 }
50 
51 //Complex &Complex::operator = (const Complex &right)
52 //{
53 //    real = right.real;
54 //    imaginary = right.imaginary;
55 //    return  *this;
56 //}
57 // 文件ex13_1.cpp: 主函数定义,通过运算符操作复数对象
58 #include <iostream.h>
59 #include "complex.h"
60 
61 int main()
62 {
63     Complex x, y(4.3, 8.2), z(3.3, 1.1);
64     
65     // 输出x,y,z
66     cout << "x: ";
67     x.print();
68     cout << "\ny: ";
69     y.print();
70     cout << "\nz: ";
71     z.print();
72     
73     //输出加法运算
74     x = y + z;
75     cout << "\n\nx = y + z:\n";
76     x.print();
77     cout << " = ";
78     y.print();
79     cout << " + ";
80     z.print();
81     
82     //输出减法运算
83     x = y - z;
84     cout << "\n\nx = y - z:\n";
85     x.print();
86     cout << " = ";
87     y.print();
88     cout << " - ";
89     z.print();
90     cout << '\n';
91     
92     return 0;
93 }