




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上34、运算符重载的含义是什么?是否所有的运算符都可以重载?答:运算符重载是对已有的运算符赋予多重含义,同一个运算符作用于不用类型的数据导致不同行为。不是。在C+语言中除了(点运算符(.)、作用域运算符(:)、条件运算符(?:)、成员指针运算符(*)、编译预处理命令的开始符号(#)五个运算符不能被重载外,其他运算符均可重载。35、运算符重载有哪两种形式?这两种形式有何区别?答:运算符重载有成员函数和友元函数两种形式。 区别:运算符作为成员函数重载时的参数比作为友元函数重载时少一个。例如使用成员函数重载双目运算符时只用一个参数,而使用友元函数重载时要用两个参数。 36、转
2、换函数的作用是什么?答: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 element; p
3、rivate: 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)/(c+di)=(ac
4、+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+=(const comple
5、x &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 complex o
6、perator/(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.image - b.
7、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.image*c2.ima
8、ge) / (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.display(); re
9、turn 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=num; cpp=
10、_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.number a.cpp a.math a.eng (ostream &in, student &b) in a.number a.cpp a.math a.eng;
11、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-(); /前置 CPo
12、int 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; return ret;
13、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), y(j) CPo
14、int& 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 *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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025届福建省福州琅岐中学高三(最后冲刺)化学试卷含解析
- 2025届黑龙江省大庆市红岗区大庆十中高三第二次模拟考试化学试卷含解析
- 候银匠课件第一
- 家庭健康知识授课
- 2025年机械设备批发服务项目建议书
- 2025年激光扫瞄显微镜项目发展计划
- 2025年电波特性测试仪器项目建议书
- 2025届科大附中高考化学二模试卷含解析
- 安徽省安庆第一中学2025年高三第二次模拟考试化学试卷含解析
- 2025年其他未列明电子设备项目合作计划书
- 【+初中语文+】《山地回忆》课件+统编版语文七年级下册
- 五年级英语下册 Unit 3 My school calendar Part B第二课时教学实录 人教PEP
- 2025-2030中国建筑装饰行业十四五发展分析及投资前景与战略规划研究报告
- 2025-2030中国奶牛智能项圈标签行业市场发展趋势与前景展望战略分析研究报告
- (一模)2025年广东省高三高考模拟测试 (一) 语文试卷语文试卷(含官方答案)
- 9.3-撒哈拉以南非洲 第2课时课件 七年级地理下册 人教版
- 河北省第八届关注时事胸怀天下知识竞赛题库及答案
- DB32T 5073.2-2025 政务“一朵云”安全管理体系规范 第2部分:密码应用技术要求
- 2023-2024学年广东省深圳市实验学校中学部八年级下学期期中英语试题及答案
- 拆除道牙和生态砖施工方案
- 三维网喷播植草施工方案
评论
0/150
提交评论