当前位置:   article > 正文

c++复习4.运算符重载(实验四)_以下等式中涉及到运算符,需要如何定义运算符重载函数?c1=c2+c3;cl=i+c2;c1=c2

以下等式中涉及到运算符,需要如何定义运算符重载函数?c1=c2+c3;cl=i+c2;c1=c2

实验四 多态和动态绑定

一.实验目的

1.理解并掌握运算符重载为成员函数的原理和方法;

2.理解并掌握运算符重载为非成员函数的原理和方法;

3.理解并掌握虚函数的原理和使用方法;

二. 实验内容

1. 理解并掌握运算符重载为成员函数的原理和方法

1.根据下列复数类的定义,请将复数的乘法重载为类的成员函数,以实现在主函数中能够直接进行复数的乘法运算。

#include <iostream>

using namespace std;

class Complex {  //复数类定义

public:   //外部接口

   Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { }   //构造函数

   Complex operator + (const Complex &c2) const; //运算符+重载成员函数

   Complex operator - (const Complex &c2) const; //运算符-重载成员函数

   void display() const;   //输出复数

private:  //私有数据成员

   double real;  //复数实部

   double imag;  //复数虚部

};

Complex Complex::operator + (const Complex &c2) const {   //重载运算符函数实现

   return Complex(real + c2.real, imag + c2.imag); //创建一个临时无名对象作为返回值

}

Complex Complex::operator - (const Complex &c2) const {   //重载运算符函数实现

   return Complex(real - c2.real, imag - c2.imag); //创建一个临时无名对象作为返回值

}

void Complex::display() const {

   cout << "(" << real << ", " << imag << ")" << endl;

}

int main() {  //主函数

   Complex c1(5, 4), c2(2, 10), c3;  //定义复数类的对象

   cout << "c1 = "; c1.display();

   cout << "c2 = "; c2.display();

   c3 = c1 - c2; //使用重载运算符完成复数减法

   cout << "c3 = c1 - c2 = "; c3.display();

   c3 = c1 + c2; //使用重载运算符完成复数加法

   cout << "c3 = c1 + c2 = "; c3.display();

   return 0;

}

完成以下内容:仿照上述的复数加减法运算程序,增加复数的乘法运算( (a+bi)*(c+di)=(ac-bd)+(ad+bc)i)重载,并在主函数中进行测试(c3=c1*c2)。给出程序的完整源代码,并粘贴运行结果截图。

  1. #include <iostream>
  2. using namespace std;
  3. class Complex { //复数类定义
  4. public: //外部接口
  5. Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { } //构造函数
  6. Complex operator + (const Complex &c2) const; //运算符+重载成员函数
  7. Complex operator - (const Complex &c2) const; //运算符-重载成员函数
  8. Complex operator * (const Complex &c2) const;
  9. void display() const; //输出复数
  10. private: //私有数据成员
  11. double real; //复数实部
  12. double imag; //复数虚部
  13. };
  14. Complex Complex::operator + (const Complex &c2) const { //重载运算符函数实现
  15. return Complex(real + c2.real, imag + c2.imag); //创建一个临时无名对象作为返回值
  16. }
  17. Complex Complex::operator - (const Complex &c2) const { //重载运算符函数实现
  18. return Complex(real - c2.real, imag - c2.imag); //创建一个临时无名对象作为返回值
  19. }
  20. Complex Complex::operator * (const Complex &c2) const { //重载运算符函数实现
  21. return Complex(real*c2.real-imag*c2.imag, real*c2.imag+imag*c2.real); //创建一个临时无名对象作为返回值
  22. }
  23. void Complex::display() const {
  24. cout << "(" << real << ", " << imag << ")" << endl;
  25. }
  26. int main() { //主函数
  27. Complex c1(5, 4), c2(2, 10), c3; //定义复数类的对象
  28. cout << "c1 = "; c1.display();
  29. cout << "c2 = "; c2.display();
  30. c3 = c1 - c2; //使用重载运算符完成复数减法
  31. cout << "c3 = c1 - c2 = "; c3.display();
  32. c3 = c1 + c2; //使用重载运算符完成复数加法
  33. cout << "c3 = c1 + c2 = "; c3.display();
  34. c3 = c1 * c2; //使用重载运算符完成复数乘法
  35. cout << "c3 = c1 * c2 = "; c3.display();
  36. return 0;
  37. }

运行结果截图:

2.理解并掌握运算符重载为非成员函数的原理和方法

2.请将上面题目的复数乘法运算重载为复数类的友元函数来实现,具体要求同上一题。

  1. #include <iostream>
  2. using namespace std;
  3. class Complex;
  4. Complex operator + (const Complex &c1, const Complex &c2); //运算符+重载非成员函数
  5. Complex operator - (const Complex &c1, const Complex &c2); //运算符-重载非成员函数
  6. Complex operator * (const Complex &c1, const Complex &c2); //运算符*重载非成员函数
  7. class Complex { //复数类定义
  8. public: //外部接口
  9. Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { } //构造函数
  10. friend Complex operator + (const Complex &c1, const Complex &c2); //运算符+重载成员函数
  11. friend Complex operator - (const Complex &c1, const Complex &c2); //运算符-重载成员函数
  12. friend Complex operator * (const Complex &c1, const Complex &c2);
  13. void display() const; //输出复数
  14. private: //私有数据成员
  15. double real; //复数实部
  16. double imag; //复数虚部
  17. };
  18. Complex operator + (const Complex &c1, const Complex &c2){ //重载运算符函数实现
  19. return Complex(c1.real + c2.real, c1.imag + c2.imag); //创建一个临时无名对象作为返回值
  20. }
  21. Complex operator - (const Complex &c1, const Complex &c2){ //重载运算符函数实现
  22. return Complex(c1.real - c2.real, c1.imag - c2.imag); //创建一个临时无名对象作为返回值
  23. }
  24. Complex operator * (const Complex &c1, const Complex &c2){ //重载运算符函数实现
  25. return Complex(c1.real*c2.real-c1.imag*c2.imag, c1.real*c2.imag+c1.imag*c2.real); //创建一个临时无名对象作为返回值
  26. }
  27. void Complex::display() const {
  28. cout << "(" << real << ", " << imag << ")" << endl;
  29. }
  30. int main() { //主函数
  31. Complex c1(5, 4), c2(2, 10), c3; //定义复数类的对象
  32. cout << "c1 = "; c1.display();
  33. cout << "c2 = "; c2.display();
  34. c3 = c1 - c2; //使用重载运算符完成复数减法
  35. cout << "c3 = c1 - c2 = "; c3.display();
  36. c3 = c1 + c2; //使用重载运算符完成复数加法
  37. cout << "c3 = c1 + c2 = "; c3.display();
  38. c3 = c1 * c2; //使用重载运算符完成复数乘法
  39. cout << "c3 = c1 * c2 = "; c3.display();
  40. return 0;
  41. }

运行结果截图:

3. 理解并掌握运算符重载的原理及方法

3.对给定的Point类,请重载++(自增),--(自减)运算符,要求同时重载

前缀和后缀的形式。

#include <iostream>

using namespace std;

class Point

{

public:

   Point& operator++();

   Point operator++(int);

   Point& operator--();

   Point operator--(int);

   Point() { _x = _y = 0; }

   int x() { return _x; }

   int y() { return _y; }

private:

   int _x, _y;

};

//给出前置++:perator++()的实现

Point& Point::operator++()

{

}

//给出后置++:operator++(int)的实现

Point Point::operator++(int)

{

  

}

//给出前置--:perator--()的实现

Point& Point::operator--()

{

}

//给出后置--:perator--(int)的实现

Point Point::operator--(int)

{

}

int main()

{

   Point a;

   cout << "a的值为:" << a.x() << " , " << a.y() << endl;

   a++;

   cout << "a的值为:" << a.x() << " , " << a.y() << endl;

   ++a;

   cout << "a的值为:" << a.x() << " , " << a.y() << endl;

   a--;

   cout << "a的值为:" << a.x() << " , " << a.y() << endl;

   --a;

   cout << "a的值为:" << a.x() << " , " << a.y() << endl;

}

请给出程序完整源代码和运行结果截图。

  1. #include <iostream>
  2. using namespace std;
  3. class Point
  4. {
  5. public:
  6. Point& operator++();
  7. Point operator++(int);
  8. Point& operator--();
  9. Point operator--(int);
  10. Point() { _x = _y = 0; }
  11. int x() { return _x; }
  12. int y() { return _y; }
  13. private:
  14. int _x, _y;
  15. };
  16. //给出前置++:perator++()的实现
  17. Point& Point::operator++()
  18. {
  19. _x++;_y++;
  20. return *this;
  21. }
  22. //给出后置++:operator++(int)的实现
  23. Point Point::operator++(int)
  24. {
  25. Point old = *this;
  26. _x++;_y++;
  27. return old;
  28. }
  29. //给出前置--:perator--()的实现
  30. Point& Point::operator--()
  31. {
  32. _x--;_y--;
  33. return *this;
  34. }
  35. //给出后置--:perator--(int)的实现
  36. Point Point::operator--(int)
  37. {
  38. Point old = *this;
  39. _x--;_y--;
  40. return old;
  41. }
  42. int main()
  43. {
  44. Point a;
  45. cout << "a的值为:" << a.x() << " , " << a.y() << endl;
  46. a++;
  47. cout << "a的值为:" << a.x() << " , " << a.y() << endl;
  48. ++a;
  49. cout << "a的值为:" << a.x() << " , " << a.y() << endl;
  50. a--;
  51. cout << "a的值为:" << a.x() << " , " << a.y() << endl;
  52. --a;
  53. cout << "a的值为:" << a.x() << " , " << a.y() << endl;
  54. }

运行结果截图:

4. 理解并掌握虚函数的原理和使用方法

4.请编写一个抽象类Shape(其中含有getArea()和getPerim()两个成员函数),类Shape派生出Rectangle和Circle类,Rectangle和Circle类都有计算面积的函数getArea()、计算对象周长的函数getPerim()。请按照题目中的要求完善程序,给出程序源代码,并粘贴运行结果截图。

#include <iostream>

using namespace std;

//完善类Shape的定义

class Shape

{

public:

    Shape(){}

    ~Shape(){}

  

};

//完善Circle 的定义

class Circle : public Shape

{

public:

    Circle(float radius):itsRadius(radius){}

    ~Circle(){}

    private:

    float itsRadius;

};

//完善Rectangle的定义

class Rectangle : public Shape

{

public:

    Rectangle(float len, float width): itsLength(len), itsWidth(width){};

    ~Rectangle(){};

    private:

    float itsWidth;

    float itsLength;

};

int main()

{

    Shape * sp;

   sp = new Circle(5);

   cout << "The area of the Circle is " << sp->getArea () << endl;

   cout << "The perimeter of the Circle is " << sp->getPerim () << endl;

   delete sp;

   sp = new Rectangle(4,6);

   cout << "The area of the Rectangle is " << sp->getArea() << endl;

   cout << "The perimeter of the Rectangle is " << sp->getPerim () << endl;

   delete sp;

   return 0;

}

  1. #include <iostream>
  2. using namespace std;
  3. const float PI = 3.14;
  4. //完善类Shape的定义
  5. class Shape
  6. {
  7. public:
  8. Shape(){}
  9. ~Shape(){}
  10. virtual float getArea()=0;//此处需要定义为纯虚函数,不然不实现函数体会报错
  11. virtual float getPerim()=0;//此处需要定义为纯虚函数,不然不实现函数体会报错
  12. };
  13. //完善Circle 的定义
  14. class Circle : public Shape
  15. {
  16. public:
  17. Circle(float radius):itsRadius(radius){}
  18. ~Circle(){}
  19. virtual float getArea();
  20. virtual float getPerim();
  21. private:
  22. float itsRadius;
  23. };
  24. //完善Rectangle的定义
  25. class Rectangle : public Shape
  26. {
  27. public:
  28. Rectangle(float len, float width): itsLength(len), itsWidth(width){};
  29. ~Rectangle(){};
  30. virtual float getArea();
  31. virtual float getPerim();
  32. private:
  33. float itsWidth;
  34. float itsLength;
  35. };
  36. float Circle::getArea(){
  37. return itsRadius*itsRadius*PI;
  38. }
  39. float Circle::getPerim(){
  40. return itsRadius*PI*2;
  41. }
  42. float Rectangle::getArea(){
  43. return itsWidth*itsLength;
  44. }
  45. float Rectangle::getPerim(){
  46. return (itsLength+itsWidth)*2;
  47. }
  48. int main()
  49. {
  50. Shape * sp;
  51. sp = new Circle(5);
  52. cout << "The area of the Circle is " << sp->getArea () << endl;
  53. cout << "The perimeter of the Circle is " << sp->getPerim () << endl;
  54. delete sp;
  55. sp = new Rectangle(4,6);
  56. cout << "The area of the Rectangle is " << sp->getArea() << endl;
  57. cout << "The perimeter of the Rectangle is " << sp->getPerim () << endl;
  58. delete sp;
  59. return 0;
  60. }

运行结果截图:

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

闽ICP备14008679号