重载()运算符

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Distance
 5 {
 6 private:
 7   int feet;   // 0 到无穷
 8   int inches; // 0 到 12
 9 public:
10   Distance()
11   {
12     feet = 0;
13     inches = 0;
14   }
15   Distance(int f, int i)
16   {
17     feet = f;
18     inches = i;
19   }
20 
21   // 重载函数调用运算符
22   Distance operator()(int a, int b, int c)
23   {
24     Distance D;
25     // 进行随机计算
26     D.feet = a + c + 10;
27     D.inches = b + c + 100;
28     return D;
29   }
30     
31   // 显示距离的方法
32   void displayDistance()
33   {
34     cout << "F: " << feet << " I:" << inches << endl;
35   }
36 };
37 
38 int main()
39 {
40   Distance D1(11, 10), D2;
41 
42   cout << "First Distance : ";
43   D1.displayDistance();
44 
45   D2 = D1(10, 10, 10); // invoke operator()
46   cout << "Second Distance :";
47   D2.displayDistance();
48 
49   return 0;
50 }
View Code

相关文章:

  • 2022-12-23
  • 2022-01-19
  • 2021-09-28
  • 2021-11-17
  • 2021-10-07
  • 2022-01-01
  • 2020-03-21
  • 2019-08-25
猜你喜欢
  • 2022-12-23
  • 2021-09-11
  • 2021-09-27
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案