C++中使用类(重载,友元函数,转换函数等)


说下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.   
  29. #endif  
mytime0.cpp
  1. #include <iostream>   
  2. #include "mytime0.h"   
  3.   
  4. Time::Time()  
  5. {  
  6.     hours = minutes = 0;  
  7. }  
  8.   
  9. Time::Time(int h, int m)  
  10. {  
  11.     hours = h;  
  12.     minutes = m;  
  13. }  
  14.   
  15. void Time::AddMin(int m)  
  16. {  
  17.     minutes += m;  
  18.     hours += minutes / 60;  
  19.     minutes %= 60;  
  20. }  
  21.   
  22. void Time::AddHr(int h)  
  23. {  
  24.     hours += h;  
  25. }  
  26.   
  27. void Time::Reset(int h, int m)  
  28. {  
  29.     hours = h;  
  30.     minutes = m;  
  31. }  
  32.   
  33. Time Time::Sum(const Time & t)const  
  34. {  
  35.     Time sum;  
  36.     sum.minutes = minutes + t.minutes;  
  37.     sum.hours = hours + t.hours + sum.minutes/60;  
  38.     sum.minutes %= 60;  
  39.     return sum;  
  40. }  
  41.   
  42. // 重载加号运算符的版本   
  43. Time Time::operator+(const Time & t)const  
  44. {  
  45.     Time sum;  
  46.     sum.minutes = minutes + t.minutes;  
  47.     sum.hours = hours + t.hours + sum.minutes/60;  
  48.     sum.minutes %= 60;  
  49.     return sum;  
  50. }  
  51.   
  52. Time Time::operator-(const Time & t)const  
  53. {  
  54.     Time diff;  
  55.     int tot1, tot2;  
  56.     tot1 = t.minutes + 60 * t.hours;  
  57.     tot2 = minutes + 60 * hours;  
  58.     diff.minutes = (tot2 - tot1) % 60;  
  59.     diff.hours = (tot2 - tot1) / 60;  
  60.     return diff;  
  61. }  
  62.   
  63. Time Time::operator*(double n)const  
  64. {  
  65.     Time result;  
  66.     long totalminutes = hours * n * 60 + minutes * n;  
  67.     result.hours = totalminutes / 60;  
  68.     result.minutes = totalminutes % 60;  
  69.     return result;  
  70. }  
  71.   
  72. std::ostream & operator<<(std::ostream & os, const Time & t)  
  73. {  
  74.     os << t.hours << "hours, " << t.minutes << " minutes";  
  75.     return os;  
  76. }  
  77.   
  78. void Time::Show()const  
  79. {  
  80.     std::cout << hours << " hours, " << minutes << " minutes";  
  81.       
  82. }  
  83.   
  84. //usetime0.cpp   
  85. #include <iostream>   
  86. #include "mytime0.h"   
  87.   
  88. int main()  
  89. {  
  90.     using std::cout;  
  91.     using std::endl;  
  92.     Time planning;  
  93.     Time coding(2, 40);  
  94.     Time fixing(5, 55);  
  95.     Time total;  
  96.   
  97.     cout << "planning time = ";  
  98.     planning.Show();  
  99.     cout << endl;  
  100.       
  101.     cout << "coding time = ";  
  102.     coding.Show();  
  103.     cout << endl;  
  104.   
  105.     cout << "fixing time = ";  
  106.     fixing.Show();  
  107.     cout << endl;  
  108.       
  109. //  total = coding.Sum(fixing);   
  110. //  重载加号运算符的版本   
  111.     total = coding + fixing;  
  112.     cout << "coding + fixing = ";  
  113.     total.Show();  
  114.     cout << endl;  
  115.   
  116.     Time morefixing(3 ,20);  
  117.     cout << "more fixing time = ";  
  118.     morefixing.Show();  
  119.     cout << endl;  
  120.     total = morefixing.operator+(total);  
  121.     cout << "morefixing.operator+(total) = ";  
  122.     total.Show();  
  123.     cout << endl;  
  124.   
  125.     Time aida(3, 35);  
  126.     Time tosca(2, 48);  
  127.     Time temp;  
  128.   
  129.     cout << "Aida and TOsca:\n";  
  130.     cout << aida <<"; " << tosca << endl;  
  131.     temp = aida + tosca;        // operator+()   
  132.     cout << "Aida + Tosca: " << temp << endl;  
  133.     temp = aida*1.17;  
  134.     cout << "Aida *1.17: " << temp << endl;  
  135.     cout << "10 * Tosca: " << 10 * tosca << endl;  
  136.   
  137.     return 0;  
  138. }  
然后接下来说下类的强制转换和自动转换。
  • 1
  • 2
  • 下一页

相关内容