C++面向对象程序设计双语教程(第3版)-参考答案【ch07】Polymorphism and Virtual Functions.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《C++面向对象程序设计双语教程(第3版)-参考答案【ch07】Polymorphism and Virtual Functions.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计双语教程(第3版)-参考答案【ch07】Polymorphism and Virtual Functions.docx(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 7 PoIymorph i sm and Vi rtuaIFunct i ons1. Wr ite the output of the fol lowing program.class Shape public:v 7. v . :.; , 7 : ; ?:/, , ., * , . * ; :;static intnumber; :; ?:.:;,;,、:.:double xCooriyCoord; 5 / :可二:;.;:.:;Shapc(0llbIe x,double y):xCoord(x),yCoord(y) ., _ *, , Z a . , , ifcout,Sh
2、ape,s constructor0 endi; I -;: / , . , . , , . ,* / V-: 1 coin Shapes area is; .: ” :;、;;:return 0.0;, J 、 . :* , . / :. , .: . j. , , a ( a ( , ZHShapeO , . H .:. , . z , ,:.coutShapes destructor* endl; :; : .工 . * * *manager. calculatePayment();salesperson. calculatePayment();employee, demote();re
3、turn 0;这段代码定义了一个基类Employee,以及从该类派生的Manager和Salesperson类。 Employee类有级别、工资和工作时间的成员变量,以及计算支付和降级的成员函数。Manager 类还有一个雇主成员变量,并重写了计算支付的函数。Salesperson类还有销售额成员变量, 并也重写了计算支付的函数。在主函数中,示例了如何创建员工、经理和销售员对象,并调用它们的计算支付函数和 降级函数。输出会显示出每个类的支付金额,以及员工降级后的级别。6. Here i s a c I ass h i erarchy, shown i n Figure 7-5, to repr
4、esent var i ous med i a objects:G i ven the def i n i t i on of the base cI ass Med i a:class Media . . : . . . . s *.1 * , , .r*- . . .- ,.:;:加谖泊.M丽阳等蕊举M二 virtual void priirtO = 0; . , , * /f . s * , . a . . ,三;:virtual string id。三0; 三三, protected: ”m string t /. . . . .:; .*, .懑涔涔犊哨案第海浅落澎 .Figure
5、7-5 The hierarchy diagram of these media objects(1) Exp lain the cI ass re I at i onsh i p descr i bed i n the h i erarchy d i agram.(2) Def i ne the Book and Magaz i ne cI asses accord i ng to the h i erarchy d i agram. (Hint: The pr i nt funct i on of each cI ass d i spI ays a I I i nformat i on o
6、f data members. The i d funct i on of each cI ass returns a str i ng i dent i f i er that can i nd i cate the med i a feature. Th i s i dent i f i er cons i sts of data members of each cI ass)(3) Wr i te the ma i n funct i on to test the Book and Magaz i ne c I asses. (Hint: You must def ine a point
7、er to a Media object in the main function.)略。7. Des i gns a cI ass named Person and i ts der i ved cI asses named Student and EmpIoyee.Assume that the dec Iarat ion of the Person cI ass i s as fol lows:class Person public:Pcrson(string stfN, string strA): name(strN)? address(strA)virtual string toSt
8、ringQ const;private:string name, address; / 9.一:J ;二:.::, z -(1) Def i ne the toStr i ng funct i on i n the Person cI ass. The return i ng va I ue of the toStr i ng funct i on i s used to display the cI ass i nformat i on in the fol lowing format: Name: XXXX;Address: XXXXXXX.(2) Def i ne the Student
9、 cI ass der i ved from the Person cI ass. A student has a cI ass status (freshmen, sophomore, jun i or, or sen i or). Overr i de the toStr i ng funct i on i n the Student cI ass.(3) Def i ne the EmpIoyee cI ass der i ved from the Person cI ass. An empIoyee has an off i ce and a saI ary. Overr i de t
10、he toStr i ng funct i on i n the EmpIoyee cI ass.(4) Write a top I eve I funct i on with a parameter of Person type and wr i te the ma i n funct i on to test the Student and EmpIoyee cI asses.epp ttinclude using namespace std;class Person protected:string name;string address;public:Person(string nam
11、e = string address = this-name = name;this-address 二 address;string toStringO return Name: + name + ; 地址: + address + );class Student : public Person private:int studentID;public:Student(string name = string address = int studentID = 0) :Person(name, address) this-studentID = studentID;);int main ()
12、 Person person(John 123 Main St Student student(Alice 456 Elm St 12345);cout Person: endl;cout person. toStringO endl;cout Student: endl;cout student. toStringO endl;return 0;这段代码定义了一个名为Person、的基类及其名为Student的派生类。Person类有一 个toString函数,用于返回格式化的类信息。Student类从Person类派生而来,并添加了 一个名为studentID的新成员。在主函数中,我们创建
13、了一个Person对象和一个Student对象,并分别输出它们的类 信息。运行该程序,你将得到类似以下的输出结果:Person:Name: John;地址:123 Main St.Student:Name: Alice;地址:456 Elm St.Wr ite a program.1712 3z(x /( z(Abstract base cI ass Shape.Classes Tr iangle, Square and Circle are der i ved from Shape.CaIcuI ate the areas and per imeters of Tr iangle, Squa
14、re and Circle.下面是一个使用C+编写的示例程序,实现了抽象基类Shape和派生类Triangle、Square 和Circle的功能。每个形状类都可以计算自身的面积和周长。请注意,代码中的计算公式 可能需要根据实际情况进行更改。、CPPttinclude using namespace std;/抽象的基类形状class Shape public:/纯虚函数,用于计算面积virtual float getAreaO = 0;/纯虚函数,用于计算周长virtual float getPerimeter()二 0;);/类三角形,从形状派生class Triangle : publi
15、c Shape private:float base;float height;public:/构造函数Triangle(float base, float height) this-base = base;this-height = height;/重写计算面积的函数float getArea() override return 0. 5 * base * height;/重写计算周长的函数float getPerimeter() override /假设三角形的边长相等return 3 * base;);/类方形,从形状派生class Square : public Shape priva
16、te:float side;public:/构造函数Square(float side) this-side = side;)/重写计算面积的函数float getAreaO override return side * side;/重写计算周长的函数float getPerimeter () return 4 * side;);/类圆形,从形状派生class Circle : public Shape private:float radius;const float pi = 3. 14159; / 假设圆周率为 3. 14159public:/构造函数Circle(float radius
17、) this-radius radius;)/重写计算面积的函数 float getAreaO override return pi * radius * radius;/重写计算周长的函数float getPerimeter() override return 2 * pi * radius;);int main() /创建三个形状对象Triangle triangle(5, 8);Square square(4);Circle circle(3);/输出每个形状的面积和周长cout Triangle: Area = triangle. getAreaO Perimeter = triang
18、le. getPerimeter () endl;cout Square: Area = square. getAreaO Perimeter = square. getPerimeter () endl;cout Circle: Area = circle. getAreaO Perimeter = circle. getPerimeter () endl;return 0;这个程序定义了一个抽象基类Shape,以及派生类Triangle、Square和Circle。每个 派生类实现了 Shape基类中的纯虚函数getAreaO和getPerimeter (),并提供了自己的实 现。在主函数
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ch07 C+面向对象程序设计双语教程第3版-参考答案 【ch07】Polymorphism and Virtual Functions C+ 面向
![提示](https://www.taowenge.com/images/bang_tan.gif)
链接地址:https://www.taowenge.com/p-97920913.html
限制150内