大学c 习题答案习题13答案

上传人:bin****86 文档编号:55167382 上传时间:2018-09-25 格式:DOC 页数:16 大小:74KB
返回 下载 相关 举报
大学c  习题答案习题13答案_第1页
第1页 / 共16页
大学c  习题答案习题13答案_第2页
第2页 / 共16页
大学c  习题答案习题13答案_第3页
第3页 / 共16页
大学c  习题答案习题13答案_第4页
第4页 / 共16页
大学c  习题答案习题13答案_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《大学c 习题答案习题13答案》由会员分享,可在线阅读,更多相关《大学c 习题答案习题13答案(16页珍藏版)》请在金锄头文库上搜索。

1、习题习题 13 答案答案一、简答题 1什么是运算符重载?实现运算符重载有几种方法?如何实现? 答案:C+提供的运算符重载机制能够实现将已有运算符应用到新的数据类型变量上,赋予 运算符新的含义。C+对运算符重载有一些限制,见 13.2.1 节。 有两种方法可以实现运算符重载,它们是:类的成员函数方式和有友元函数方式。 定义友元的目的是在友元函数中直接访问类的私有成员。实际上,可以通过类的公有 函数接口访问类的私有成员,所以实现运算符重载,可以使用非成员、非友元实现运算符 的重载。但是这种实现方法增加了访问私有数据成员的公有函数的调用时间,不值得提倡。 见 13.2.2 节。2如何实现本类对象与其

2、它类对象之间的相互类型转换? 答案:可以使用“类型转换函数”将本类对象转换成其他类对象。而“类型转换函数”本质上 就是运算符的重载。另外,可以使用“构造函数”将其他类对象转换成本类对象。3什么是虚函数?什么是纯虚函数?它们的作用是什么? 答案: 多态性是面向对象程序设计的重要特性,它与前面讲的封装性和继承性构成面向对象 程序设计的三大特性。这三大特性是互相关联的,封装性是基础,继承性是关键,而多态 性是补充。 多态分为静态多态和动态多态。函数重载和运算符重载属于静态多态。动态多态是运 行时的多态,如某些函数的调用关系在运行阶段才能确定。为了实现函数调用关系的动态 多态,可以将函数定义成虚函数。

3、将一个成员函数定义成虚函数的方法是,在函数定义的 返回值类型前加 virtual。 在抽象类中,有时无法实现某一功能(即无法决定某一函数的具体实现) ,只能将函数 定义成纯虚函数。 虚函数具有函数实现的函数体,而纯虚函数没有函数体,即没有函数实现。对纯虚函 数,只能在其派生类中实现该函数的功能。4试比较虚函数与虚基类的概念及作用。 答案:虚函数用于实现动态多态。而虚基类用于实现在多重继承情况下,基类数据成员在 派生类中保留一个副本,见 12.6 节。二、选择题 答案: 1. C 2. D3. A4. D5. B6. C7. D8. C三、运行结果、完善程序 1 运行结果: n=4 n=3 2,

4、 3 2 答案: 先看虚线框中的程序,运行结果为: D:show( ) called. 8 若将 A 行的 virtual 去掉,则结果为: B:show( ) called. 5 对本题,若将虚线框中的 fun( )和 main( )函数同时修改成右侧实线框中的对应函数,则若 A 行有 virtural 时,结果为: D:show( ) called. 8 ; 若 A 行无 virtual 时,结果为: B:show( ) called. 5 。3 运行结果为: The B version B The D1 info: 4 version 1 The D2 info: 100 version

5、 B The D3 info: -25 version 3去掉 A 行的 virtual,则运行结果为: The B version B The B version 1 The B version B The B version 34 答案: (1)operator char *( ) (2)return s; (3)str = num; 四、编程题 1 答案: / 实现 1,赋值运算符重(=和+=)载函数的返回值为对象的引用。 / 缺点:不可实现对象的连续赋值,只能 c1= c2 #include class Complex float Real, Image; public: Comple

6、x(float r=0,float i=0) Real=r;Image=i; void show( ) coutclass Complex float Real, Image; public: Complex(float r=0,float i=0) Real=r;Image=i; void show( ) cout #include class Fraction int m, n; / m 是分子,n 是分母 public: Fraction(int im=0,int in=1) int t=gcd(im,in); m=im/t; n=in/t; if(m*n0) / 若分子和分母同号 m=

7、abs(m); n=abs(n); else / 若分子和分母异号 m=-abs(m); n=abs(n); ; int gcd(int x,int y); Fraction n=b.n; return *this; Fraction operator+ ( Fraction b ) return Fraction(m*b.n+n*b.m,n*b.n); Fraction operator- ( Fraction b ) return Fraction(m*b.n-n*b.m,n*b.n); friend Fraction operator* ( Fraction a, Fraction b

8、);friend Fraction operator/ ( Fraction a, Fraction b ); void Show() cout class Point float x,y; public: Point(float a=0,float b=0) x=a;y=b; void show( ) cout #include class string char *str; int len; public: string(char *s=0)/构造函数,含缺省构造函数 if(s) /此处必须判断 s 是否为空指针。 len=strlen(s); /若 s 为空指针,则 strlen(s)报

9、运行错。str=new charlen+1;strcpy(str,s); else len=0,str=0; /注意逗号 string(string str=new charlen+1;strcpy(str,st.str); else len=0,str=0; string( ) /析构函数 if(str) delete str; void set(char *s) /置值 if(str) delete str; /必须先删除原空间 if(s) len=strlen(s); str=new charstrlen(s)+1;strcpy(str,s); else str=0,len=0; voi

10、d show(void) /显示串 if(str) cout class Matrix int *p, m, n; public: Matrix(int mm, int nn ) m=mm, n=nn; p=new intm*n; Matrix(Matrix n=b.n; p=new intm*n; for(int i=0; ipi; Matrix operator+(Matrix Matrix A(m,n), B(m,n); cout class Shape public: virtual double Area()=0; virtual void Setdata(double,double

11、=0)=0; ;class Triangle:public Shape double w,h; public: Triangle(double w1=0,double h1=0) w=w1; h=h1; void Setdata(double w1,double h1=0) w=w1; h=h1; double Area() return (w*h)/2); ;class Rectangle:public Shape double w,h; public: Rectangle(double w1=0,double h1=0) w=w1; h=h1; void Setdata(double w1

12、,double h1=0) w=w1; h=h1; double Area() return w*h; ;class Square:public Shape double s; public: Square(double s1=0) s=s1; void Setdata(double s1,double h1=0)s=s1; double Area( ) return s*s; ;class Circle:public Shape double r; public: Circle(double r1=0) r=r1; void Setdata(double r1,double h1=0) r=r1; double Area( ) return (3.14*r*r); ;double CalcArea(Shape *ps) return(ps-Area(); void main() Triangle tri(3,4); Rectangle rec(4,5); Square squ; Circle cir;double s=2, r=8; squ.Setdata(s); cir.Setdata(r);double area=CalcArea( cout “The sum of area = “areaendl;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号