歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++中使用類(重載,友元函數,轉換函數等)

C++中使用類(重載,友元函數,轉換函數等)

日期:2017/3/1 10:12:54   编辑:Linux編程
說下C++裡的操作符重載和以後的內容。C++中有個operator操作符概念。如果想重載+運算符,那麼需要寫成operator+()。一般兩個數相加是這麼調用的:
  1. a = b+c; == a = b.operator+(c);
當調用操作符,會有一個隱式的調用。把自己的對象作為操作符的對象。然後顯示調用參數c。

重點介紹友元函數。因為重載了運算符後可能出現類似:

  1. a = 1.5 + c;
此時按照上面的說法。1.5不是一個對象。無法完成操作,這時候就需要友元函數了,其實友元函數就是類的非成員函數,不會隱式的將自身對象也作為參數。

然後貼三段代碼,注釋寫的還算詳細。大家可以復制後去運行下看下結果。代碼是從C++Primer Plus裡抄過來的。

mytime0.h

  1. #ifndef MYTIME0_H_
  2. #define MYTIME0_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. // 接下來時重載+運算符的函數
  17. Time operator+(const Time & t)const;
  18. void Show()const;
  19. // 重載-運算符的函數
  20. Time operator-(const Time & t)const;
  21. // 重載*運算符的函數
  22. Time operator*(double n)const;
  23. // 友元函數 類似 a+b 的原型是 a.operator+(b); 所以如果 5*a 則5沒有那個重載的函數,這時就不能使用類的成員函數了,需要友元函數來進行操作,類友元函數也就是非成員函數
  24. friend Time operator*(double m, const Time & t){return t * m;} //內聯函數,調用該類的成員函數,這裡也就是上面那個函數。
  25. // 友元函數。對於那些非成員重載操作符函數來說,操作符左面的操作數對應函數的第一個參數。類似 c = a+b 類似於 c = operator+(a,b)
  26. friend std::ostream & operator<<(std::ostream & os, const Time & t);
  27. };
  28. #endif
mytime0.cpp
  1. #include <iostream>
  2. #include "mytime0.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 %= 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 = hours + t.hours + sum.minutes/60;
  32. sum.minutes %= 60;
  33. return sum;
  34. }
  35. // 重載加號運算符的版本
  36. Time Time::operator+(const Time & t)const
  37. {
  38. Time sum;
  39. sum.minutes = minutes + t.minutes;
  40. sum.hours = hours + t.hours + sum.minutes/60;
  41. sum.minutes %= 60;
  42. return sum;
  43. }
  44. Time Time::operator-(const Time & t)const
  45. {
  46. Time diff;
  47. int tot1, tot2;
  48. tot1 = t.minutes + 60 * t.hours;
  49. tot2 = minutes + 60 * hours;
  50. diff.minutes = (tot2 - tot1) % 60;
  51. diff.hours = (tot2 - tot1) / 60;
  52. return diff;
  53. }
  54. Time Time::operator*(double n)const
  55. {
  56. Time result;
  57. long totalminutes = hours * n * 60 + minutes * n;
  58. result.hours = totalminutes / 60;
  59. result.minutes = totalminutes % 60;
  60. return result;
  61. }
  62. std::ostream & operator<<(std::ostream & os, const Time & t)
  63. {
  64. os << t.hours << "hours, " << t.minutes << " minutes";
  65. return os;
  66. }
  67. void Time::Show()const
  68. {
  69. std::cout << hours << " hours, " << minutes << " minutes";
  70. }
  71. //usetime0.cpp
  72. #include <iostream>
  73. #include "mytime0.h"
  74. int main()
  75. {
  76. using std::cout;
  77. using std::endl;
  78. Time planning;
  79. Time coding(2, 40);
  80. Time fixing(5, 55);
  81. Time total;
  82. cout << "planning time = ";
  83. planning.Show();
  84. cout << endl;
  85. cout << "coding time = ";
  86. coding.Show();
  87. cout << endl;
  88. cout << "fixing time = ";
  89. fixing.Show();
  90. cout << endl;
  91. // total = coding.Sum(fixing);
  92. // 重載加號運算符的版本
  93. total = coding + fixing;
  94. cout << "coding + fixing = ";
  95. total.Show();
  96. cout << endl;
  97. Time morefixing(3 ,20);
  98. cout << "more fixing time = ";
  99. morefixing.Show();
  100. cout << endl;
  101. total = morefixing.operator+(total);
  102. cout << "morefixing.operator+(total) = ";
  103. total.Show();
  104. cout << endl;
  105. Time aida(3, 35);
  106. Time tosca(2, 48);
  107. Time temp;
  108. cout << "Aida and TOsca:\n";
  109. cout << aida <<"; " << tosca << endl;
  110. temp = aida + tosca; // operator+()
  111. cout << "Aida + Tosca: " << temp << endl;
  112. temp = aida*1.17;
  113. cout << "Aida *1.17: " << temp << endl;
  114. cout << "10 * Tosca: " << 10 * tosca << endl;
  115. return 0;
  116. }
然後接下來說下類的強制轉換和自動轉換。
Copyright © Linux教程網 All Rights Reserved