ImageVerifierCode 换一换
格式:DOCX , 页数:12 ,大小:16.55KB ,
资源ID:3856642      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3856642.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(24 运算符重载 第二章 C++面向对象程序设计.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

24 运算符重载 第二章 C++面向对象程序设计.docx

1、24 运算符重载 第二章 C+面向对象程序设计34、运算符重载的含义是什么?是否所有的运算符都可以重载?答:运算符重载是对已有的运算符赋予多重含义,同一个运算符作用于不用类型的数据导致不同行为。不是。在C+语言中除了(点运算符(.)、作用域运算符(:)、条件运算符(?:)、成员指针运算符(*)、编译预处理命令的开始符号(#)五个运算符不能被重载外,其他运算符均可重载。35、运算符重载有哪两种形式?这两种形式有何区别?答:运算符重载有成员函数和友元函数两种形式。 区别:运算符作为成员函数重载时的参数比作为友元函数重载时少一个。例如使用成员函数重载双目运算符时只用一个参数,而使用友元函数重载时要用

2、两个参数。 36、转换函数的作用是什么?答:1、支持混合类型表达式 2、转换减少所需操作符的数目 37、分析下列程序的结果。#includeclass CMatrix public: cMatrix(int r,int c) row=r;col=c; element=new doublerow*col; double& operator() (int x,int y) return elementcol*(x-1)+y-1; double& operator() (int x,int y) const return elementcol*(x-1)+y-1; CMatrix() delete

3、element; private: double *element; int row,col;void main() CMatrix m(5,8); for(int i=;i5;i+) m(i,1)=i+5; for(i=0;i5;i+) coutm(i,1)”,”; coutendl;运行结果:5,6,7,8,9, 38、定义一个复数类,通过重载运算符:=、+=、+、-、*、/,直接实现两个复数之间的各种运算。编写一个完整的程序(包括测试各种运算符的程序部分)。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ab+bc)I,而两复数相除的计算公式为:(a+bi)/

4、(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。#include using namespace std; class complexpublic: complex(int i=0, int j=0):real(i), image(j) complex(const complex &a): real(a.real), image(a.image) complex& operator=(const complex &a) real = a.real; image = a.image; return *this; complex& operator+=(co

5、nst complex &a) real = real + a.real; image = image + a.image; return *this; void display() cout ( real , image ) endl; friend complex operator+(const complex&, const complex&); friend complex operator-(const complex&, const complex&); friend complex operator*(const complex&, const complex&); friend

6、 complex operator/(const complex&, const complex&);private: int real; int image; complex operator+(const complex &a, const complex &b) complex r(a); r.real += b.real; r.image += b.image; return r; complex operator-(const complex &a, const complex &b) complex r; r.real = r.real - b.real; r.image = r.

7、image - b.image; return r; complex operator *(const complex &c1, const complex &c2) complex t; t.real=c1.real * c2.real - c1.image * c2.image; t.image = c1.image*c2.real +c1.real* c2.image; return t; complex operator/(const complex &c1, const complex &c2) complex t; t.real = (c1.real*c2.real + c1.im

8、age*c2.image) / (c2.real*c2.real+c2.image*c2.image); t.image = (c2.image*c2.real - c1.real*c2.image) / (c2.real*c2.real + c2.image*c2.image); return t; int main() complex a(32,6); complex b(23,63); complex c; c=a+b; c.display(); a+=b; a.display(); c=a-b; c.display(); c=a*b; c.display(); c=a/b; c.dis

9、play(); return 0;39、定义一个学生类,数据成员包括:姓名、学号、C+、英语和数学成绩。重载运算符“”,实现学生对象的直接输入输出。增加转换函数,实现姓名的转换。设计一个完整的程序,验证成员函数和重载运算符的正确性。#includeusing namespace std; class studentpublic: student(char *n= ):number(201),cpp(0.0),eng(0.0),math(0.0) void set(char *nm,char *num,float _cpp,float ma,float _eng) name=nm; mumber

10、=num; cpp=_cpp; math=ma; eng=_eng; friend ostream& operator(ostream &in, student &b);private: char name20; char number20; float cpp; float math; float eng; ostream& operator(ostream &out, const student &a) out a.name a.number a.cpp a.math a.eng (ostream &in, student &b) in a.name a.number a.cpp a.ma

11、th a.eng; int main() student a; a.set(linda,201,34,64,64); cout b; cout b;40、定义一个平面直角坐标系上的一个点的类CPoint,重载“+”和“-”运算符,并区分这两种运算符的前置和后置运算,构造一个完整的程序。#include using namespace std; class CPointpublic: CPoint(int i=0, int j=0): x(i), y(j) CPoint& operator+(); /前置 CPoint operator+(int); /后置 CPoint& operator-(

12、); /前置 CPoint operator-(int); /后置 void display();private: int x; int y; CPoint& CPoint:operator +() +x; +y; return *this; CPoint& CPoint:operator -() -x; -y; return *this; CPoint CPoint:operator +(int) CPoint ret(*this); +x; +y; return ret; CPoint CPoint:operator -(int) CPoint ret(*this); -x; -y; re

13、turn ret; void CPoint:display() cout x y endl; int main() CPoint dot1(2,5); +dot1; dot1.display(); CPoint dot2(1,2); -dot2; dot2.display(); CPoint dot3(2,2); dot3+; dot3.display(); return 0;41、在上题的基础上,为其定义友元函数实现重载运算符“+”。#include using namespace std; class CPointpublic: CPoint(int i=0, int j=0): x(i)

14、, y(j) CPoint& operator+(); /前置 CPoint operator+(int); /后置 CPoint& operator-(); /前置 CPoint operator-(int); /后置 void display(); friend CPoint operator+(const CPoint &a, const CPoint &b);private: int x; int y; CPoint& CPoint:operator +() +x; +y; return *this; CPoint& CPoint:operator -() -x; -y; return

15、 *this; CPoint CPoint:operator +(int) CPoint ret(*this); +x; +y; return ret; CPoint CPoint:operator -(int) CPoint ret(*this); -x; -y; return ret; void CPoint:display() cout x y endl; CPoint operator+(const CPoint &a, const CPoint &b) CPoint t(a); t.x += b.x; t.y += b.y; return t; int main() CPoint dot1(2,5); +dot1; dot1.display(); CPoint dot2(1,2); -dot2; dot2.display(); CPoint dot3(2,2); dot3+; dot3.display(); CPoint dot4; dot4 = dot1 + dot2; dot4.display(); return 0;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1