• 第11章 使用类


    11.1 运算符重载

    11.2 计算时间:一个运算符重载示例

    mytime.h

    1. #ifndef MYTIME_H_
    2. #define MYTIME_H_
    3. class Time
    4. {
    5. private:
    6. int hours;
    7. int minutes;
    8. public:
    9. Time();
    10. Time(int h,int m = 0);
    11. void AddMin(int m);
    12. void AddHr(int h);
    13. void Reset(int h=0,int m=0);
    14. Time Sum(const Time &t) const;
    15. Time operator+(const Time &t) const;
    16. Time operator*(double m) const;
    17. friend Time operator*(double m,const Time &t);
    18. void Show() const;
    19. };
    20. #endif

    mytime.cpp

    1. #include <iostream>
    2. #include "mytime.h"
    3. Time::Time()
    4. {
    5. hours = minutes = 0;
    6. }
    7. Time::Time(int h,int m)
    8. {
    9. hours = h;
    10. minutes = m;
    11. }
    12. void Time::AddMin(int m)
    13. {
    14. minutes += m;
    15. hours +=minutes/60;
    16. minutes = minutes%60;
    17. }
    18. void Time::AddHr(int h)
    19. {
    20. hours +=h;
    21. }
    22. void Time::Reset(int h,int m)
    23. {
    24. hours = h;
    25. minutes = m;
    26. }
    27. Time Time::Sum(const Time &t) const
    28. {
    29. Time sum;
    30. sum.minutes = minutes + t.minutes;
    31. sum.hours = t.hours +hours + sum.minutes/60;
    32. sum.minutes %= 60;
    33. return sum;
    34. }
    35. Time Time::operator+(const Time &t) const
    36. {
    37. Time sum;
    38. sum.minutes = minutes + t.minutes;
    39. sum.hours = t.hours +hours + sum.minutes/60;
    40. sum.minutes %= 60;
    41. return sum;
    42. }
    43. Time Time::operator*(double m) const
    44. {
    45. Time result;
    46. long totalmin = hours*60*m + minutes*m;
    47. result.hours = totalmin/60;
    48. result.minutes = totalmin%60;
    49. return result;
    50. }
    51. Time operator*(double m,const Time &t)
    52. {
    53. /*Time result;
    54. long totalmin = t.hours*60*m + t.minutes*m;
    55. result.hours = totalmin/60;
    56. result.minutes = totalmin%60;*/
    57. return t*m;
    58. }
    59. void Time::Show() const
    60. {
    61. std::cout <<hours << " hours, " << minutes << " min\n";
    62. }

    usetime.cpp

    1. #include <iostream>
    2. #include "mytime.h"
    3. Time::Time()
    4. {
    5. hours = minutes = 0;
    6. }
    7. Time::Time(int h,int m)
    8. {
    9. hours = h;
    10. minutes = m;
    11. }
    12. void Time::AddMin(int m)
    13. {
    14. minutes += m;
    15. hours +=minutes/60;
    16. minutes = minutes%60;
    17. }
    18. void Time::AddHr(int h)
    19. {
    20. hours +=h;
    21. }
    22. void Time::Reset(int h,int m)
    23. {
    24. hours = h;
    25. minutes = m;
    26. }
    27. Time Time::Sum(const Time &t) const
    28. {
    29. Time sum;
    30. sum.minutes = minutes + t.minutes;
    31. sum.hours = t.hours +hours + sum.minutes/60;
    32. sum.minutes %= 60;
    33. return sum;
    34. }
    35. Time Time::operator+(const Time &t) const
    36. {
    37. Time sum;
    38. sum.minutes = minutes + t.minutes;
    39. sum.hours = t.hours +hours + sum.minutes/60;
    40. sum.minutes %= 60;
    41. return sum;
    42. }
    43. Time Time::operator*(double m) const
    44. {
    45. Time result;
    46. long totalmin = hours*60*m + minutes*m;
    47. result.hours = totalmin/60;
    48. result.minutes = totalmin%60;
    49. return result;
    50. }
    51. Time operator*(double m,const Time &t)
    52. {
    53. /*Time result;
    54. long totalmin = t.hours*60*m + t.minutes*m;
    55. result.hours = totalmin/60;
    56. result.minutes = totalmin%60;*/
    57. return t*m;
    58. }
    59. void Time::Show() const
    60. {
    61. std::cout <<hours << " hours, " << minutes << " min\n";
    62. }

    执行结果:

    1. planning time:0 hours, 0 min
    2. coding time:2 hours, 40 min
    3. fixing time:5 hours, 55 min
    4. total time:8 hours, 35 min
    5. total time * 1.1:9 hours, 26 min

    11.3 友元

    • 友元函数
    • 友元类
    • 友元成员函数

    创建友元

    友元函数是非成员函数,但范文权限和成员函数相同

    1、将其原型放在类声明中,并在原型声明前加上关键字friend

            friend Time operator*(double m,const Time &t);

    2、编写函数定义,应为不是成员函数,不加类限定符(time::),定义中不用关键字friend。

    程序用例:

    mytime3.h

    1. #ifndef MYTIME3_H_
    2. #define MYTIME3_H_
    3. #include <iostream>
    4. class Time
    5. {
    6. private:
    7. int hours;
    8. int minutes;
    9. public:
    10. Time();
    11. Time(int h,int m = 0);
    12. void AddMin(int m);
    13. void AddHr(int h);
    14. void Reset(int h=0,int m=0);
    15. Time Sum(const Time &t) const;
    16. Time operator+(const Time &t) const;
    17. Time operator*(double m) const;
    18. friend Time operator*(double m,const Time &t)
    19. {return t*m;}
    20. //void Show() const;
    21. friend std::ostream &operator<<(std::ostream &os,const Time &t);
    22. };
    23. #endif

    mytime3.cpp

    1. #include "mytime3.h"
    2. Time::Time()
    3. {
    4. hours = minutes = 0;
    5. }
    6. Time::Time(int h,int m)
    7. {
    8. hours = h;
    9. minutes = m;
    10. }
    11. void Time::AddMin(int m)
    12. {
    13. minutes += m;
    14. hours +=minutes/60;
    15. minutes = minutes%60;
    16. }
    17. void Time::AddHr(int h)
    18. {
    19. hours +=h;
    20. }
    21. void Time::Reset(int h,int m)
    22. {
    23. hours = h;
    24. minutes = m;
    25. }
    26. Time Time::Sum(const Time &t) const
    27. {
    28. Time sum;
    29. sum.minutes = minutes + t.minutes;
    30. sum.hours = t.hours +hours + sum.minutes/60;
    31. sum.minutes %= 60;
    32. return sum;
    33. }
    34. Time Time::operator+(const Time &t) const
    35. {
    36. Time sum;
    37. sum.minutes = minutes + t.minutes;
    38. sum.hours = t.hours +hours + sum.minutes/60;
    39. sum.minutes %= 60;
    40. return sum;
    41. }
    42. Time Time::operator*(double m) const
    43. {
    44. Time result;
    45. long totalmin = hours*60*m + minutes*m;
    46. result.hours = totalmin/60;
    47. result.minutes = totalmin%60;
    48. return result;
    49. }
    50. /*Time operator*(double m,const Time &t)
    51. {
    52. Time result;
    53. long totalmin = t.hours*60*m + t.minutes*m;
    54. result.hours = totalmin/60;
    55. result.minutes = totalmin%60;
    56. return t*m;
    57. }*/
    58. /*void Time::Show() const
    59. {
    60. std::cout <<hours << " hours, " << minutes << " min\n";
    61. }*/
    62. std::ostream &operator<<(std::ostream &os,const Time &t)
    63. {
    64. os << t.hours << " hours," << t.minutes << " minutes";
    65. return os;
    66. }

    usetime3.cpp

    1. #include <iostream>
    2. #include "mytime3.h"
    3. using namespace std;
    4. int main()
    5. {
    6. Time coding(3,35);
    7. Time fixing(2,48);
    8. Time total;
    9. cout << "coding and fixing:" ;
    10. cout << coding << ";" << fixing << endl;
    11. total = coding + fixing;
    12. cout << "coding + fixing:" << total << endl;
    13. total = coding*1.17;
    14. cout << "coding*1.17:" << total << endl;
    15. cout << "10.0*fixing" << 10.0*fixing << endl;
    16. return 0;
    17. }

    运行结果:

    1. coding and fixing:3 hours,35 minutes;2 hours,48 minutes
    2. coding + fixing:6 hours,23 minutes
    3. coding*1.17:4 hours,11 minutes
    4. 10.0*fixing28 hours,0 minutes

    11.4重载运算符:作为成员函数还是非成员函数

    11.5再谈重载:一个矢量类

    示例:

    vector.h

    1. #ifndef VECTOR_H_
    2. #define VECTOR_H_
    3. #include <iostream>
    4. namespace VECTOR
    5. {
    6. class Vector
    7. {
    8. public:
    9. enum Mode{RECT,POL}; //状态成员 RECT直角坐标模式 POL极坐标模式
    10. private:
    11. double x;
    12. double y;
    13. double mag;
    14. double ang;
    15. Mode mode;
    16. void set_mag();
    17. void set_ang();
    18. void set_x();
    19. void set_y();
    20. public:
    21. Vector();
    22. Vector(double n1,double n2,Mode form = RECT);
    23. void reset(double n1,double n2,Mode form = RECT);
    24. ~Vector();
    25. double xval() const {return x;}
    26. double yval() const {return y;}
    27. double magval() const {return mag;}
    28. double angval() const {return ang;}
    29. void polar_mode();
    30. void rect_mode();
    31. Vector operator+(const Vector &b) const;
    32. Vector operator-(const Vector &b) const;
    33. Vector operator-() const;
    34. Vector operator*(double n) const;
    35. friend Vector operator*(double n,const Vector &a);
    36. friend std::ostream &operator<<(std::ostream &os,const Vector &v);
    37. };
    38. }
    39. #endif

    vector.cpp

    1. #include "vector.h"
    2. #include <cmath>
    3. using namespace std;
    4. namespace VECTOR
    5. {
    6. const double Rad_to_deg = 45.0/atan(1.0);
    7. void Vector::set_mag()
    8. {
    9. mag = sqrt(x*x+y*y);
    10. }
    11. void Vector::set_ang()
    12. {
    13. if(x == 0 && y == 0)
    14. ang = 0;
    15. else
    16. ang = atan2(y,x);
    17. }
    18. void Vector::set_x()
    19. {
    20. x = mag*cos(ang);
    21. }
    22. void Vector::set_y()
    23. {
    24. y = mag*sin(ang);
    25. }
    26. Vector::Vector()
    27. {
    28. x = y =mag = ang = 0;
    29. mode = RECT;
    30. }
    31. Vector::Vector(double n1,double n2,Mode form)
    32. {
    33. mode = form;
    34. if(form == RECT)
    35. {
    36. x= n1;
    37. y =n2;
    38. set_mag();
    39. set_ang();
    40. }
    41. else if(form == POL)
    42. {
    43. mag = n1;
    44. ang = n2;
    45. set_x();
    46. set_y();
    47. }
    48. else
    49. {
    50. cout << "incorrect argument\n";
    51. cout << "vector set to 0";
    52. x = y =mag = ang = 0;
    53. mode = RECT;
    54. }
    55. }
    56. void Vector::reset(double n1,double n2,Mode form)
    57. {
    58. mode = form;
    59. if(form == RECT)
    60. {
    61. x= n1;
    62. y =n2;
    63. set_mag();
    64. set_ang();
    65. }
    66. else if(form == POL)
    67. {
    68. mag = n1;
    69. ang = n2;
    70. set_x();
    71. set_y();
    72. }
    73. else
    74. {
    75. cout << "incorrect argument\n";
    76. cout << "vector set to 0";
    77. x = y =mag = ang = 0;
    78. mode = RECT;
    79. }
    80. }
    81. Vector::~Vector()
    82. {
    83. }
    84. void Vector::polar_mode()
    85. {
    86. mode = POL;
    87. }
    88. void Vector::rect_mode()
    89. {
    90. mode = RECT;
    91. }
    92. Vector Vector::operator+(const Vector &b) const
    93. {
    94. return Vector((x+b.x),(y+b.y));
    95. }
    96. Vector Vector::operator-(const Vector &b) const
    97. {
    98. return Vector((x-b.x),(y-b.y));
    99. }
    100. Vector Vector::operator-() const
    101. {
    102. return Vector((-x),(-y));
    103. }
    104. Vector Vector::operator*(double n) const
    105. {
    106. return Vector(n*x,n*y);
    107. }
    108. Vector operator*(double n,const Vector &a)
    109. {
    110. return a*n;
    111. }
    112. std::ostream &operator<< (std::ostream &os,const Vector &v)
    113. {
    114. //
    115. if(v.mode == Vector::RECT)
    116. os << "(x,y) = (" << v.x << "," << v.y <<")";
    117. else if(v.mode == Vector::POL)
    118. {
    119. os << "(m,a) = (" << v.mag << "," << v.ang <<")";
    120. }
    121. else
    122. os << "vector is invalid";
    123. return os;
    124. }
    125. }

    随即漫步(醉鬼走路):

    randwalk.cpp

    1. #include "vector.h"
    2. #include <cstdlib>
    3. #include <ctime>
    4. using namespace std;
    5. int main()
    6. {
    7. using VECTOR::Vector;
    8. srand(time(0));
    9. double direction;
    10. Vector step;
    11. Vector result(0.0,0.0);
    12. unsigned long steps = 0;
    13. double target;
    14. double dstep;
    15. cout <<"enter target distance (q to quite):";
    16. while(cin >> target)
    17. {
    18. cout << "enter step length:";
    19. if(!(cin >> dstep))
    20. {
    21. break;
    22. }
    23. while(result.magval() < target)
    24. {
    25. direction = rand()%360;
    26. step.reset(dstep,direction,Vector::POL);
    27. result = result + step;
    28. steps++;
    29. }
    30. cout << "after " << steps << " steps,the subject has the following location:\n";
    31. cout << result << endl;
    32. result.polar_mode();
    33. cout << "or\n" << result << endl;
    34. cout << "average outward distance per step = " << result.magval()/steps << endl;
    35. steps = 0;
    36. result.reset(0.0,0.0);
    37. cout << "enter target distance(q to quit):";
    38. }
    39. cout << "Byte!\n";
    40. cin.clear();
    41. while(cin.get()!='\n')
    42. continue;
    43. return 0;
    44. }

    运行结果:

    1. enter target distance (q to quite):10
    2. enter step length:1
    3. after 116 steps,the subject has the following location:
    4. (x,y) = (-3.42834,-9.68444)
    5. or
    6. (m,a) = (10.2734,-1.91104)
    7. average outward distance per step = 0.0885634
    8. enter target distance(q to quit):20
    9. enter step length:2
    10. after 117 steps,the subject has the following location:
    11. (x,y) = (6.30402,19.1596)
    12. or
    13. (m,a) = (20.17,1.25293)
    14. average outward distance per step = 0.172393
    15. enter target distance(q to quit):q
    16. Byte!

    11.6类的自动转换和强制类型转换

    stonewt.h

    1. #ifndef STONEWT_H_
    2. #define STONEWT_H_
    3. class Stonewt
    4. {
    5. private:
    6. enum {Labs_per_stn = 14};
    7. int stone;
    8. double pds_left;
    9. double pounds;
    10. public:
    11. Stonewt(double lbs);
    12. Stonewt(int stn,double lbs);
    13. Stonewt();
    14. ~Stonewt();
    15. void show_lbs() const;
    16. void show_stn() const;
    17. };
    18. #endif

    stonewt.cpp

    1. #include <iostream>
    2. #include "stonewt.h"
    3. using namespace std;
    4. Stonewt::Stonewt(double lbs)
    5. {
    6. stone = int (lbs)/Labs_per_stn;
    7. pds_left = int (lbs)%Labs_per_stn +lbs - int(lbs);
    8. pounds = lbs;
    9. }
    10. Stonewt::Stonewt(int stn,double lbs)
    11. {
    12. stone = stn;
    13. pds_left = lbs;
    14. pounds = stn*Labs_per_stn + lbs;
    15. }
    16. Stonewt::Stonewt()
    17. {
    18. stone = pounds = pds_left = 0;
    19. }
    20. Stonewt::~Stonewt()
    21. {
    22. }
    23. void Stonewt::show_stn() const
    24. {
    25. cout << stone << "stone," << pds_left << "pounds\n";
    26. }
    27. void Stonewt::show_lbs() const
    28. {
    29. cout << pounds << "pounds\n";
    30. }

    explicit:只能用于显示转换,不能用于隐式转换。

    stone.cpp

    1. #include <iostream>
    2. #include "stonewt.h"
    3. using namespace std;
    4. void display(const Stonewt &st,int n);
    5. int main()
    6. {
    7. Stonewt incognito = 275;
    8. Stonewt wolfe(285.7);
    9. Stonewt taft(21,8);
    10. cout << "the celebrity weighed ";
    11. incognito.show_stn();
    12. cout << "the detective weighed ";
    13. wolfe.show_stn();
    14. cout << "the president weighed ";
    15. taft.show_lbs();
    16. incognito = 276.8;
    17. taft = 325;
    18. cout << "after dinner,the celebrity weighed ";
    19. incognito.show_stn();
    20. cout << "after dinner,the president weighed ";
    21. taft.show_lbs();
    22. display(taft,2);
    23. cout << "the wrestler weiged even more.\n";
    24. display(422,2);
    25. cout << "No stone left unearned\n";
    26. return 0;
    27. }
    28. void display(const Stonewt &st,int n)
    29. {
    30. for(int i = 0;i<n;i++)
    31. {
    32. cout << "Wow| ";
    33. st.show_stn();
    34. }
    35. }

    运行结果:

    1. the celebrity weighed 19stone,9pounds
    2. the detective weighed 20stone,5.7pounds
    3. the president weighed 302pounds
    4. after dinner,the celebrity weighed 19stone,10.8pounds
    5. after dinner,the president weighed 325pounds
    6. Wow| 23stone,3pounds
    7. Wow| 23stone,3pounds
    8. the wrestler weiged even more.
    9. Wow| 30stone,2pounds
    10. Wow| 30stone,2pounds
    11. No stone left unearned

    转换函数:

  • 相关阅读:
    微信小程序通过获取键盘高度解决键盘弹出挡住输入框问题
    【无标题】
    微服务和Spring Cloud Alibaba介绍
    居家必备练习题(C语言)
    uniapp基础篇 - - 实现APP语言国际化
    第十二周总结
    yolov6 win10环境配置详细过程
    酷开科技丨这么好用的酷开系统,不用真的会后悔!
    开源软件:释放创新的力量,改变数字世界的游戏规则
    redis探索之缓存击穿、缓存雪崩、缓存穿透
  • 原文地址:https://blog.csdn.net/m0_59666413/article/details/125454375