欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > C++进阶之路:日期类的实现、const成员(类与对象_中篇)

C++进阶之路:日期类的实现、const成员(类与对象_中篇)

2024/10/24 6:22:23 来源:https://blog.csdn.net/Srlua/article/details/143158840  浏览:    关键词:C++进阶之路:日期类的实现、const成员(类与对象_中篇)


✨✨ 欢迎大家来访Srlua的博文(づ ̄3 ̄)づ╭~✨✨

🌟🌟 欢迎各位亲爱的读者,感谢你们抽出宝贵的时间来阅读我的文章。

我是Srlua小谢,在这里我会分享我的知识和经验。🎥

希望在这里,我们能一起探索IT世界的奥妙,提升我们的技能。🔮

记得先点赞👍后阅读哦~ 👏👏

📘📚 所属专栏:C/C++

欢迎访问我的主页:Srlua小谢 获取更多信息和资源。✨✨🌙🌙

​​

​​

目录

日期类的实现

const成员

1. const 对象可以调用非 const 成员函数吗?

2. 非 const 对象可以调用 const 成员函数吗?

3. const 成员函数内可以调用其它的非 const 成员函数吗?

4. 非 const 成员函数内可以调用其它的 const 成员函数吗?

总结

取地址及const取地址操作符重载


日期类的实现

class Date
{
public:// 获取某年某月的天数int GetMonthDay(int year, int month){static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 
31};int day = days[month];if (month == 2&&((year % 4 == 0 && year % 100 != 0) || (year%400 == 0))){day += 1;}return day;}// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数// d2(d1)Date(const Date& d);// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d);// 析构函数~Date();// 日期+=天数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);
private:int _year;int _month;int _day;
}

const成员

将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();
}

在 C++ 中const 对象、非 const 对象、const 成员函数和非 const 成员函数之间的关系是理解对象和成员函数修饰符的重要方面。

思考下面的几个问题:

  1. const对象可以调用非const成员函数吗?

  2. 非const对象可以调用const成员函数吗?

  3. const成员函数内可以调用其它的非const成员函数吗?

  4. 非const成员函数内可以调用其它的const成员函数吗?

1. const 对象可以调用非 const 成员函数吗?

答案:不可以。

当你有一个 const 对象时,它被视为不可修改的。这意味着你不能调用会修改对象状态的非 const 成员函数。例如:

class MyClass {
public:void nonConstFunction() {// 这个函数会修改对象的状态}
};const MyClass obj;
obj.nonConstFunction(); // 错误:无法从 const 对象调用非 const 成员函数

2. 非 const 对象可以调用 const 成员函数吗?

答案:可以。

const 对象可以调用 const 成员函数,因为 const 成员函数承诺不修改对象的状态。例如:

class MyClass {
public:void constFunction() const {// 这个函数不会修改对象的状态}
};MyClass obj;
obj.constFunction(); // 正确:可以从非 const 对象调用 const 成员函数

3. const 成员函数内可以调用其它的非 const 成员函数吗?

答案:不可以。

const 成员函数内部,不能调用任何非 const 成员函数,因为 const 成员函数不允许修改对象的状态。这是为了保持 const 的语义。例如:

class MyClass {
public:void nonConstFunction() {// 这个函数会修改对象的状态}void constFunction() const {nonConstFunction(); // 错误:无法在 const 成员函数中调用非 const 成员函数}
};

4. 非 const 成员函数内可以调用其它的 const 成员函数吗?

答案:可以。

const 成员函数可以调用 const 成员函数,因为 const 成员函数不会修改对象的状态。如下所示:

class MyClass {
public:void constFunction() const {// 这个函数不会修改对象的状态}void nonConstFunction() {constFunction(); // 正确:可以在非 const 成员函数中调用 const 成员函数}
};

总结

  • const 对象不能调用非 const 成员函数。
  • 非 const 对象可以调用 const 成员函数。
  • const 成员函数不能调用非 const 成员函数。
  • 非 const 成员函数可以调用 const 成员函数。

这些规则确保了对象的状态被正确管理和保护,符合 C++ 的设计原则。

取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Date
{ 
public :Date* operator&(){return this ;}const Date* operator&()const{return this ;}
private :int _year ; // 年int _month ; // 月int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需 要重载,比如想让别人获取到指定的内容

 ​​

希望对你有帮助!加油!

若您认为本文内容有益,请不吝赐予赞同并订阅,以便持续接收有价值的信息。衷心感谢您的关注和支持!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com