目录
1.类的6个默认成员函数
默认成员函数:空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动在类中生成以下6个默认成员函数。用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
默认成员函数目的是解决某些容易忘记的操作,比如初始化、销毁等。
2. 构造函数
构造函数特征:自动初始化函数,他的特征有以下几点:
1. 函数名与类名相同。
2. 无返回值(没有void)。
3. 对象实例化时编译器
自动调用对应的构造函数,调用时是这样:Data d1(x,y,z)/Data d1。
4. 构造函数可以重载。
5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数Data d1(),一旦用户显式定义编译器将不再生成。自动生成的构造函数却不会初始化,是随机值。
![](https://img-blog.csdnimg.cn/08d353f7e1a64a2c8436576beedec53d.png)
C++给出类里面的类型如果是内置类型(char、int、char*、short)则不处理(所以需要我们写构造函数进行初始化),自定义类型(struct、class)会调用他的默认构造的原则。如图日期类,他的私有成员(_year,_month,_data)是就是内置类型,需要我们手动写构造函数。
7. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为
是默认构造函数。
![](https://img-blog.csdnimg.cn/787fd080ae4c4249b755420a2b2b686e.png)
3.析构函数
3.1
概念
通过前面构造函数的学习,我们知道一个对象是怎么来的,那一个对象又是怎么没呢的?
析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由
编译器完成的。而
对象在销毁时会自动调用析构函数,完成对象中资源的清理工作
。
3.2
特性
析构函数
是特殊的成员函数,其
特征
如下:
1.
析构函数名是在类名前加上字符
~
。
2.
无参数无返回值类型。
3.
一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构
函数不能重载
4.
对象生命周期结束时,C++编译系统系统自动调用析构函数。
5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数Data d1(),一旦用户显式定义编译器将不再生成。C++给出类里面的类型如果是内置类型(char、int、char*、short)则不处理(所以需要我们写构造函数进行初始化),自定义类型(struct、class)会调用他的默认构造的原则。如图日期类,他的私有成员(_year,_month,_data)是就是内置类型,需要我们手动写构造函数
6.析构顺序:先定义的后析构,后定义先析构。
4.拷贝构造函数
4.1 特征
析构函数是特殊的成员函数,其特征如下:
1.拷贝构造函数是构造函数的一个重载形式。
2.拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。
3.
若未显式定义,编译器会生成默认的拷贝构造函数。
默认的拷贝构造函数对象按内存存储按
字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。
![](https://img-blog.csdnimg.cn/cd64ed94ae584f53bb716e9cd739ce3c.png)
所有需要写析构函数的类,都需要写深拷贝的拷贝构造函数。反之则调用浅拷贝的默认拷贝构造函数。
拷贝构造函数与复制重载的区别:
5. 赋值运算符重载
C++
为了增强代码的可读性引入了运算符重载
,
运算符重载是具有特殊函数名的函数
,也具有其
返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字
operator
后面接需要重载的运算符符号
。
函数原型:
返回值类型
operator
操作符
(
参数列表
)
注意: 不能通过连接其他符号来创建新的操作符:比如operator@重载操作符必须有一个类类型参数用于内置类型的运算符,其含义不能改变,例如:内置的整型+
,不能改变其含义。作为类成员函数重载时,其形参看起来比操作数数目少1
,因为成员函数的第一个参数为隐藏的this .* :: sizeof ?: .
注意以上
5
个运算符不能重载。这个经常在笔试选择题中出现。
![](https://img-blog.csdnimg.cn/0d2ec369110b4aecae41df94d2e219fa.png)
日期类的实现:
class Date
{
public:
// 获取某年某月的天数--该函数由于要多次调用,为减少调用消耗的函数栈帧,采用内敛函数
inline int GetMonthDay(int year, int month)
{
int MonthArray[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year / 100 != 0) || (year % 400 == 0)))
{
return 29;
}
else
return MonthArray[month];
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
// 深拷贝-为解决浅拷贝被赋值对象析构后,引起赋值对象的改变,采用为被复制对象新开辟一段空间
int* newcapacity = (int*)malloc(sizeof(int*));
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
return *this;
}
// 析构函数
~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
// 日期+=天数 2022,10,18 += 100
Date& operator+=(int day)
{
if (day < 0)
{
//return *this -= -day;
return *this -= abs(day);
}
_day = _day + day;
while (_day > GetMonthDay(_year, _month) && _month <= 12 && _year >= 1)
{
_day = _day - GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
// 日期+天数--直接拷贝构造一个
Date operator+(int day)
{
Date ret(*this);// 拷贝构造函数
ret += day;
return ret;
}
// 日期-天数--2022,10,18 -20
Date operator-(int day)
{
_day = _day - day;
while (_day <= 0)
{
_month--;
if (_month == 0 && _year > 1)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期-=天数
Date& operator-=(int day)
{
Date ret(*this);
ret -= day;
return *this;
}
// 前置++ 返回++以后的值,该值出了作用域还在
Date& operator++()
{
*this += 1;
return *this;
}
// 后置++ 返回++之前的值,返回拷贝的值,多一个参数(int型)主要是为了跟前置进行区分,编译器约定的
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
// 后置--
Date operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
// 前置--
Date& operator--()
{
*this -= 1;
return *this;
}
// >运算符重载
bool operator>(const Date& d)
{
if (this->_year > d._year)
{
return true;
}
if (this->_year == d._year && this->_month > d._month)
{
return true;
}
if (this->_year == d._year && this->_month == d._month && this->_day > _day)
{
return true;
}
else
return false;
}
// ==运算符重载
bool operator==(const Date& d)
{
return this->_year == d._year
&& this->_month == d._month
&& this->_day == d._month;
}
// >=运算符重载
bool operator>= (const Date& d)
{
return *this > d || *this == d;
}
// <运算符重载
bool operator< (const Date& d)
{
if (this->_year < d._year)
return true;
if (this->_year == d._year && this->_month < d._month)
{
return true;
}
if (this->_year == d._year && this->_month == d._month && this->_day < d._day)
{
return true;
}
else
return false;
}
// <=运算符重载--
bool operator<= (const Date& d)
{
return *this < d || *this == d;
}
// !=运算符重载
bool operator!= (const Date& d)
{
if (this->_month == d._month && this->_day == d._day && this->_year == d._year)
{
return false;
}
else
return true;
}
private:
{
int _year;
int _month;
int _day;
}
6. const成员函数
const可以在成员函数的后面进行修饰,他修饰的是接收类对象的this指针,降低权限。表明在该成员函数中不能对类的任何成员进行修改。