1、类的6个默认成员函数
空类:如果一个类中什么成员都没有,形如下面这样,我们就称其为空类。
class Date { };
那空类中什么都没有吗?并不是,任何一个类,在我们不写内容的情况下都会自动生成6个默认成员函数。
2、构造函数
1、概念
构造函数是一个特殊的成员函数,名字和类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有一个合适的初始值,并且在对象的生命周期内只调用一次。
2、特性
构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫做构造,但是构造函数主要的任务并不是开辟空间创建对象,而是初始化对象。
特征:
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
class Date
{
public:
//1、无参构造函数
Date()
{}
//2、带参构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d1;//调用无参构造函数
Date d2(2019, 6, 16);//调用带参构造函数
}
5.如果类中没有显示定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显示定义编译器将不再生成。
class Date
{
public:
//如果用户没有显示定义任何构造函数,编译器将会生成一个默认的构造函数
Date()
{}
//无参构造函数和全缺省的构造函数不能同时存在
//全缺省的构造函数
Date(int year=2010, int month=1, int day=1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d;
return 0;
}
6.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数都可以认为是默认成员函数。
7.有一个问题:在我们不实现构造函数的情况下,编译器会生成默认的构造函数,但是看起来默认构造函数又没什么用,d对象调用了编译器生成的默认构造函数,但是d对象year/month/day的值依旧是随机值,所以这里编译器生成的默认构造函数并没有用??
class Time
{
public:
Time(int hour = 0, int minute = 0, int second = 0)
{
_hour = hour;
_minute = minute;
_second = second;
cout << "Time::Time(int,int,int):" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date()
{
cout << "Date()" << endl;
}
void SetDate(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void PrintDate()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
Time _t;
};
int main()
{
//d---->t---->调用时间类的构造函数
//显示定义构造函数
Date d; //call Time(int,int,int)
return 0;
}
运行这段代码,得到如下结果:
可知C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语法已经定义好的类型:如int/char...,自定义类型就是我们使用struct/class/union自己定义的类型,由以上程序我们可知编译器默认生成的构造函数会对自定义类型成员_t调用它的默认成员函数。
8.成员变量的命名风格
一般情况下建议给成员变量前面加一个下划线以示区分,不做具体要求。
3、析构函数
1、概念
简单地说,一个对象是通过构造函数来的,然后通过析构函数没的。
析构函数:与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。
2、特性
析构函数是特殊的成员函数。
特征:
- 析构函数名是在类名前面加上字符~。
- 无参数无返回值。---->不能重载
- 一个类有且只有一个析构函数。若未显示定义,系统会自动生成默认的析构函数。
- 对象生命周期结束时,C++编译系统自动调用析构函数。
typedef int DataType;
class SeqList
{
public:
SeqList(int capacity = 3)
{
cout << "SeqList(int):" << this << endl;
_array = (DataType*)malloc(sizeof(DataType)*capacity);
if (_array == nullptr)
{
assert(0);
return;
}
_capacity = capacity;
_size = 0;
}
//析构函数没有参数
~SeqList()
{
cout << "~SeqList():" << this << endl;
if (_array)
{
free(_array);
_array = nullptr;
}
}
private:
DataType* _array;
size_t _size;
size_t _capacity;
};
void TestSeqList()
{
SeqList s;
}
int main()
{
TestSeqList();
return 0;
}
5.关于编译器自动生成的析构函数,是否会完成一些事情呢?编译器生成的默认析构函数,会对自定义类型成员调用它的析构函数。
4、拷贝构造函数
1、概念
问题:在创建对象时,可否创建一个与一个对象一模一样的新对象呢?
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
2、特征
拷贝构造函数也是特殊的成员函数,其特征如下:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
class Date
{
public:
//全缺省的构造函数
Date(int year = 2010, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._month;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date da(d1);
return 0;
}
若使用传值的方式,会引发对象的拷贝,然后层层传值引发对象的拷贝的递归调用,但其实如果你写了这样的代码,首先编译就过不去,编译器不允许这样的代码编译成功。
3.若未显示定义,系统生成默认的拷贝构造函数。默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
4.那么编译器 生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,我们还需要自己去实现吗?当然像日期类这种类是没有必要的,但string类就不是这样了。
#include <malloc.h>
#include <assert.h>
class String
{
public:
String(const char* pstr = "")
{
_str = (char*)malloc(strlen(pstr) + 1);
if (_str == nullptr)
{
assert(0);
return;
}
strcpy(_str, pstr);
cout << "String()" << endl;
}
~String()
{
if (_str)
{
free(_str);
}
cout << "~String()" << endl;
}
private:
char* _str;
};
void TestString()
{
String s1("hello");
String s2(s1);
}
int main()
{
TestString();
return 0;
}
运行这段代码后会崩溃,因为同一块地址重复释放,而比较这个类与之前的日期类我们可以知道:一个类中如果管理资源了,那一定要给出拷贝构造函数。
对于浅拷贝问题:即多个对象共用同一份资源,销毁时同一份资源释放多次,必然会引起崩溃。
5、赋值运算符重载
1、运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型operator操作符(参数列表)
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型或者枚举类型的操作数
- 用于内置类型的操作符,其含义不能改变,例如:内置的整形+,不能改变其含义。
- 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的,操作符有一个默认的形参this,限定为第一个参数
- .*、::、sizeof、?:、.注意以上5个运算符不能重载。
class Date
{
public:
Date(int year = 2010, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//private:
int _year;
int _month;
int _day;
};
//这里会发现运算符重载成全局的就需要成员变量是共有的,那么该如何保证其封装性?
//这个问题可以用友元解决,也可以重载成成员函数
bool operator==(const Date& d1, const Date& d2)
{
return d1._year == d2._year
&&d1._month == d2._month
&&d1._day == d2._day;
}
void Test()
{
Date d1(2019, 6, 16);
Date d2(2019, 6, 17);
cout << (d1 == d2) << endl;
}
class Date
{
public:
Date(int year = 2010, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//bool operator==(Date* this,const Date& d2)
//这里需要注意的是左操作数是this指向的调用函数的对象
bool operator==(const Date& d2)
{
return _year == d2._year
&&_month == d2._month
&&_day == d2._day;
}
private:
int _year;
int _month;
int _day;
};
void Test()
{
Date d1(2019, 6, 16);
Date d2(2019, 6, 17);
cout << (d1 == d2) << endl;
}
2、赋值运算符重载
赋值运算符主要有4点:
- 参数类型
- 返回值
- 检测是否自己给自己赋值
- 返回*this
- 一个类如果没有显示定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝(也是浅拷贝)
class Date
{
public:
Date(int year = 2010, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._month;
}
//d1=d2=d3;
Date& operator=(const Date& d)
{
if (this != &d)//检测是否自己给自己赋值
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2019, 6, 15);
Date d2(2019, 6, 16);
Date d3(2019, 6, 17);
d1 = d2 = d3;
return 0;
}
这里我们要实现d3给d2赋值,再用d2给d1赋值,所以在赋值运算符重载时,d3给d2赋值的过程中需要返回的是d2,即当前的*this,为了保证地址的一致性,得到的是d2而不是它的一份临时拷贝,采用了返回引用,此处返回的值生命周期大于该赋值运算符重载函数,所以返回引用没有问题。
虽然编译器生成的默认赋值重载函数可以完成字节序的值拷贝了,但这并不是说我们就不用自己实现了,比如以下的代码:
class String
{
public:
String(const char* pstr = "")
{
_str = (char*)malloc(strlen(pstr) + 1);
if (_str == nullptr)
{
assert(0);
return;
}
strcpy(_str, pstr);
cout << "String()" << endl;
}
~String()
{
if (_str)
{
free(_str);
}
cout << "~String()" << endl;
}
private:
char* _str;
};
int main()
{
String s1("hello");
String s2("world");
s1=s2;
}
我们会发现这个程序会崩溃掉,同样是浅拷贝,所以同样的会出现资源多次释放的问题,且刚开始s1与s2分别得到了一块空间,赋值操作后s1与s2的地址变得一模一样了,此时s2原本的那段空间丢失了,会出现内存泄漏的问题,最后调用析构函数时,多次释放同一块空间,会造成程序崩溃。
接下来我们看一个++运算符重载的实现方法(前置++与后置++)
这个问题重点在于返回值,前置++与后置++的区别在于是先自增再运算还是先运算再自增,所以在前置++运算中我们需要返回加1后的*this,而后置++我们需要临时创建一个temp存放此时的*this,然后再自增。
class Date
{
public:
Date(int year = 2010, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//前置++
Date& operator++()
{
_day += 1;
return *this;
}
//后置++
Date operator++(int)
{
Date temp(*this);
_day += 1;
return temp;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2019, 6, 17);
Date d2;
d2 = ++d1;
d2 = d1++;
return 0;
}
6、const成员
1、const修饰类的成员函数
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
void Display () const ------> void Display (const Date* this)
class Date
{
public:
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// const成员函数中不能修改成员变量
// const修饰this指针,表明:当前对象中的内容不允许被修改
// const Date* const
void PrintDate()const
{
//this->_day = 1;
cout << _year << "-" << _month << "-" << _day << endl;
}
// Date* const
void PrintDate()
{
_day = 1;
cout << _year << "-" << _month << "-" << _day << endl;
}
bool operator==(const Date& d)const
{
//_day = 1;
//d._day = 1;
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
Date* operator&()
{
return this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d(2019, 6, 16);
d.PrintDate();
cout << &d << endl;
const Date cd(2019, 6, 16);
cout << &cd << endl;
return 0;
}
我们需要知道普通类型的成员函数中的this:可读可写
const类型的成员函数中的this:只读,不可修改
有了这样的结论后,我们来看下面这几道题:
- const对象可以调用非const成员函数吗?不可以
- 非const对象可以调用const成员函数吗?可以
- const成员函数内可以调用其他的非const成员函数吗?不可以
- 非const成员函数内可以调用其他的const成员函数吗?可以