码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++程序设计第十章课后习题答案——运算符重载练习


    目录

    一、对运算符重载的方法

    1、运算符重载函数作为类成员函数

    2、运算符重载函数作为友元函数

    二、重载流插入运算符“<<”和流提取运算符“>>”

    1、重载流插入运算符“<<”

    2、重载流提取运算符“>>”

    三、不同数据间的类型转换

    1、定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编程序,求两个复数之和。

    2、定义一个复数类Complex,重载运算符“+”、“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序,分别计算两个复数之和、差、积和商。

    3、定义一个复数类Complex,重载运算符“+”,使之能用于复数的假发运算。参加运算的两个运算量可以都是类对象,也可以其中一个是整数,顺序任意。编写程序,分别求两个复数之和、整数和复数之和。

    4、有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。

    5、有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加,如c=a+b。重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。

    6、请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个douuble型的变量毒d1中,输出d1的值,再以复数形式输出此值。定义Complex类,在成员函数中包含重载类型转换运算符:operator double(){return real;}

    7、定义一个teacher(教师)类和一个student(学生)类,二者有一份数据成员是相同的,例如num,name,sex。编写程序,将一个student对象(学生)转换为teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为:一个学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。


    一、对运算符重载的方法

    1、运算符重载函数作为类成员函数

    • 定义类 operator+(定义类 &);
    • 当c1+c2编译器解释为c1.operator+(c2)
    • 如果将运算符重载作为成员函数,它可以通过this指针自由地地址访问本类的数据成员,因此可以少写一个函数的参数。

    2、运算符重载函数作为友元函数

    • friend 定义类 operator+(定义类 &,定义类 &);
    • 当c1+c2编译器解释为operator+(c1,c2)
    • 双目运算符重载为友元函数时,由于友元函数不是该类的成员函数,因此在函数的形参列表中必须有两个参数,不能省略。

    二、重载流插入运算符“<<”和流提取运算符“>>”

    1、重载流插入运算符“<<”

    • friend istream& operator>>(istream&,Arr&);

    2、重载流提取运算符“>>”

    • friend ostream& operator<<(ostream&,Arr&);

    三、不同数据间的类型转换

    1、转换构造函数

    • Complex(double r){real=r;imag=0;}
    • 转换构造函数只能有一个参数。如果有多个参数的,它就不是转换构造函数。

    2、类型转换函数

    • operator 类型名(){实现转换的语句}
    • 类型转换函数只能作为成员函数,因为转换的主体是本类的对象。不能作为友元函数或普通函数。

    1、定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编程序,求两个复数之和。

    1. #include
    2. using namespace std;
    3. //习题 1
    4. class Complex{
    5. public:
    6. Complex(){real=0;imag=0;}
    7. Complex(double r,double i){real=r;imag=i;}
    8. double get_real(){return real;}
    9. double get_imag(){return imag;}
    10. void display();
    11. private:
    12. double real;
    13. double imag;
    14. };
    15. void Complex::display() {
    16. cout<<"("<","<"i)"<
    17. }
    18. Complex operator + (Complex &c1,Complex &c2)
    19. {
    20. return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
    21. }
    22. int main(){
    23. Complex c1(3,1),c2(2,-7),c3;
    24. cout<<"c1=";c1.display();
    25. cout<<"c2=";c2.display();
    26. cout<<"c1+c2=";c3.display();
    27. }

    2、定义一个复数类Complex,重载运算符“+”、“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序,分别计算两个复数之和、差、积和商。

    1. //习题 2
    2. class Complex{
    3. public:
    4. Complex(){real=0;imag=0;}
    5. Complex(double r,double i){real=r;imag=i;}
    6. Complex operator+(Complex &c2);
    7. Complex operator-(Complex &c2);
    8. Complex operator*(Complex &c2);
    9. Complex operator/(Complex &c2);
    10. void display();
    11. private:
    12. double real;
    13. double imag;
    14. };
    15. Complex Complex::operator+(Complex &c2) {
    16. return Complex(real+c2.real,imag+c2.imag);
    17. }
    18. Complex Complex::operator-(Complex &c2) {
    19. return Complex(real-c2.real,imag-c2.imag);
    20. }
    21. Complex Complex::operator*(Complex &c2) {
    22. return Complex(real*c2.real-imag*c2.imag,imag*c2.real+real*c2.imag);
    23. }
    24. Complex Complex::operator/(Complex &c2) {
    25. return Complex((real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(imag*c2.real+real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));
    26. }
    27. void Complex ::display() {
    28. cout<<"("<","<"i)"<
    29. }

    3、定义一个复数类Complex,重载运算符“+”,使之能用于复数的假发运算。参加运算的两个运算量可以都是类对象,也可以其中一个是整数,顺序任意。编写程序,分别求两个复数之和、整数和复数之和。

    1. //习题 3
    2. class Complex{
    3. public:
    4. Complex(){real=0;imag=0;}
    5. Complex(double r,double i){real=r;imag=i;}
    6. friend Complex operator+(Complex &c1,Complex &c2);
    7. friend Complex operator+(int &i,Complex &c1);
    8. friend Complex operator+(Complex &c1,int &i);
    9. void display();
    10. private:
    11. double real;
    12. double imag;
    13. };
    14. Complex operator+(Complex &c1,Complex &c2){
    15. return Complex(c1.real+c2.real,c1.imag+c2.imag);
    16. }
    17. Complex operator+(Complex &c1,int &i){
    18. return Complex(c1.real+i,c1.imag);
    19. }
    20. Complex operator+(int &i,Complex&c1){
    21. return Complex(i+c1.real,c1.imag);
    22. }
    23. void Complex::display() {
    24. cout<<"("<","<"i)"<
    25. }
    26. int main(){
    27. Complex c1(2,3),c2(2,9),c3;
    28. int i=2;
    29. cout<<"c1=";c1.display();
    30. cout<<"c2=";c2.display();
    31. c3=c1+c2;
    32. cout<<"c1+c2=";c3.display();
    33. c3=i+c1;
    34. cout<<"i+c1=";c3.display();
    35. c3=c1+i;
    36. cout<<"c1+i=";c3.display();
    37. }

    4、有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。

    1. //习题 4
    2. #include
    3. class Arr{
    4. public:
    5. Arr();
    6. friend Arr operator+(Arr &a1,Arr &a2);
    7. void inp();
    8. void out();
    9. private:
    10. int arr[2][3];
    11. };
    12. Arr::Arr() {
    13. for (int i = 0; i < 2; ++i)
    14. for (int j = 0; j < 3; ++j)
    15. arr[i][j]=0;
    16. }
    17. Arr operator+(Arr&a1,Arr&a2){
    18. Arr a3;
    19. for (int i = 0; i < 2; ++i)
    20. for (int j = 0; j < 3; ++j)
    21. a3.arr[i][j]=a1.arr[i][j]+a2.arr[i][j];
    22. return a3;
    23. }
    24. void Arr::inp() {
    25. for (int i = 0; i < 2; ++i)
    26. for (int j = 0; j < 3; ++j)
    27. cin>>arr[i][j];
    28. }
    29. void Arr::out() {
    30. for (int i = 0; i < 2; ++i){
    31. for (int j = 0; j < 3; ++j)
    32. cout << setw(4) << arr[i][j];
    33. cout<
    34. }
    35. }
    36. int main(){
    37. Arr a1 ,a2, a3;
    38. a1.inp();
    39. a2.inp();
    40. a3=a1+a2;
    41. a3.out();
    42. }
    43. /*
    44. 1 2 3 4 5 6
    45. 1 2 3 4 5 6
    46. 2 4 6
    47. 8 10 12
    48. */

    5、有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加,如c=a+b。重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。

    1. //习题 5
    2. #include
    3. using namespace std;
    4. class Arr {
    5. public :
    6. friend ostream& operator<<(ostream&,Arr&);
    7. friend istream& operator>>(istream&,Arr&);
    8. friend Arr operator + (Arr & c1,Arr & c2);
    9. Arr();
    10. private :
    11. int Q[2][3];
    12. };
    13. Arr::Arr() {
    14. for(int i=0; i<2; i++)
    15. for(int j=0; j<3; j++)
    16. Q[i][j]=0;
    17. }
    18. ostream & operator <<(ostream & output,Arr& a) {
    19. for (int i=0; i<2; i++) {
    20. for (int j=0; j<3; j++)
    21. output<<setw(4)<
    22. output<
    23. }
    24. return output;
    25. }
    26. istream & operator >>(istream & input,Arr& a) {
    27. for (int i=0; i<2; i++)
    28. for (int j=0; j<3; j++)
    29. input>>a.Q[i][j];
    30. return input;
    31. }
    32. Arr operator +(Arr & a1,Arr & a2) {
    33. Arr a3;
    34. for (int i=0; i<2; i++)
    35. for (int j=0; j<3; j++)
    36. a3.Q[i][j]=a1.Q[i][j]+a2.Q[i][j];
    37. return a3;
    38. }
    39. int main() {
    40. Arr a1,a2,a3;
    41. cin>>a1>>a2;
    42. a3=a1+a2;
    43. cout<
    44. return 0;
    45. }

    6、请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个douuble型的变量毒d1中,输出d1的值,再以复数形式输出此值。定义Complex类,在成员函数中包含重载类型转换运算符:operator double(){return real;}

    1. //习题 6
    2. class Complex{
    3. public:
    4. Complex(){real=0;imag=0;}
    5. Complex(double r,double i){real=r;imag=i;}
    6. Complex(double d1){real=d1;imag=0;}
    7. operator double (){return real;}
    8. void out(){cout<"+"<"i";}
    9. private:
    10. double real;
    11. double imag;
    12. };
    13. int main(){
    14. Complex c1(2,3),c2;
    15. double d1;
    16. d1=2.5+c1;
    17. cout<
    18. c2=Complex(d1);
    19. c2.out();
    20. return 0;
    21. }

    7、定义一个teacher(教师)类和一个student(学生)类,二者有一份数据成员是相同的,例如num,name,sex。编写程序,将一个student对象(学生)转换为teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为:一个学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。

    1. //习题 7
    2. class Student{
    3. public:
    4. Student(){num=10;name="王";sex='M';}
    5. void display(){
    6. cout<
    7. cout<
    8. cout<
    9. }
    10. private:
    11. int num;
    12. string name;
    13. char sex;
    14. };
    15. class Teacher:public Student{};
    16. int main(){
    17. Teacher t1;
    18. t1.display();
    19. }

  • 相关阅读:
    Webpack面试题总结
    OpenAI开发者大会大模型圈开卷AI Agent? 实在智能布局前瞻已下“先手棋”
    百度百科怎么快速创建人物词条?教你几招技巧!
    gitlab 合并分支
    web前端期末大作业实例 (1500套) 集合
    jitsi音视频会议集成到自己的网页中
    0046【Edabit ★☆☆☆☆☆】【长方形面积】Area of a Rectangle
    MySQL的预编译入门
    一种使用setdll+HOOK钩子技术+dll为MFC程序ProtocalTool等老旧程序打补丁的思路(含源码)
    怎样把英语视频字幕翻译成中文
  • 原文地址:https://blog.csdn.net/qq_21402983/article/details/127717501
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号