面向对象程序设计(3.2.5)--多态性作业

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

}
运行结果:
zrf is:1/7; ssh is:2/5 (zrf==ssh) is:0; (zrf<ssh) is:1
8.2 已知一个有理数类 Zrf_Ratio,实现如下的操作符重载形式:
friend zrf_Ratio operator-(const zrf_Ratio&); friend zrf_Ratio operator+(const zrf_Ratio&, const zrf_Ratio&); friend zrf_Ratio operator-(const zrf_Ratio&, const zrf_Ratio&); friend zrf_Ratio operator*(const zrf_Ratio&, const zrf_Ratio&); friend zrf_Ratio operator/(const zrf_Ratio&, const zrf_Ratio&);
运行结果:
zrf is:1/7; ssh is:2/5 zl is:1/7 After zrf*=ssh, zrf is:1/7 After zrf++ is:1/7 After ++zrf is:15/7 zrf[1] is:15; zrf[2] is:7
8.8 声明一个抽象类 Med 作为基类,其中仅包含一个 string 类型数据成员 title(名称)和
zrf_Ratio& operator=(const zrf_Ratio&); zrf_Ratio& operator*=(const zrf_Ratio&); zrf_Ratio operator++(); zrf_Ratio operator++(int); int& operator[](int);
运行结果:
zrf is:1/7; ssh is:2/5 -zrf is:-1/7 (zrf-ssh) is:-9/35 (zrf+ssh) is:19/35 (zrf*ssh) is:2/35 (zrf/ssh) is:5/14
8.3 已知一个有理数类 Zrf_Ratio,实现如下的操作符重载形式:
作业 8 以下作业题号与《C++语言程序设计(第 3 版)教师用书》(郑莉,清华大学出版社) 题号对应
8.1 已知一个有理数类 Zrf_Ratio,实现如下的操作符重载形式:
friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&); friend std::istream& operator>>(std::istream&, zrf_Ratio&); friend bool operator==(const zrf_Ratio&, const zrf_Ratio&); friend bool operator<(const zrf_Ratio&, const zrf_Ratio&);
测试用主函数:
in源自文库 main() {
zrf_Ratio zrf(1,7),ssh(26,65),zl; zl=zrf; std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ; std::cout<<"zl is:"<<zl; std::cout<<"\nAfter zrf*=ssh, zrf is:"<<zrf; std::cout<<"\nAfter zrf++ is:"<<zrf++ ; std::cout<<"\nAfter ++zrf is:"<<++zrf ; std::cout<<"\nzrf[1] is:"<<zrf[1]<<"; zrf[2] is:"<<zrf[2] ; return 0; }
运行结果:
C++ 语言程序设计 , Author: 张三, Publisher: 清华大学出版社. id: 0-000-00000-1
辨析 C/C++编程模式 , Magzine: Vol. 16, No. 1 id: 0000-000X
C++源代码, CD composer:清华大学计算中心 id: ARCHIV000001
测试用主函数:
int main() {
Book book("张三", "C++ 语言程序设计", "清华大学出版社", "0-000-00000-1"); Mag mag("辨析 C/C++编程模式", "0000-000X", 020, 01); CD cd("C++源代码", "清华大学计算中心", "ARCHIV", "000001"); book.print(); cout << "\tid: " << book.id() << endl; mag.print(); cout << "\tid: " << mag.id() << endl; cd.print(); cout << "\tid: " << cd.id() << endl; return 0; }
测试用主函数:
int main() {
zrf_Ratio zrf(1,7),ssh(26,65); std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ; std::cout<<"-zrf is:"<<-zrf; std::cout<<"\n(zrf-ssh) is:"<<(zrf-ssh); std::cout<<"\n(zrf+ssh) is:"<<(zrf+ssh) ; std::cout<<"\n(zrf*ssh) is:"<<(zrf*ssh) ; std::cout<<"\n(zrf/ssh) is:"<<(zrf/ssh) ; return 0; }
两个纯虚函数 print()和 id()。然后派生出 Book 类,CD 类和 Mag 类。在 Book 类中,包含 3
个 string 类型数据成员 author(作者), pub(出版者), isbn(书号)和函数成员
print()和 id()。在 CD 类中,包含 3 个 string 类型数据成员 composer(制作者), make
测试用主函数:
int main() {
zrf_Ratio zrf(1,7),ssh(26,65); std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ; std::cout<<"(zrf==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh) ; return 0;
(介质形式), number(编号)和函数成员 print()和 id()。在 Mag 类中,包含 2 个 string
类型数据成员 issn(杂志编号), pub(出版者),两个整型数据成员 volume(卷),
number(号)和函数成员 print()和 id()。
派生类的 print()和 id()函数功能请自行设计。
相关文档
最新文档