C++程序设计(第2版)课后习题答案--第12章
2013-03-25 15:40
441 查看
View Code
// 文件complex.h: 复数类的定义 #ifndef __COMPLEX__H__ #define __COMPLEX__H__ class Complex { public: Complex(double = 0.0, double = 0.0); Complex operator + (const Complex &) const; Complex operator - (const Complex &) const; //Complex & operator = (const Complex &); void print()const; private: double real; // 实数部分 double imaginary; // 虚数部分 }; #endif // 文件complex.cpp: 复数类的实现 #include <iostream.h> #include "complex.h" Complex::Complex(double r, double i) { real = r; imaginary = i; } void Complex::print()const { cout << '(' << real << ", " << imaginary << ')'; } Complex Complex::operator + (const Complex &operand2) const { Complex sum; sum.real = real + operand2.real; sum.imaginary = imaginary + operand2.imaginary; return sum; } Complex Complex::operator - (const Complex &operand2) const { Complex diff; diff.real = real - operand2.real; diff.imaginary = imaginary - operand2.imaginary; return diff; } //Complex &Complex::operator = (const Complex &right) //{ // real = right.real; // imaginary = right.imaginary; // return *this; //} // 文件ex13_1.cpp: 主函数定义,通过运算符操作复数对象 #include <iostream.h> #include "complex.h" int main() { Complex x, y(4.3, 8.2), z(3.3, 1.1); // 输出x,y,z cout << "x: "; x.print(); cout << "\ny: "; y.print(); cout << "\nz: "; z.print(); //输出加法运算 x = y + z; cout << "\n\nx = y + z:\n"; x.print(); cout << " = "; y.print(); cout << " + "; z.print(); //输出减法运算 x = y - z; cout << "\n\nx = y - z:\n"; x.print(); cout << " = "; y.print(); cout << " - "; z.print(); cout << '\n'; return 0; }
相关文章推荐
- C++程序设计(第2版)课后习题答案--第14章
- C++程序设计(第2版)课后习题答案--第8章
- C++程序设计(第2版)课后习题答案--第13章
- C++程序设计(第2版)课后习题答案--第11章
- 《跟阿铭学Linux》第12章 shell脚本中的变量:课后习题与答案
- C++程序设计(第2版)课后习题答案--第4章
- 编程珠玑(第2版)第一章课后习题答案
- C++程序设计(第二版)谭浩强----程序题课后习题答案第一章
- 鸟哥的Linux私房菜基础学习篇(第二版)第六章课后习题与答案
- C++primer第5版课后练习习题答案 9.20
- 汇编语言王爽(第二版)课后习题答案
- 大学计算机基础第五版习题和课后题答案
- C语言程序设计第四版谭浩强课后习题答案 完整版
- 鸟哥的Linux私房菜基础学习篇(第二版)第二章课后习题与答案
- 数据库系统概论课后习题答案第二章
- 郑莉版c语言第六章课后习题答案
- 《C语言程序设计教程》(主编黄迪明、余勤)第四章课后习题答案
- 《软件工程导论》课后习题答案 张海藩
- 谢希仁计算机网络第五版课后习题答案(1~6章)
- 《C语言程序设计教程》课后习题答案