const关键字:
C语言—>const只能修饰变量
C++—>const修改全局或者局部变量、修饰成员函数、修饰成员变量
若一定修改某个成员变量时在定义该成员变量的时候,使用mutable关键字修改该成员
class Date { public: //Date* const-->this的指向不能修改,但空间内容可修改 //const Date* const-->this的指向和内容都不能修改 void Print()const//const成员函数:const修饰成员函数,实际是修饰this指针 不能修改成员变量 { _day++; cout << _year << "/" << _month << "/" << _day << endl; } private: int _year; int _month; mutable int _day; };静态成员函数:
0.通过对象.静态成员函数(…)||类名::静态成员函数(…)
1.必须被static修饰
2.没有this指针,不能直接访问非静态成员变量
3.不可被const修饰
0.必须通过对象.静态成员函数(…)
1.不需要static修饰
2.具有隐藏的this指针,可访问普通成员函数
3.可被const修饰
class Date { public: //静态成员函数 static int Getcount() { return _count; } private: int _year; int _month; //不在对象中存储,是对每一个对象的共享 static int _count;//静态成员变量(在类中只声明,必须在类外定义) }; //必须在成员函数前加类名,且不用static关键字 int Date::_count = 0; int main() { TestDate(); cout << Date::Getcount() << endl; }构造函数:
成员变量顺序和初始化列表顺序须保持一致
初始化列表可以不写,不写不代表编译器不执行初始化列表
如果用户满没有显示写出初始化列表,编译器会自动补全
对于类中内置类型成员变量,使用随机值填充
对于类中自定义类型的成员变量,调用对应类的无参或者全缺省参数初始化构造
//explicit关键字构造方法,表明禁止单参构造方法类型转化void TestDate4() explicit Date(int year = 1999, int month = 1, int day = 1) :_year(year), _month(month), _day(day) {} //构造函数 //Date(int year = 1999, int month = 1, int day = 1) //{ // _year = year; // _month = month; // _day = day; //} void TestDate4() { Date d(2022); //先用2023调用单参的构造方法创建一个匿名对象 //使用匿名对象给d进行赋值 //匿名对象赋值结束之后被销毁 //d = 2023;使用explicit关键字后无法调用 }拷贝构造函数 :
Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } //拷贝构造函数场景1 void TestDate1() { Date d1(1999, 1, 1); Date d2(d1); } //拷贝构造函数场景2:以值的方式传参 void TestDate2(Date d) { } //拷贝构造函数场景3:以值的方式返回 //如果返回的是匿名对象,则不会进行拷贝,而是直接将匿名对象返回 //匿名对象:没有名字的对象 例:Date(1999,1,1) //场景2同理 Date TestDate3() { Date d; return d; }重载:
重载==
bool operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; }
重载=
Date& operator=(const Date& d) { if (this != &d)//检测是否为自己赋值 { _year = d._year; _month = d._month; _day = d._day; } //return d;//无法完成a1=a2=a3;的赋值 return *this; }
重载 后置++ 前置++
//为了区分前置++,后置++,规定给后置++添加一个int参数 //重载 后置++ (a++) Date& operator++(int) { Date temp(*this); _day += 1; return temp; } //重载 前置++(++a) Date& operator++() { _day += 1; return *this; }
重载流运算符:
第一个参数必须是ostream&,因此该运算符不能重载成类的成员函数,只能重载成全局函数,并在类中声明友元函数
类的成员函数**(不使用)**:
//重载<< //void operator<<(Date* const this,ostream& out) //须d<<cout;调用不符合常规 //d.operator<<(cout); void operator<<(ostream& out) { out << _year << "/" << _month << "/" << _day << endl; }
全局函数,在类中的友元函数:
class Date { public: Date(int year = 1999, int month = 1, int day = 1) :_year(year), _month(month), _day(day) { } //友元函数 friend ostream& operator<>(istream& in, Date& d); private: int _year; int _month; int _day; }; //返回值类型ostream&,目的是为了支持连续输出 ostream& operator<<(ostream& out, const Date& d) { out << d._year << "/" << d._month << "/" <>(istream& in, Date& d) { in >> d._year >> d._month >> d._day; } int main() { Date d1(2222,2,2); cout<<d1; }析构函数
~Date() { }友元函数:
1.不是成员函数,则没有this指针
2.不能被const修饰,因为没有this指针
3.不受访问限定符约束,因为不是类的成员函数
4.一个类可拥有多个友元函数
5.不具有交换性
class Time { public: Time(int hour,int minute,int second):_hour(hour),_minute(minute),_second(second) {} //友元函数 friend void TestFriend(); private: int _hour; int _minute; int _second; }; class Date { public: Date(int year = 1999, int month = 1, int day = 1) :_year(year), _month(month), _day(day) { } //友元函数 friend void TestFriend(); private: int _year; int _month; mutable int _day; }; void TestFriend() { Date d(2002,1,1); d._day+=1; Time t(16,2,1); t._second+=1; }内部类:(外部类的友元类)
是一个独立的类,不属于外部类,更不能通过外部类的对象去访问内部类的成员
//内部类 class List { class ListNode { public: ListNode(ListNode* next, int data = 0) :_next(nullptr), _data(data) { } private: ListNode* _next; int _data; }; public: List(List* head) :_head(nullptr) { } public: void PushBack(int data) { } void PushFront(int data) { } void PopBack() { } void PopFront() { } private: List* _head; };