赞
踩
目录
问:这个函数为什么不和其他的函数一样放在Date.cpp文件中实现呢?
1.2 CheckDate函数(检查日期有效性)、Print函数(打印日期)
为什么参数顺序为(ostream& out, const Date& d)?
3. const成员函数内可以调用其它的非const成员函数吗?
4. 非const成员函数内可以调用其它的const成员函数吗?
- #define _CRT_SECURE_NO_WARNINGS 1
- #pragma once
- #include<iostream>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<math.h>
- #include<time.h>
- #include<iostream>
- #include<assert.h>
-
- using namespace std;
-
- class Date {
-
- // 友元函数声明
- friend ostream& operator<<(ostream& out, const Date& d);
- friend istream& operator>>(istream& in, Date& d);
-
- public:
-
- // 全缺省的构造函数
- Date(int year = 1900, int month = 1, int day = 1);
- void Print() const;
-
- // 直接定义在类中,他默认是inline
- // 频繁调用
- // 获取某年某月的天数
- int GetMonthDay(int year, int month)
- {
- assert(month > 0 && month < 13);
-
- static int monthDayArray[13] = { -1,31,28,31,30,31,30,31,30,31,31,30,31 };
- if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
- return 29;
- else
- {
- return monthDayArray[month];
- }
- return monthDayArray[month];
- }
-
- bool CheckDate();
-
- // 不使用引用修改成员的都能加上 const
- bool operator<(const Date& d) const;
- bool operator<=(const Date& d) const;
- bool operator>(const Date& d) const;
- bool operator>=(const Date& d) const;
- bool operator==(const Date& d) const;
- bool operator!=(const Date& d) const;
-
- // 赋值运算符重载
- // d2 = d3 -> d2.operator=(&d2, d3)
- //Date& operator=(const Date& d);
-
- // d1 += 100
- // 日期+=天数
- Date& operator+=(int day);
-
- // d1 + 100
- // 日期+天数
- Date operator+(int day) const;
-
-
- // d1 -= 100
- // 日期-=天数
- Date& operator-=(int day);
-
- // d1 - 100;
- // 日期-天数
- Date operator-(int day) const;
-
- // d1 - d2
- // 日期-日期 返回天数
- int operator-(const Date& d) const;
-
- // ++d1
- Date& operator++();
-
- // d1++ -> d1.operator(1)
- // 为了区分,构成了重载,给后置++,强行增加了一个int形参
- // 这里不需要写形参名,因为接收值是多少不中呀,也不需要调用
- // 这个参数仅仅是为了前缀和后缀区分而存在的
- Date operator++(int);
-
- // --d1
- Date& operator--();
-
- // d1--
- Date operator--(int);
-
- 流插入
- //void operator<<(ostream& out);
- // 不建议, 因为Date* this占据了一个参数位置, 使用d<<cout不符合习惯
-
-
- // private:
- int _year;
- int _month;
- int _day;
- };
-
- // 重载
- ostream& operator<<(ostream & out, const Date & d);
- istream& operator>>(istream& in, Date& d);
- // 获取某年某月的天数
- int GetMonthDay(int year, int month)
- {
- // 断言:确保传入的月份是有效的
- assert(month > 0 && month < 13);
-
- // 存储了每个月通常的天数
- static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-
- // 闰年检查:如果月份是2月,并且年份是闰年,则返回29天
- if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
- return 29;
- }
- // 如果不是2月或年份不是闰年,返回静态数组中存储的对应月份的天数
- else {
- return monthDayArray[month];
- }
- }
- bool Date::CheckDate()
- {
- if (_month < 1 || _month > 12 // 检查_month成员变量是否小于1或大于12
- || _day < 1 || _day > GetMonthDay(_year, _month))
- // 检查_day成员变量是否小于1
- // 调用GetMonthDay(_year, _month)获取当前年份和月份对应的天数,并检查_day是否大于这个值
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
- // Date类的构造函数,用于初始化Date对象
- Date::Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
-
- // 调用CheckDate函数检查日期是否合法
- if (!CheckDate())
- {
- cout << "日期非法" << endl;
- }
- }
-
- // 打印日期
- void Date::Print() const
- {
- cout << _year << "-" << _month << "-" << _day << "\n";
- }
只需实现两个运算符,就能借助这两个运算符去简易的实现其他运算符。
- // d1 < d2
- 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)
- {
- return _day < d._day;
- }
- }
- return false;
- }
- // d1 == d2
- bool Date::operator==(const Date& d) const
- {
- return _year == d._year
- && _month == d._month
- && _day == d._day;
- }
- // d1 <= d2
- 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);
- }
-
- bool Date::operator!=(const Date& d) const
- {
- return !(*this == d);
- }
- // d1 += day
- Date& Date::operator+=(int day)
- {
- _day += day;
- while (_day > GetMonthDay(_year, _month))
- // 进位
- {
- _day -= GetMonthDay(_year, _month);
- ++_month;
- if (_month == 13)
- {
- ++_year;
- _month = 1;
- }
- }
- return *this;
- }
-
- // 1970以后
- // d1 -= 100
- Date& Date::operator-=(int day)
- {
- if (day < 0)
- {
- return *this += -day;
- }
-
- _day -= day;
- while (_day <= 0)// 借位
- {
- --_month;
- if (_month == 0)
- {
- _month = 12;
- _year--;
- }
-
- //借上一个月的天数
- _day += GetMonthDay(_year, _month);
- }
-
- return *this;
- }
- Date Date::operator-(int day) const
- {
- Date tmp = *this;
- tmp -= day;
- return tmp;
- }
-
- Date Date::operator+(int day) const
- {
- Date tmp = *this;
- tmp += day;
- return tmp;
- }
- // d1 - d2
- int Date::operator-(const Date& d) const
- {
- Date max = *this;
- Date min = d;
- int flag = 1;
-
- if (*this < d) // 确定最大和最小日期
- {
- max = d;
- min = *this;
- flag = -1;
- // 即当前对象小于传入的日期,则得到负数天数差
- }
-
- int n = 0;
- while (min != max)// 实现日期的 != 重载
- {
- ++min;
- ++n;
- }
-
- return n * flag;
- }
- // ++d1
- Date& Date::operator++()
- {
- *this += 1;
- return *this;
- }
-
- // d1++ -> d1.operator(1)
- // 为了区分,构成了重载,给后置++,强行增加了一个int形参
- // 这里不需要写形参名,因为接收值是多少不重要,也不需要调用
- // 这个参数仅仅是为了前缀和后缀区分而存在的
- Date Date::operator++(int)
- {
- Date tmp(*this);
- *this += 1;
- return tmp;
- }
-
- // --d1
- Date& Date::operator--()
- {
- *this -= 1;
- return *this;
- }
-
- // d1--
- Date Date::operator--(int)
- {
- Date tmp = *this;
- *this -= 1;
- return tmp;
- }
为了区分,构成了重载,给后置++,强行增加了一个int形参。
Date Date::operator++(int)
这里不需要写形参名,因为接收值是多少不重要,也不需要调用。
这个参数仅仅是为了前缀和后缀区分而存在的。
- // 友元函数声明,声明后可访问私有成员变量
- friend ostream& operator<<(ostream& out, const Date& d);
- friend istream& operator>>(istream& in, Date& d);
-
- // 重载输出流操作符<<,用于将Date对象以特定格式输出到输出流中
- ostream& operator<<(ostream& out, const Date& d)
- {
- // 向输出流中输出Date对象的年份、月份和日期,并添加中文字符和换行符进行格式化
- out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
- // 返回输出流的引用,以便支持链式调用
- return out;
- }
-
- // 重载输入流操作符>>,用于从输入流中读取数据并设置到Date对象中
- istream& operator>>(istream& in, Date& d)
- {
- cout << "请依次输入年月日:>";
- in >> d._year >> d._month >> d._day;
-
- // 调用Date对象的CheckDate()方法检查输入的日期是否合法
- if (!d.CheckDate())
- {
- cout << "日期非法" << endl;
- }
-
- // 返回输入流的引用,以便支持链式调用
- return in;
- }
<<
是左结合的,也就是说左侧应该是流对象(如 cout
),右侧是我们想要输出的对象(如 Date
)。改变参数顺序后,调用方式也必须相应改变即(<< cout),这与常规用法不符。标准库中的 operator<<
已经定义好了左侧是 ostream&
,右侧是要输出的对象。改变参数顺序后,编译器不会再将其识别为流插入运算符,导致无法正常使用链式调用等特性。
将 operator<<
的参数顺序反过来会导致函数不能正常作为流插入运算符使用,破坏标准库的调用方式和使用习惯。
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
我们来看看下面的代码
- class Date
- {
- public:
- Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- void Print()
- {
- cout << "Print()" << endl;
- cout << "year:" << _year << endl;
- cout << "month:" << _month << endl;
- cout << "day:" << _day << endl << endl;
- }
- void Print() const
- {
- cout << "Print()const" << endl;
- cout << "year:" << _year << endl;
- cout << "month:" << _month << endl;
- cout << "day:" << _day << endl << endl;
- }
- private:
- int _year; // 年
- int _month; // 月
- int _day; // 日
- };
- void Test()
- {
- Date d1(2022, 1, 13);
- d1.Print();
- const Date d2(2022, 1, 13);
- d2.Print();
- }
const
对象不能调用非const
成员函数。
const
时,实际上是在承诺不会修改这个对象的状态。因此,只能对这个对象调用const成员函数,因为这些函数承诺不会修改对象的状态。非const
对象可以调用const
成员函数。
const
成员函数的主要目的是确保函数不会修改对象的状态。因此,对于非const
对象(即可以修改的对象)来说,调用const
成员函数是安全的,因为即使对象本身可以被修改,const
成员函数也承诺不会修改它。不可以。在const
成员函数中,不能直接调用同一个类的非const
成员函数。原因是const
成员函数承诺不会修改对象的状态,而如果它调用了非const
成员函数,就会违背这个承诺,因为非const
成员函数可能会修改对象。
然而,有一种情况可以间接调用非const
成员函数,那就是如果你将对象的const
性质通过const_cast
去除,然后调用非const
成员函数。但这种做法是不推荐的,因为它破坏了const
的正确性和对象的常量性质,可能导致未定义行为或程序错误。通常来说,应该尽量避免这种做法。
总结来说,const
成员函数内不能直接调用非const
成员函数,以保持const
成员函数的承诺不修改对象状态。
是的,非const
成员函数内部可以调用const
成员函数。
const
成员函数的主要特点是它不会修改调用它的对象的状态。因此,从逻辑上讲,在可以修改对象状态的非const
成员函数中调用一个不会修改对象状态的const
成员函数是安全的。
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
- class A
- {
- public:
- // 我们不实现,编译器会自己实现,我们实现了编译器就不会自己实现了
- // 一般不需要我们自己实现
- // 除非不想让别人取到这个类型对象的真实地址
- A* operator&()
- {
- cout << "A* operator&()" << endl;
-
- return nullptr;
- }
-
- const A* operator&() const
- {
- cout << "const A* operator&() const" << endl;
-
- return (const A*)0xffffffff;
- }
- private:
- int _a1 = 1;
- int _a2 = 2;
- int _a3 = 3;
- };
-
- int main()
- {
- A aa1;
- const A aa2;
-
- cout << &aa1 << endl;
- cout << &aa2 << endl;
-
- return 0;
- }
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容!
今天就先到这了!!!
看到这里了还不给博主扣个:
⛳️ 点赞☀️收藏 ⭐️ 关注!
你们的点赞就是博主更新最大的动力!
有问题可以评论或者私信呢秒回哦。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。