我是靠谱客的博主 聪慧鼠标,这篇文章主要介绍C++类和对象实战之Date类的实现方法,现在分享给大家,希望可以做个参考。

零、前言

在学了C++类和对象基本知识以及六个默认成员函数后,我们可以上手实现一个Date类出来,检验学习的效果。

一、Date类相关接口

接口展示:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Date { //输出操作符重载 friend ostream& operator<<(ostream& _cout, const Date& d); //输出操作符重载 friend istream& operator>>(istream& _cin, Date& d); public: // 获取某年某月的天数 int GetMonthDay(int year, int month); // 全缺省的构造函数 Date(int year=1988, int month=1, int day=1); // 拷贝构造函数 Date(const Date& d); // 赋值运算符重载 Date& operator=(const Date& d); // 日期+=天数 Date& operator+=(int day); // 日期+天数 Date operator+(int day); // 日期-天数 Date operator-(int day); // 日期-=天数 Date& operator-=(int day); // 前置++ Date& operator++(); // 后置++ Date& operator++(int); // 后置-- Date& operator--(int); // 前置-- Date& operator--(); // >运算符重载 bool operator>(const Date& d); // ==运算符重载 bool operator==(const Date& d); // >=运算符重载 bool operator>=(const Date& d); // <运算符重载 bool operator<(const Date& d); // <=运算符重载 bool operator<=(const Date& d); // !=运算符重载 bool operator!=(const Date& d); // 日期-日期 返回两个日期之间相隔的具体天数 int operator-(const Date& d); //日期展示 void print() { cout << _year << " " << _month << " " << _day << endl; } private: int _year; int _month; int _day; };

二、具体接口函数实现

注意:

因为对于定义在类里面的函数会自动设成内联函数,而只有一些简单的函数才建议设成内联函数,所以实现函数时我们是声明和定义分离(在类里面声明,类外定义)

在类外实现函数接口需要加上类域名称

1、获取月份天数

注意:

闰年二月与平年二月的天数不同

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
//获取月份天数 int Date::GetMonthDay(int year, int month) { //设置平年月天数数组 static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//设置成静态避免重复创建 int day = monthdays[month]; //对闰年二月的处理 if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { day = 29; } return day; }

2、Date打印

注:打印函数比较简单,设成内联函数很适合,可以直接在类里定义

实现代码:

复制代码
1
2
3
4
void Date::Print() { cout << _year << "年" << _month << "月" << _day << "日" << endl; }

3、Date构造函数

注意:

对于构造函数建议写成全缺省函数(便于无参数初始化),但是只能定义和声明其中一个写缺省

考虑初始化的日期是否合理

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//构造函数 //类里声明 Date(int year = 0, int month = 1, int day = 1); //定义 Date::Date(int year, int month, int day) { // 检查日期的合法性 if (year >= 0 && month > 0 && month < 13 && day > 0 && day <= GetMonthDay(year, month)) { _year = year; _month = month; _day = day; } else { // 严格来说抛异常更好 cout << "非法日期" << endl; cout << year << "年" << month << "月" << day << "日" << endl; exit(-1); } }

4、Date析构函数

注:对于像Date一样的类来说,析构函数(没有需要清理的空间资源),拷贝函数和赋值重载函数(能够完成成员变量浅拷贝)都不用自己写,编译器默认生成的已经足够使用

实现代码:

复制代码
1
2
3
4
5
6
7
//析构函数 Date::~Date() { _year = 1; _month = 0; _day = 0; }

5、Date拷贝构造函数

实现代码:

复制代码
1
2
3
4
5
6
7
//拷贝构造 Date::Date(const Date& d) { _year = d._year; _month = d._month; _day= d._day; }

6、Date赋值重载函数

注意:

对于赋值操作符来说,是需要能支持连续赋值的操作,这里我们返回Date本身来进行接下来的继续赋值

实现代码:

复制代码
1
2
3
4
5
6
7
8
//赋值运算符重载 Date& Date::operator=(const Date& d) { _year = d._year; _month = d._month; _day = d._day; return *this; }

效果图:

7、Date+=天数

注意:

  1. +=表示会修改Date本身的数据
  2. 处理传入负数天数
  3. 处理好天数进位,月份进位

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//日期+=天数 Date& Date::operator+=(int day) { if (day < 0)//处理特殊情况 { *this -= -day;//复用Date-=天数 } else { _day += day; while (_day > GetMonthDay(_year, _month))//处理数据合理性 { _day -= GetMonthDay(_year, _month); _month++; if (_month > 12) { _year++; _month = 1; } } } return *this;//返回引用,即对象本身 }

8、Date+天数

注意:

+天数表示不会修改Date本身的数据(使用const修饰,避免修改)

逻辑与Date+=天数基本一致,可以进行复用

实现代码:

复制代码
1
2
3
4
5
6
Date Date::operator+(int day) const { Date tmp = *this;//赋值重载 tmp += day;//复用+=重载 return tmp;//返回值(拷贝构造) }

9、Date-=天数

注意:

  1. +=表示会修改Date本身的数据
  2. 处理传入负数天数
  3. 考虑日期的借位,月份的借位

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//日期-=天数 Date& Date::operator-=(int day) { if (day < 0) { *this += -day;//复用Date+=天数 } else { _day -= day; while (_day <= 0)//处理数据合理性 { _month--; if (_month <= 0) { _year--; _month = 12; } _day += GetMonthDay(_year, _month); } } return *this; }

10、Date-天数

注意:

  1. -天数不会修改Date本身的数据(使用const修饰,避免修改)
  2. 逻辑与Date-=天数基本一致,可以进行复用

实现代码:

复制代码
1
2
3
4
5
6
Date Date::operator-(int day) const { Date tmp = *this; tmp -= day; return tmp; }

11、++Date

注意:

前置++表示,Date先增后使用

实现代码:

复制代码
1
2
3
4
5
6
//++Date Date& Date::operator++() { *this += 1;//复用Date+=天数 return *this; }

12、Date++

注意:

语法规定,因为与前置命名相同的缘故,这里的后置函数多一个参数来与前置函数形成重载

后置++表示先使用后自增

实现代码:

复制代码
1
2
3
4
5
6
7
//Date++ Date Date::operator++(int) { Date tmp = *this;//保存一份日期 *this += 1;//自增当前日期 return tmp;//返回自增前的日期 }

13、–Date

实现代码:

复制代码
1
2
3
4
5
6
//--Date Date& Date::operator--() { *this -= 1; return *this; }

14、Date–

实现代码:

复制代码
1
2
3
4
5
6
7
//Date-- Date Date::operator--(int) { Date tmp = *this; *this -= 1; return tmp; }

15、日期比较

注:可以多次复用

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//日期比较 bool Date::operator>(const Date& d) const { if (_year > d._year) { return true; } else if(_year == d._year) { if (_month > d._month) { return true; } else if(_month == d._month) { if (_day > d._day) { return true; } } }> return false; } bool Date::operator==(const Date& d) const { return _year == d._year && _month == d._month && _day == d._day; } bool Date::operator<(const Date& d) const { return !(*this >= d); } bool Date::operator>=(const Date& d) const { return *this > d || *this == d; } bool Date::operator<=(const Date& d) const { return !(*this > d); } bool Date::operator!=(const Date& d) const { return !(*this == d); }

16、Date相减

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//日期减日期 int Date::operator-(const Date& d) const { //确定日期的大小 Date max = *this; Date min = d; if (*this < d)//复用日期比较 { max = d; min = *this; } int day = 0; while (max != min) { ++min; ++day; } return day; }

17、日期输入日期输出

注意:

  1. 对于输入操作符,我们习惯是cin>>date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)
  2. 虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
//输出操作符重载 ostream& operator<<(ostream& _cout, const Date& d) { _cout << d._year << "年" << d._month << "月" << d._day << "日" ; return _cout; } //输出操作符重载 istream& operator>>(istream& _cin, Date& d) > { _cin >> d._year >> d._month >> d._day; return _cin; }

效果图:

  1. date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)
  2. 虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
//输出操作符重载 ostream& operator<<(ostream& _cout, const Date& d) { _cout << d._year << "年" << d._month << "月" << d._day << "日" ; return _cout; } //输出操作符重载 istream& operator>>(istream& _cin, Date& d) { _cin >> d._year >> d._month >> d._day; return _cin; }

效果图:

总结

到此这篇关于C++类和对象实战之Date类实现的文章就介绍到这了,更多相关C++ Date类的实现内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是聪慧鼠标最近收集整理的关于C++类和对象实战之Date类的实现方法的全部内容,更多相关C++类和对象实战之Date类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部