【C++】-- 实现Date类的各种运算符重载

简介: 【C++】-- 实现Date类的各种运算符重载

上一篇文章只实现了operator==操作符重载,由于运算符较多,该篇文章单独实现剩余所有的运算符重载。继续以Date类为例,实现运算符重载:

Date.h

1. #pragma once
2. #include<iostream>
3. #include<assert.h>
4. 
5. using namespace std;
6. 
7. class Date
8. {
9. public:
10.   //构造函数
11.   Date(int year = 0, int month = 1, int day = 0);
12. 
13.   //打印
14.   void Print() const;
15. 
16.   //Date类的成员变量全部都是内置类型,析构函数、拷贝构造函数、赋值运算符重载函数都可以不写,默认生成就够用
17.   //Stack类才需要写这三个函数
18. 
19.   //日期+整数
20.   Date& operator+=(int day);//日期被改变
21.   Date operator+(int day) const;//日期不会被改变
22. 
23.   //日期-整数
24.   Date& operator-=(int day);//日期被改变
25.   Date operator-(int day) const;//日期不会被改变
26. 
27. 
28.   //前置++和后置++都完成了++,不同的地方在于返回值不一样
29.   //++d 前置++  返回++之后的值 
30.   Date& operator++();
31. 
32.   //d++ 后置++  返回++之前的值
33.   Date operator++(int);//这里的int不需要给实参,没用该实参,它的作用是为了跟前置++构成函数重载
34. 
35.   //前置--和后置--都完成了--,不同的地方在于返回值不一样
36.   //--d 前置--  返回--之后的值
37.   Date& operator--();
38. 
39.   //d-- 后置--  返回--之前的值
40.   Date operator--(int);//这里的int不需要给实参,没用该实参,它的作用是为了跟前置--构成函数重载
41. 
42.   //比较运算符
43.   bool operator>(const Date& d) const;
44.   bool operator<(const Date& d) const;
45.   bool operator>=(const Date& d) const;
46.   bool operator<=(const Date& d) const;
47.   bool operator==(const Date& d) const;
48.   bool operator!=(const Date& d) const;
49. 
50.   //日期-日期,计算两个日期之间相差多少天
51.   int operator-(const Date& d) const;
52. 
53. private:
54.   int _year;
55.   int _month;
56.   int _day;
57. };

Date.cpp

1. #include "Date.h"
2. 
3. //每次调用构造函数都会调用getMonthDay,定义成inline就没有函数压栈开销了
4. inline int getMonthDay(int year, int month)
5. {
6.  //数组存储平年每个月的天数,数组定义成static是为了避免每次调用getMonthDay时都要创建dayArray数组
7.  static int dayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
8.  int day = dayArray[month];
9. 
10.   if ((month == 2) && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
11.   {
12.     day = 29;
13.   }
14.   return day;
15. }
16. 
17. Date::Date(int year, int month, int day)
18. {
19.   //检查日期的合法性
20.   if (year > 0 && month > 0 && month < 13 && day > 0 && day <= getMonthDay(year,month))
21.   {
22.     _year = year;
23.     _month = month;
24.     _day = day;
25.   }
26.   else
27.   {
28.     cout << "非法日期" << endl;
29.     assert(false);
30.   } 
31. }
32. 
33. void Date::Print() const
34. {
35.   cout << _year << "-" << _month << "-" << _day << endl;
36. }
37. 
38. //d += day 日期+整数,日期被改变
39. Date& Date::operator+=(int day)//因为对象是在Test()中,即Date& Date::operator+=(int day)之外定义的
40.                  //出了该函数作用域,该对象还存在,可以用引用返回
41. {
42.   _day += day;
43. 
44.   //天不合法,不断进位,让天合法
45.   while (_day > getMonthDay(_year, _month))
46.   {
47.     _day -= getMonthDay(_year, _month);
48.     _month++;
49.     if (_month > 12)
50.     {
51.       _year++;
52.       _month = 1;
53.     }
54.   }
55. 
56.   return *this;//天数加到了*this上,返回*this
57. }
58. 
59. //d + day 日期+整数,日期不会被改变
60. Date Date::operator+(int day) const
61. {
62.   Date ret(*this);
63.   ret += day; //ret.operator+=(day)
64.         //复用+=,即调用Date& Date::operator+=(int day)函数,ret被改变,*this没有被改变
65.   return ret;
66. }
67. 
68. //d -= day 日期-整数,日期被改变
69. Date& Date::operator-=(int day)
70. {
71.   //判断day是否为负数
72.   if (day < 0)
73.   {
74.     *this += -day;
75.   }
76.   else
77.   {
78.     _day -= day;
79. 
80.     while (_day <= 0)
81.     {
82.       _day += getMonthDay(_year, _month - 1);
83.       _month--;
84. 
85.       if (_month < 1)
86.       {
87.         _year--;
88.         _month = 12;
89.       }
90.     }
91.   }
92. 
93.   return *this;//天数减到了*this上,返回*this
94. }
95. 
96. //d - day 日期整数,日期不会被改变
97. Date Date::operator-(int day) const
98. {
99.   Date ret(*this);
100.  ret -= day; //ret.operator+=(day)
101.        //复用+=,即调用Date& Date::operator+=(int day)函数,ret被改变,*this没有被改变
102.  return ret;
103. }
104. 
105. //++d ->d.operator++(&d)  前置++  返回++之后的值
106. Date& Date::operator++() 
107. {
108.  *this += 1;
109.  return *this;
110. }
111. 
112. //d++ ->d.operator+=(&d, 0) 后置++  返回++之前的值
113. Date Date::operator++(int)
114. {
115.  Date temp(*this);//将++之前的值保存起来
116.  *this += 1;
117.  return temp;
118. }
119. 
120. //--d ->d.operator--(&d)  前置--  返回--之后的值
121. Date& Date::operator--()
122. {
123.  *this -= 1;
124.  return *this;
125. }
126. 
127. //d-- ->d.operator-=(&d, 0) 后置--  返回--之前的值
128. Date Date::operator--(int)
129. {
130.  Date temp(*this);//将--之前的值保存起来
131.  *this -= 1;
132.  return temp;
133. }
134. 
135. 
136. //比较运算符
137. //d1 > d2
138. bool Date::operator>(const Date& d) const
139. {
140.  if (_year > d._year)
141.  {
142.    return true;
143.  }
144.  else if (_year == d._year)
145.  {
146.    if (_month > d._month)
147.    {
148.      return true;
149.    }
150.    else if (_month == d._month)
151.    {        
152.      if (_day > d._day)
153.      {
154.        return true;
155.      }       
156.    }   
157.  }
158. 
159.  return false;
160. }
161. 
162. //d1 == d2
163. bool Date::operator==(const Date& d) const
164. {
165.  return (_year == d._year)
166.    && (_month == d._month)
167.    && (_day == d._day);
168. }
169. 
170. //d1 >= d2
171. bool Date::operator>=(const Date& d) const
172. {
173.  return *this > d || *this == d;
174. }
175. 
176. //d1 < d2
177. bool Date::operator<(const Date& d) const
178. {
179.  return !(*this >= d);
180. }
181. 
182. //d1 <= d2
183. bool Date::operator<=(const Date& d) const
184. {
185.  return !(*this > d);
186. }
187. 
188. //d1 != d2
189. bool Date::operator!=(const Date& d) const
190. {
191.  return !(*this == d);
192. }
193. 
194. //日期-日期,计算两个日期之间相差多少天
195. int Date::operator-(const Date& d) const
196. {
197.  //找出this和d哪个大
198.  Date max = *this;//假设this大
199.  Date min = d;
200.  int flag = 1;//可能两个日期相减值为负数,将flag作为标志位进行记录
201. 
202.  if (*this < d)
203.  {
204.    max = d;
205.    min = *this;
206.    flag = -1;
207.  }
208. 
209.  int n = 0;
210.  while (min != max)//使用operator!=运算符重载
211.  {
212.    min++;
213.    n++;
214.  }
215.  return flag * n;
216. }

Test.cpp

1. #include "Date.h"
2. void Test1()
3. {
4.  Date d1(2022, 4, 18);
5.  d1.Print();
6. 
7.  d1 += 100;
8.  d1.Print();//d1被改变
9. 
10.   //用d2来接收operator+的返回值
11.   Date d2 = d1 + 3;
12.   d2.Print();
13.   d1.Print();//d1未被改变
14. }
15. 
16. void Test2()
17. {
18.   Date d1(2022, 4, 18);
19.   d1.Print();
20. 
21.   d1 -= 100;
22.   d1.Print();//d1被改变
23. 
24.   //用d3来接收operator-的返回值
25.   Date d3 = d1 + 3;
26.   d3.Print();
27.   d1.Print();//d1未被改变
28. }
29. 
30. void Test3()
31. {
32.   Date d1(2022, 4, 18);
33.   d1.Print();
34. 
35.   Date ret = ++d1;//前置++,返回++之后的值,用ret来接收返回值,和Test4()中的返回值不同
36.   ret.Print();//
37.   d1.Print();
38. }
39. 
40. void Test4()
41. {
42.   Date d1(2022, 4, 18);
43.   d1.Print();
44. 
45.   Date ret = d1++;//后置++,返回++之前的值,用ret来接收返回值,和Test3()中的返回值不同
46.   ret.Print();
47.   d1.Print();
48. }
49. 
50. void Test5()
51. {
52.   Date d1(2022, 4, 18);
53.   Date d2(2022, 12, 18);
54. 
55.   int ret = d1 - d2;
56.   cout << ret << endl;
57. }
58. 
59. int main()
60. {
61.   Test5();
62. }


相关文章
|
5天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
5天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
4天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
4天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
8天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
10天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
10天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
10天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
11天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元