当前位置:   article > 正文

第四章——运算符重载运算课后练习题_c++程序设计04737第四章运算符重载习题

c++程序设计04737第四章运算符重载习题

练习题1

解题思路:题目中描述是非成员,非友元的普通函数,又要实现复数的运算。第一感觉应该把real和imag设置成public,但是这样就破坏了C++的封装性,所以,我设置了一个getReal()和getImag()的函数,获取real和imag数据

  1. //类声明文件
  2. #include<iostream>
  3. using namespace std;
  4. class Complex
  5. {
  6. public:
  7. //构造函数
  8. Complex();
  9. //构造构造函数
  10. Complex(int ,int );
  11. int getReal();
  12. int getImag();
  13. void display();
  14. private:
  15. int real;
  16. int imag;
  17. };
  18. //类实现文件
  19. #include<iostream>
  20. #include "complex.h"
  21. using namespace std;
  22. Complex::Complex():real(0),imag(0){}
  23. Complex::Complex(int r,int i):real(r),imag(i){}
  24. int Complex::getReal()
  25. {
  26. return this->real;
  27. }
  28. int Complex::getImag()
  29. {
  30. return this->imag;
  31. }
  32. void Complex::display()
  33. {
  34. cout<<this->real<<" + "<<this->imag<<"i"<<endl;
  35. }
  36. //主函数文件
  37. #include<iostream>
  38. #include "complex.h"
  39. using namespace std;
  40. Complex operator +( Complex &complex1, Complex &complex2)
  41. {
  42. return (Complex(complex1.getReal()+complex2.getReal(),complex1.getImag()+complex2.getImag()));
  43. }
  44. int main()
  45. {
  46. class Complex complex1(3,30),complex2(5,20);
  47. Complex complex3 = complex1 + complex2;
  48. complex3.display();
  49. system("pause");
  50. return 0;
  51. }

练习题2

  1. //类声明文件
  2. #include<iostream>
  3. using namespace std;
  4. class Complex
  5. {
  6. public:
  7. //构造函数
  8. Complex();
  9. //构造构造函数
  10. Complex(int ,int );
  11. Complex operator +( Complex &complex);
  12. Complex operator -( Complex &complex);
  13. Complex operator *( Complex &complex);
  14. Complex operator /( Complex &complex);
  15. int getReal();
  16. int getImag();
  17. void display();
  18. private:
  19. int real;
  20. int imag;
  21. };
  22. //类实现文件
  23. #include<iostream>
  24. #include "complex.h"
  25. using namespace std;
  26. Complex::Complex():real(0),imag(0){}
  27. Complex::Complex(int r,int i):real(r),imag(i){}
  28. Complex Complex::operator +( Complex &complex)
  29. {
  30. return (Complex(this->real + complex.real,this->imag + complex.imag));
  31. }
  32. Complex Complex::operator -( Complex &complex)
  33. {
  34. return (Complex(this->real - complex.real,this->imag - complex.imag));
  35. }
  36. Complex Complex::operator *( Complex &complex)
  37. {
  38. return (Complex(this->real * complex.real - this->imag * complex.imag,this->real * complex.imag + this->imag * complex.real));
  39. }
  40. Complex Complex::operator /( Complex &complex)
  41. {
  42. int Denominator = complex.real * complex.real + complex.imag * complex.imag;
  43. if(Denominator != 0)
  44. {
  45. return (Complex((this->real * complex.real + this->imag * complex.imag)/Denominator,(this->imag * complex.real - this->real * complex.imag)/Denominator));
  46. }
  47. }
  48. int Complex::getReal()
  49. {
  50. return this->real;
  51. }
  52. int Complex::getImag()
  53. {
  54. return this->imag;
  55. }
  56. void Complex::display()
  57. {
  58. cout<<this->real<<" + "<<this->imag<<"i"<<endl;
  59. }
  60. //主函数文件
  61. #include<iostream>
  62. #include "complex.h"
  63. using namespace std;
  64. int main()
  65. {
  66. class Complex complex1(3,30),complex2(5,20);
  67. Complex complex3 = complex1 + complex2;
  68. complex3.display();
  69. Complex complex4 = complex1 - complex2;
  70. complex4.display();
  71. Complex complex5 = complex1 * complex2;
  72. complex5.display();
  73. Complex complex6 = complex1 / complex2;
  74. complex6.display();
  75. system("pause");
  76. return 0;
  77. }

练习题3

解题思路:根据题目描述,可以决定采用友元函数+转换构造函数可以实现加法交换律

  1. //类声明文件
  2. #include<iostream>
  3. using namespace std;
  4. class Complex
  5. {
  6. public:
  7. //构造函数
  8. Complex();
  9. //构造构造函数
  10. Complex(int ,int );
  11. //转换构造函数
  12. Complex(int );
  13. friend Complex operator +( const Complex &,const Complex &);
  14. int getReal();
  15. int getImag();
  16. void display();
  17. private:
  18. int real;
  19. int imag;
  20. };
  21. //类实现文件
  22. #include<iostream>
  23. #include "complex.h"
  24. using namespace std;
  25. Complex::Complex():real(0),imag(0){}
  26. Complex::Complex(int r):real(r),imag(0){}// 转换构造函数
  27. Complex::Complex(int r,int i):real(r),imag(i){}
  28. Complex operator +( const Complex &complex1,const Complex &complex2)
  29. {
  30. return (Complex(complex1.real + complex2.real,complex1.imag + complex2.imag));
  31. }
  32. int Complex::getReal()
  33. {
  34. return this->real;
  35. }
  36. int Complex::getImag()
  37. {
  38. return this->imag;
  39. }
  40. void Complex::display()
  41. {
  42. cout<<this->real<<" + "<<this->imag<<"i"<<endl;
  43. }
  44. //主函数文件
  45. #include<iostream>
  46. #include "complex.h"
  47. using namespace std;
  48. int main()
  49. {
  50. class Complex complex1(3,30),complex2(5,20);
  51. Complex complex3 = complex1 + complex2;
  52. complex3.display();
  53. complex3 = 2 + complex2;
  54. complex3.display();
  55. complex3 = complex2 + 2;
  56. complex3.display();
  57. system("pause");
  58. return 0;
  59. }

练习题4+5

解题思路:在矩阵中我用了二维数组指针,同时还用了指针的指针。在析构函数中释放指针的指针,在浅拷贝中容易造成野指针,所以我使用了复制构造函数,解决上述问题。

指针的指针接收指针数组的过程,参考博客:https://blog.csdn.net/ChaoFeiLi/article/details/103674308

  1. //类声明文件
  2. #include<iostream>
  3. using namespace std;
  4. class Matrix
  5. {
  6. public:
  7. Matrix(int = 1,int = 1);
  8. Matrix(const Matrix&);
  9. friend Matrix operator +(const Matrix&,const Matrix&);
  10. friend ostream& operator <<(ostream&,const Matrix&);
  11. friend istream& operator >>(istream&,const Matrix&);
  12. void set();
  13. void set(double **,int,int);
  14. void display();
  15. ~Matrix(){
  16. delete []ptr;
  17. }
  18. protected:
  19. float **ptr;
  20. private:
  21. int row;
  22. int column;
  23. };
  24. //类实现文件
  25. #include<iostream>
  26. #include "matrix.h"
  27. using namespace std;
  28. Matrix::Matrix(int r ,int c)
  29. {
  30. if(r ==0 || c == 0)
  31. {
  32. return;
  33. }
  34. row = r;
  35. column = c;
  36. ptr = new float* [row];
  37. for(int i = 0; i < row; i++)
  38. {
  39. ptr[i] = new float [column];
  40. }
  41. }
  42. Matrix::Matrix(const Matrix&matrix)
  43. {
  44. this->row = matrix.row;
  45. this->column = matrix.column;
  46. this->ptr = new float*[row];
  47. for(int i = 0;i<row;i++)
  48. {
  49. ptr[i] = new float[column];
  50. for(int j = 0;j<column;j++)
  51. {
  52. this->ptr[i][j] = matrix.ptr[i][j];
  53. }
  54. }
  55. }
  56. Matrix operator +(const Matrix &matrix1,const Matrix &matrix2)
  57. {
  58. int row = matrix1.row;
  59. int column = matrix1.column;
  60. Matrix matrix(row,column);
  61. for(int i = 0;i < row;i++)
  62. {
  63. for(int j = 0;j < column;j++)
  64. {
  65. matrix.ptr[i][j] = matrix1.ptr[i][j] + matrix2.ptr[i][j];
  66. }
  67. }
  68. return matrix;
  69. }
  70. ostream& operator <<(ostream &output,const Matrix &matrix)
  71. {
  72. int row = matrix.row;
  73. int column = matrix.column;
  74. for(int i = 0;i < row;i++)
  75. {
  76. for(int j = 0;j < column;j++)
  77. {
  78. output<<matrix.ptr[i][j]<<" ";
  79. }
  80. output<<endl;
  81. }
  82. return output;
  83. }
  84. istream& operator >>(istream& input,const Matrix&matrix)
  85. {
  86. int row = matrix.row;
  87. int column = matrix.column;
  88. for(int i = 0;i < row;i++)
  89. {
  90. for(int j = 0;j < column;j++)
  91. {
  92. input>>matrix.ptr[i][j];
  93. }
  94. }
  95. return input;
  96. }
  97. void Matrix::set()
  98. {
  99. for(int i = 0;i < row;i++)
  100. {
  101. for(int j = 0;j < column;j++)
  102. {
  103. cin>>ptr[i][j];
  104. }
  105. }
  106. }
  107. void Matrix::set(double **p,int r,int c)
  108. {
  109. ptr = new float *[row];
  110. for (int i = 0;i < row;i++)
  111. {
  112. ptr[i] = new float[column];
  113. for (int j = 0;i<column;j++)
  114. {
  115. ptr[i][j] = p[i][j];
  116. }
  117. }
  118. }
  119. void Matrix::display()
  120. {
  121. for(int i = 0;i < row;i++)
  122. {
  123. for(int j = 0;j < column;j++)
  124. {
  125. cout<<ptr[i][j]<<" ";
  126. }
  127. cout<<endl;
  128. }
  129. }
  130. //主函数文件
  131. #include<iostream>
  132. #include "matrix.h"
  133. using namespace std;
  134. int main()
  135. {
  136. Matrix matrix1(2,3);
  137. cin>>matrix1;
  138. cout<<endl;
  139. Matrix matrix2(2,3);
  140. cin>>matrix2;
  141. Matrix matrix = matrix1 + matrix2;
  142. cout<<matrix;
  143. system("pause");
  144. return 0;
  145. }

练习题6

  1. //类声明文件
  2. #include<iostream>
  3. using namespace std;
  4. class Complex
  5. {
  6. public:
  7. //构造函数
  8. Complex();
  9. //构造构造函数
  10. Complex(double,double);
  11. //转换构造函数
  12. Complex(double);
  13. //类型转换函数
  14. operator double() const;
  15. void display();
  16. private:
  17. double real;
  18. double imag;
  19. };
  20. //类实现文件
  21. #include<iostream>
  22. #include "complex.h"
  23. using namespace std;
  24. Complex::Complex():real(0),imag(0){}
  25. Complex::Complex(double r):real(r),imag(0){}
  26. Complex::Complex(double r,double i):real(r),imag(i){}
  27. void Complex::display()
  28. {
  29. cout<<this->real<<" + "<<this->imag<<"i"<<endl;
  30. }
  31. Complex::operator double() const
  32. {
  33. return this->real;
  34. }
  35. //主函数文件
  36. #include<iostream>
  37. #include "complex.h"
  38. using namespace std;
  39. int main()
  40. {
  41. class Complex complex1(3,30);
  42. double d1 = 2.5;
  43. d1 = d1 + complex1;
  44. cout<<d1<<endl;
  45. Complex(d1).display();
  46. system("pause");
  47. return 0;
  48. }

练习题7

必须把class Student中的成员变量设置为public的,或者通过get函数获取;在转换构造函数中无法使用

  1. //类声明文件
  2. #include <iostream>
  3. class Student
  4. {
  5. public:
  6. Student(int,char [],char);
  7. void display();
  8. int num;
  9. char name[50];
  10. char sex;
  11. };
  12. class Teacher
  13. {
  14. public:
  15. Teacher(int,char [],char);
  16. //转换构造函数
  17. Teacher(const Student&);
  18. void display();
  19. private:
  20. int num;
  21. char name[50];
  22. char sex;
  23. };
  24. //类实现文件
  25. #include <iostream>
  26. #include "worker.h"
  27. using namespace std;
  28. Student::Student(int n,char na[],char s)
  29. {
  30. this->num = n;
  31. strcpy(this->name, na);
  32. this->sex = s;
  33. }
  34. void Student::display()
  35. {
  36. cout <<name<<endl;
  37. }
  38. Teacher::Teacher(int n,char na[],char s)
  39. {
  40. this->num = n;
  41. strcpy(this->name, na);
  42. this->sex = s;
  43. }
  44. Teacher::Teacher(const Student &student)
  45. {
  46. this->num = student.num;
  47. strcpy(this->name, student.name);
  48. this->sex = student.sex;
  49. }
  50. void Teacher::display()
  51. {
  52. cout<<this->name<<" "<<this->num<<" "<<this->sex<<endl;
  53. }
  54. //主函数文件
  55. #include<iostream>
  56. #include "worker.h"
  57. using namespace std;
  58. int main()
  59. {
  60. class Student student1(2017,"andrew",'m');
  61. student1.display();
  62. class Teacher teacher1(student1);
  63. teacher1.display();
  64. system("pause");
  65. return 0;
  66. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/806840
推荐阅读
相关标签
  

闽ICP备14008679号