✨前言✨
🎓作者:【 教主 】
📜文章推荐:
☕博主水平有限,如有错误,恳请斧正。
📌机会总是留给有准备的人,越努力,越幸运!
💦导航助手💦
运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名以及参数列表,其返回值类型和参数列表与普通的函数类似。
函数名:关键字operator后面接需要重载的运算符符号。
函数原型:返回类型 operator操作符(参数列表)
注意:
创建一个日期类,对==操作符进行重载,如下:
- class Date
- {
- public:
- Date(size_t year = 1, size_t month = 1, size_t day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- bool operator==(const Date& d)
- {
- return d._day == _day && d._month == _month && d._year == d._year;
- }
- private:
- size_t _year;
- size_t _month;
- size_t _day;
- };
日期类实现前置++和后置++
- class Date
- {
- public:
- Date(size_t year = 1, size_t month = 1, size_t day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- Date& operator++()//前置++
- {
- _day++;
- return *this;
- }
- Date operator++(int)//后置++
- {
- Date temp(*this);
- _day++;
- return temp;
- }
- private:
- size_t _year;
- size_t _month;
- size_t _day;
- };
- }
C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递。
前置++使用引用返回,因为this指向的对象函数结束后不会销毁,引用传参效率高。后置++返回临时变量,不能使用引用传参。
初始化列表是以一个冒号开始,以逗号分隔的数据成员列表,每个成员变量后面跟一个放在括号中的初始值或者表达式。
- class Date
- {
- public:
- Date(size_t year = 1, size_t month = 1, size_t day = 1)
- :_year(year)
- ,_month(month),
- _day(day)
- {}
- private:
- size_t _year;
- size_t _month;
- size_t _day;
- };
注意
- class A
- {
- public:
- A(int a=0)
- :_a(a)
- {}
- private:
- int _a;
- };
- class B
- {
- public:
- B(int a,int ref)
- :_aobj(a)
- ,_ref(ref)
- ,_n(1)
- {}
- private:
- A _aobj;
- int& _ref;
- const int _n;
- };
用static修饰的成员变量称为静态成员变量,成员函数称为静态成员函数,静态成员一定要在类外进行初始化
特性
友元提供了一种突破封装的方式,提供了便利,但突破封装又增加了风险,因此要少用。
友元函数可以访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但是需要在类的内部声明,声明时需要加friend关键字
- void show(const Date& d)
- {
- cout << d._year << "-" << d._month << "-" << d._day << endl;
- }
-
- class Date
- {
- friend void show(const Date& d);
- public:
- Date(size_t year = 1, size_t month = 1, size_t day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- private:
- size_t _year;
- size_t _month;
- size_t _day;
- };
注意
友元类的所有成员函数都可以是另一个类的友元函数,可以访问另一个类的非公有成员
注意
如果一个类定义在类的内部,这个类就叫做内部类,内部类是一个独立的类,不属于外部类,不能通过外部类的对象访问内部类的成员,外部类对内部类没有访问权限。但是,内部类是外部类的友元。
特性:
欢迎关注,码字不易,希望多多点赞、收藏哦!抱拳了。
