类的6个默认成员函数
1.构造函数
构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员 都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
构造函数特点
构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主 要任务并不是开空间创建对象,而是初始化对象。
其特征如下:
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
class Date
{
public:
Date() //无参构造函数
{
_year=1996;
_month=1;
_day=29; //Date()和Date(int year,int month,int day)两个构造函数不能 同时存在,
}
Date(int year,int month,int day) //带参全缺省构造函数
{
_year=year;
_month=month;
_day=day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
}
无参构造函数和全缺省构造函数不能同时存在;
2.析构函数
析构函数:与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而 对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。
析构函数特点
析构函数是特殊的成员函数。
其特征如下:
-
析构函数名是在类名前加上字符 ~。
-
无参数无返回值。
-
一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
-
对象生命周期结束时,C++编译系统系统自动调用析构函数。
class seqlist { public: seqlist(int capacity=10) //构造函数 { _pData = (int*)malloc(capacity * sizeof(int)); assert(_pData); _size = 0; _capacity = capacity; } ~SeqList() //析构函数 { if (_pData) { free(_pData ); // 释放堆上的空间 _pData = NULL; // 将指针置为空 _capacity = 0; _size = 0; } } private: int* _pData ; size_t _size; size_t _capacity; }; int main() { seqlist d1(10); }
3.拷贝构造函数
拷贝构造函数也是特殊的成员函数,其特征如下:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
class Date
{
public:
Date() //无参构造函数;
{
_year=1996;
_month=1;
_day=29;
}
Date(const Date& d) //拷贝构造函数,形参必须传引用,类类型的引用 ,传值会导致无限递归
{
_year=d._year;
_month=d._month;
_day=d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1; //定义类对d1;
Date d2(d1); //将d1拷贝给d2;
}
4.赋值运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类 型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意:
1.不能通过连接其他符号来创建新的操作符:比如operator@
2.重载操作符必须有一个类类型或者枚举类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
4.作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的 操作符有一个默认的形参this,限定为第一个形参
- .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
#include<iostream>
#include<windows.h>
using namespace std;
class Date
{
public:
Date(int year=1990, int month=1,int day=1)
{
if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > yearmonthday(year, month))
{
cout << "输入日期有误" << endl;
_year = year;
_month = month;
_day = day;
}
else
{
_year = year;
_month = month;
_day = day;
}
}
Date(const Date& d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
int yearmonthday(int year, int month)
{
static int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (2 == month)
{
arr[month-1] += 1;
}
}
int day = arr[month - 1];
return day;
}
Date& operator+=(int day)
{
if (day<0)
{
return *this -= -day;
}
_day += day;
//考虑是否需要进位
while(_day > yearmonthday(_year, _month))
{
_day -= yearmonthday(_year, _month);
_month++;
//判断月份是否合理
if (_month == 13)
{
_month = 1;
++_year;
}
}
return *this;
}
Date& operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
_month = 12;
--_year;
}
_day += yearmonthday(_year, _month);
}
return *this;
}
Date operator+(int days)
{
Date ret(*this);
ret += days;
return ret;
}
Date operator-(int days)
{
Date ret(*this);
ret -= days;
return ret;
}
/*前置++*/
Date& operator++()
{
return *this += 1; //先自加,再返回
}
//后置++
Date operator++(int)
{
Date ret(*this); //先返回自加之前的值,再返回
*this+= 1;
return ret;
}
Date operator--(int) //后置--
{
Date ret(*this);
*this-= 1;
return ret;
}
Date& operator--() //前置--
{
*this -= 1;
return *this;
}
bool operator==(const Date& d) const
{
return this->_year == d._year&&this->_month == d._month&&this->_day == d._day;
}
bool operator>(const Date& d) const
{
if (this->_year > d._year)
{
return true;
}
else if ((this->_year == d._year) && (this->_month > d._month))
{
return true;
}
else if ((this->_year == d._year) && (this->_month >= d._month) && this->_day > d._day)
{
return true;
}
else
{
return false;
}
}
bool operator>=(const Date& d) const
{
return !(*this < d );
}
bool operator<(const Date& d) const
{
return ((*this >d)||(*this==d));
}
bool operator<=(const Date& d) const
{
return !(*this>d);
}
bool operator!=(const Date& d) const
{
return !(*this==d);
}
int operator-(const Date& d)
{
Date ret(*this);
int flag = 1;
if (ret < d)
{
flag = -1;
}
int day = 0;
if (ret < d)
{
while (ret < d)
{
++ret;
++day;
}
}
else
{
while (ret>d)
{
--ret;
--day;
}
}
return day*flag;
}
void display()
{
cout << _year<<"." << _month <<"."<< _day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2019, 3,1);
Date d2(2020, 1,1);
cout<< "d1:" << endl;
d1.display();
cout <<endl<<"d2:"<< endl;
d2.display();
cout << endl;
cout << "d1==d2 " << (d1 == d2) << endl;
cout << "d1!=d2 " << (d1 != d2) << endl;
cout << "d1>=d2 " << (d1 >= d2) << endl;
cout << "d1<=d2 " << (d1 <= d2) << endl;
cout << "d1>d2 " << (d1 > d2) << endl;
cout << "d1<d2 " << (d1 < d2) << endl;
++d1;
++d2;
cout << "++d1:" << endl;
d1.display();
cout <<endl<< "++d2:" << endl;
d2.display();
Date d3=d2 + 365;
cout << endl << "d2+365" << endl;
d3.display();
Date d4 = d2 - 365;
cout <<endl <<"d2-365" << endl;
d4.display();
system("pause");
return 0;
}
赋值运算符主要有四点:
-
参数类型
-
返回值
-
检测是否自己给自己赋值
-
返回*this
-
一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
5.const修饰类的成员函数
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this 指针,表明在该成员函数中不能对类的任何成员进行修改。
6.取地址及const取地址操作符重载
class Date { public: Date* operator&() //取地址操作符重载 { return this; } const Date* operator&() const //const 取地址操作符重载 { return this; } private: int _year; int _month; int _day; };
这两个成员函数一般不用重新定义,编译器默认会生成;