C++第五章 类与对象习题解答(3)
第五章 类与对象习题
complex complex::operator -(complex c){ //重载复数\ complex temp;
temp.real = real - c.real; temp.imag = imag - c.imag; return temp; }
complex complex::operator *(complex c){ //重载复数\ complex temp;
temp.real = real * c.real - imag * c.imag; temp.imag = real * c.imag + imag * c.real; return temp; }
complex complex::operator /(complex c){ //重载复数\ complex temp; double d;
d = (c.real*c.real + c.imag*c.imag);
temp.real = (real * c.real + imag * c.imag)/d; temp.imag = (c.real * imag - real * c.imag)/d; return temp; }
complex complex::operator +=(complex c){ //重载复数\ real = real + c.real; imag = imag + c.imag; return *this; }
complex complex::operator -=(complex c){ //重载复数\ real = real - c.real; imag = imag - c.imag; return *this; }
complex complex::operator *=(complex c){ //重载复数\ complex temp;
temp.real = real * c.real - imag * c.imag; temp.imag = real * c.imag + imag * c.real; *this = temp; return *this; }
11
第五章 类与对象习题
complex complex::operator /=(complex c){ //重载复数\ complex temp; double d;
d = (c.real*c.real + c.imag*c.imag);
temp.real = (real * c.real + imag * c.imag)/d; temp.imag = (c.real * imag - real * c.imag)/d; *this = temp; return *this; }
complex complex::operator ++(){ //重载复数前缀\ ++this->real;//完整表达形式 ++this->imag; return *this; }
complex complex::operator ++(int){ //重载复数后缀\ complex temp(real,imag); real++; imag++;
return temp; }
void complex::print(){ //显示复数 cout << real;
if(imag > 0) cout << \
if(imag != 0) cout << imag << \}
void main(){
complex A(30,40),B(15,30),C; //定义3个复数对象 cout<<\为:\ A.print();
C=A.operator++(1);//即A++ cout<<\后,C为:\ C.print(); cout<<\为:\ A.print();
C=A.operator++();//即++A cout<<\后,C为:\ C.print(); cout<<\为:\ A.print(); cout<<\为:\
12
第五章 类与对象习题
B.print(); C=A+B;
cout<<\后,C为:\ C.print(); A+=B;
cout<<\后,A为: \ A.print(); C=A-B;
cout<<\后,C为:\ C.print(); A-=B;
cout<<\后,A为: \ A.print(); C=A*B;
cout<<\后,C为:\ C.print(); A*=B;
cout<<\后,A为: \ A.print(); C=A/B;
cout<<\后,C为:\ C.print(); A/=B;
相关推荐:
- [互联网资料]2022年厦门大学机电工程系824机械设计
- [互联网资料]东南大学2022年硕士研究生拟录取名单公
- [互联网资料]能源调研报告(精选多篇)
- [互联网资料]初三英语下学期 中考英语 语法填空训练
- [互联网资料]2022内蒙古选调生行测常识备考:新事物
- [互联网资料]自驾必备!在新西兰租什么样的车自驾游
- [互联网资料]佛教素食菜谱44页未完
- [互联网资料]盈利能力分析外文翻译
- [互联网资料]2022年南昌航空大学音乐学院736马克思
- [互联网资料]优选外贸跟单实习报告总结(精品版)
- [互联网资料]银行新员工培训总结
- [互联网资料]2_year_visa_new_guidance_190316
- [互联网资料]天津市五校宝坻一中静海一中杨村一中芦
- [互联网资料]2007--2008学年第一学期高三数学宁波市
- [互联网资料]Chromatic framework for vision in ba
- [互联网资料]幼儿园大班上学期美术教案《心愿树》含
- [互联网资料]2022年华中农业大学信息学院820微型计
- [互联网资料]硬盘坏道的表现 __硬盘使用久了
- [互联网资料]江苏省2016年会计从业资格考试《会计基
- [互联网资料]公共场所卫生监督试卷全解
- 高级英语第一册所有修辞方法及例子总结
- 综合交通枢纽规划与城市发展
- 沃尔玛的企业文化案例分析
- 美国Thanksgiving Day 感恩节 介绍
- PEP六年级英语上册Unit6How do you fee
- 最齐全的中国大型商场购物中心名单
- 数据结构实验报告八—哈夫曼编译码
- 杭州市余杭区人民政府(通知)
- 七年级语文成语运用专项训练
- 微观经济学第三章 消费者行为 课后习题
- 对_钱学森之问_的思考
- Excel_三级联动_下拉菜单
- 办公用品需求计划申请表
- 对外汉语教材必须要知道的发展史
- 挑战杯大学生学术科技作品竞赛作品申报
- 举办民办教育培训机构应具备下列条件
- 太阳能路灯项目设计方案
- 2013年八年级上最新人教版新教材Unit3I
- 【历史】 6-4 《近代科学之父牛顿》 课
- 高中生物《第四章 第二节 探讨加酶洗衣




