有没有跟一起做网店一样的网站,网页制作怎么做模板,网站打开慢如何优化,公司logo设计图片大全上一篇博客学的默认成员函数是类和对象的最重要的内容#xff0c;相信大家已经掌握了吧#xff0c;这一篇博客接着继续剩下的内容#xff0c;加油#xff01;
目录
一、const成员#xff08;理解#xff09;
1.0 引入
1.1 概念
1.2 总结
1.2.1 对象调用成员函数
… 上一篇博客学的默认成员函数是类和对象的最重要的内容相信大家已经掌握了吧这一篇博客接着继续剩下的内容加油
目录
一、const成员理解
1.0 引入
1.1 概念
1.2 总结
1.2.1 对象调用成员函数
1.2.2 成员函数调用成员函数
二、取地址及const取地址操作符重载了解
三、日期类的重新规范书写
3.1 Date.h
3.2 Date.cpp 一、const成员理解
1.0 引入 先看代码
#include iostream
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year year;_month month;_day day;}void Print(){cout year: _year endl;cout month: _month endl;cout day: _day endl endl;}private:int _year; // 年int _month; // 月int _day; // 日
};void func( Date d) 这里的形参为对象它会进行调用一次拷贝构造
{d.Print();
}int main()
{Date d1(2024, 7, 20);func(d1); 这里直接传递的是对象return 0;
}
程序运行结果、 再看代码
#include iostream
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year year;_month month;_day day;}void Print(){cout year: _year endl;cout month: _month endl;cout day: _day endl endl;}private:int _year; // 年int _month; // 月int _day; // 日
};void func( const Date d) 这里的形参为对象的引用/别名它就是d1的别名同一个实体不会进行拷贝构造 加const代表我引用这个对象但是我不会通过引用从而修改这个对象
{d.Print();
}int main()
{Date d1(2024, 7, 20);func(d1); 这里直接传递的是对象return 0;
}
程序运行结果 为什么会是上面的结果 其实这就是我们在入门阶段学习的指针的相互赋值指针的能力不能出现扩张这样编译器会报错那又该如何解决呢 这便引入了要学习的const成员。 void Print() const // void Print(const Date* this ) {cout year: _year endl;cout month: _month endl;cout day: _day endl endl;}
1.1 概念 将const修饰的“成员函数”称之为const成员函数const修饰类成员函数实际修饰该成员函数 隐含的this指针表明在该成员函数中不能对类的任何成员变量进行修改。const修饰的指针比如_year 2026; (编译器底层this-_year2026;) 这是不允许的 复习 const Date* p1 : const修饰的是指针指向的对象也就是说不可以通过这个指针来修改对象的数据Date const * p2 : 和上面的一样const修饰的是指针指向的对象也就是说不可以通过这个指针来修改对象的数据Date * const p3 : const修饰的是指针p3本身也就是说不可以修改指针的指向const Date* const p4: const既修饰指针指向的对象也就是说不可以通过这个指针来修改对象的数据也修饰了指针p3本身也就是说不可以修改指针的指向双重限定 1.2 总结
1.2.1 对象调用成员函数 1. const对象可以调用非const成员函数吗 不可以。const对象只能调用const成员函数因为const对象不能被修改而非const成员函数可能会修改对象的状态。 class MyClass
{public:void nonConstFunc() {// 修改对象状态的代码}void constFunc() const {// 不修改对象状态的代码}
};int main(){const MyClass obj;obj.constFunc(); // 可以调用obj.nonConstFunc(); // 编译错误return 0;}2. 非const对象可以调用const成员函数吗 可以。非const对象可以调用const成员函数因为const成员函数保证不会修改对象的状态。 class MyClass
{
public:void nonConstFunc() {// 修改对象状态的代码}void constFunc() const {// 不修改对象状态的代码}
};int main()
{MyClass obj;obj.constFunc(); // 可以调用obj.nonConstFunc(); // 也可以调用return 0;
}1.2.2 成员函数调用成员函数 1. const成员函数内可以调用其它的非const成员函数吗 不可以。const成员函数不能调用非const成员函数因为这会违反const成员函数不修改对象状态的承诺。 class MyClass
{
public:void nonConstFunc() {// 修改对象状态的代码}void constFunc() const {nonConstFunc(); // 编译错误}
};int main()
{MyClass obj;obj.constFunc();return 0;
}2. 非const成员函数内可以调用其它的const成员函数吗 可以。非const成员函数可以调用const成员函数因为const成员函数不会修改对象的状态。 class MyClass
{
public:void nonConstFunc() {constFunc(); // 可以调用// 修改对象状态的代码}void constFunc() const{// 不修改对象状态的代码}
};int main()
{MyClass obj;obj.nonConstFunc();return 0;
}理解记忆明确一个原则只要调用成员函数都涉及this指针我们就要分析指针类型的变化。 结论什么时候会给成员函数加const 只要成员函数中不需要修改成员变量最好都加上const但是如果你需要改变成员变量你就不能加const因为这个时候const对象可以调用这个const修饰的成员函数非const对象普通对象也可以调用这个const修饰的成员函数。 二、取地址及const取地址操作符重载了解
这两个默认成员函数一般不用重新定义 编译器默认会生成。
#include iostream
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year year;_month month;_day day;}void Print() const{cout year: _year endl;cout month: _month endl;cout day: _day endl endl;}/*自己实现的取地址运算符重载函数以及const取地址操作符重载函数构成函数重载Date* operator(){return this;//return nullptr;}const Date* operator() const{cout operator()endl;return this;//return nullptr;}*/private:int _year; // 年int _month; // 月int _day; // 日
};void func( const Date d)
{d.Print();
}int main()
{Date d1(2024, 7, 20);Date d2(2024, 7, 21);const Date d3(2024, 7, 22);cout d1 endl; //调用的是取地址运算符重载函数不实现的话编译器默认会生成cout d2 endl; //调用的是取地址运算符重载函数不实现的话编译器默认会生成cout d3 endl; //调用的是const取地址操作符重载函数不实现的话编译器默认会生成return 0;
} 这两个运算符一般不需要重载使用编译器生成的默认取地址的重载即可只有特殊情况才需要重载比如不想让别人获取到指定的内容
三、日期类的重新规范书写
3.1 Date.h
#pragma once
#include iostream
using namespace std;class Date
{
public:int GetMonthDay(int year, int month) const;Date(int year 0, int month 1, int day 1);Date(const Date d);~Date();void Print() 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;Date operator(const Date d);Date operator(int day) const;Date operator(int day); //不能加Date operator-(int day) const;Date operator-(int day); //不能加Date operator(); //不能加Date operator(int);Date operator--();Date operator--(int);int operator-(const Date d) const;private:int _year;int _month;int _day;
};
3.2 Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1#include Date.hint Date::GetMonthDay(int year, int month) const
{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::Date(int year , int month , int day ) //缺省参数只在声明中给
{if (year 0 month 1 month 12 day 1 day GetMonthDay(year, month)){_year year;_month month;_day day;}else{cout 非法日期 endl;}
}Date::Date(const Date d)
{_year d._day;_month d._month;_day d._day;
}Date::~Date()
{cout ~Date() endl;
}void Date::Print() const
{cout _year - _month - _day endl;
}bool Date::operator(const Date d) const
{if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}return false;
}bool Date::operator(const Date d) const
{if (_year d._year _month d._month _day d._day){return true;}return false;}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); }Date Date::operator(const Date d)
{if (this ! d) {_year d._day;_month d._month;_day d._day;}return *this;
}Date Date::operator(int day) const
{Date ret(*this); ret._day day;while (ret._day GetMonthDay(ret._year, ret._month)){ret._day - GetMonthDay(ret._year, ret._month);ret._month;if (ret._month 13) {ret._year;ret._month 1; }}return ret;
}Date Date::operator(int day)
{if (day 0){return *this - -day;}_day day;while (_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;if (_month 13) {_year;_month 1; }}return *this;
}Date Date::operator-(int day) const
{Date ret(*this); ret._day - day;while (ret._day 0) {--ret._month;if (ret._month 0){--ret._year; ret._month 12; }ret._day GetMonthDay(ret._year, ret._month);}return ret;}Date Date::operator-(int day)
{if (day 0){return *this -day; }_day - day;while (_day 0) {--_month;if (_month 0){--_year; _month 12; }_day GetMonthDay(_year, _month);}return *this;
}Date Date::operator()
{*this 1;return *this;
}Date Date::operator(int)
{Date tmp(*this); *this 1;return tmp;
}Date Date::operator--()
{*this - 1;return *this;
}Date Date::operator--(int)
{Date tmp(*this); *this - 1;return tmp;
}int Date::operator-(const Date d) const
{int flag 1;Date max *this; Date min d;if (*this d) {max d;min *this;flag -1;}int n 0;while (min ! max){min;n;}return n * flag;
} 至此C面向对象-中全部内容就学习完毕这一节内容比较重要建议多看几遍认真复习消化熟练使用C相对来说较为复杂我们应该时刻理清自己的思路耐下心来一点点积累 星光不问赶路人加油吧感谢阅读如果对此专栏感兴趣点赞加关注