文章目录
一、类的6个默认成员函数
如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数。
class Date {};
二、构造函数
2.1 概念
对于以下的日期类:
#include <iostream>
#include <string.h>
using namespace std;
class Date
{
public:
void Init(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;
};
int main()
{
Date d1;
Date d2;
d1.Init(2022, 1, 17);
d1.PrintDate();
d2.Init(2022, 1, 30);
d2.PrintDate();
return 0;
}
对于Date类,可以通过Init公有的方法给对象设置内容,但是如果每次创建对象都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?
构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
2.2 特性
构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象。
其特征如下:
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
基于上面这四点,下面设计构造函数
#include <iostream>
#include <string.h>
using namespace std;
class Date
{
public:
//无参构造函数
Date()
{
_year = 0;
_month = 1;
_day = 1;
}
//带参构造函数
Date(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;
};
int main()
{
Date d1;//调用无参构造函数
d1.PrintDate();
Date d2(2022, 1, 17);//调用有参构造函数
d2.PrintDate();
return 0;
}
注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
我们也可以将无参的构造函数和有参的构造函数合并成一个全缺省或者半缺省的构造函数。
class Date
{
public:
//推荐使用全缺省,或者办半缺省
Date(int year = 0, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void PrintDate()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
- 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
不是编译器会自动生成一个无参的默认构造函数吗?这里看起来并没有什么变化啊!继续看下面的例子:
#include <iostream>
#include <string.h>
using namespace std;
class A
{
public:
A()
{
cout << "A()" << endl;
_a = 0;
}
private:
int _a;
};
class Date
{
public:
void PrintDate()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
//C++里面把类型分为两类:内置类型和自定义类型
//内置类型:int/char/double/指针/指针类型数组等等
//自定义类型:struct/class定义的类型
//我们不写构造函数,编译器会自动生成默认构造函数,对于内置类型不做初始化处理
//对于自定义类型会去调用它的默认构造函数(不用参数就可以调用)初始化
//如果没有默认构造函数就会报错
int _year;//内置类型
int _month;
int _day;
A a;//自定义类型
};
int main()
{
Date d1;//调用无参构造函数
return 0;
}
6. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。
// 默认构造函数
class Date
{
public:
//无参构造函数
Date()
{
_year = 1900;
_month = 1;
_day = 1;
}
//全缺省构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
- 关于编译器生成的默认成员函数,很多童鞋会有疑惑:在我们不实现构造函数的情况下,编译器会生成默认的构造函数。但是看起来默认构造函数又没什么用?d对象调用了编译器生成的默认构造函数,但是d对象year/month/_day,依旧是随机值。也就说在这里编译器生成的默认构造函数并没有什么用??
解答:C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语法已经定义好的类型:如int/char…,自定义类型就是我们使用class/struct/union自己定义的类型。
总结:C++我们不写构造函数,编译器默认生成构造函数,设计的不好,没有对内置类型和自定义类型统一做处理,编译器只是处理自定义类型,而不处理内置类型。
三、析构函数
3.1 概念
前面通过构造函数的学习,我们知道一个对象时怎么来的,那一个对象又是怎么没呢的?
析构函数:与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作
3.2 特性
析构函数是特殊的成员函数。
其特征如下:
- 析构函数名是在类名前加上字符 ~。
- 无参数无返回值。
- 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
- 对象生命周期结束时,C++编译系统系统自动调用析构函数。
比如:
但是这个例子析构函数没有对资源进行清理,我们就以栈为例:
#include <iostream>
#include <string.h>
using namespace std;
class Stack
{
public:
Stack(int capacity = 4)
{
_a = (int*)malloc(sizeof(int) * capacity);
if (nullptr == _a)
{
cout << "malloc fail\n" << endl;
exit(-1);
}
_top = capacity;
}
~Stack()
{
free(_a);
_a = nullptr;
_top = _capacity = 0;
}
private:
int* _a;
size_t _top;
size_t _capacity;
};
int main()
{
Stack s1;
Stack s2(20);
return 0;
}
调试程序:
5. 关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器生成的默认析构函数,对会自定类型成员调用它的析构函数,内置类型成员变量不处理。
#include <iostream>
#include <string.h>
using namespace std;
class String
{
public:
String(const char* str = "jack")
{
_str = (char*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
~String()
{
cout << "~String()" << endl;
free(_str);
}
private:
char* _str;
};
class Person
{
private:
String _name;
int _age;
};
int main()
{
Person p;
return 0;
}
四、拷贝构造函数
4.1 概念
在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。
那在创建对象时,可否创建一个与一个对象一某一样的新对象呢?
构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用
4.2特征
拷贝构造函数也是特殊的成员函数,其特征如下:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
例如:
这里为什么会报错呢?
调用拷贝构造函数,需要先传参数,传参又是一个拷贝构造。
调用拷贝构造函数,需要先传参数,传参又是一个拷贝构造。
传值传参就会发生无穷递归,我们可以引用传参。
这样就是实现了拷贝构造,但是最好加上const:
3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝
4. 那么编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,我们还需要自己实现吗?当然像日期类这样的类是没必要的。那么下面的类呢?验证一下试试?
这是什么原因呢?
所以s2再去调用析构函数,就会崩溃。
总结:
拷贝构造我们不写生成的默认构造拷贝函数对于内置类型和自定义类型都会做拷贝处理。但是处理细节是不一样的,这个跟构造和析构是不一样的。
五、赋值运算符重载
5.1 运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型或者枚举类型的操作数
- 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
- 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
- .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
以日期类举例:
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
默认情况下C++编译器是不支持自定义类型对象使用运算符。那么就简单设计一个运算符重载。
#include <iostream>
#include <string.h>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//private:
int _year;
int _month;
int _day;
};
//函数名:operator操作符
//返回值:看操作符运算后返回值是什么
//参数:操作数有几个操作数,就有几个参数
//全局的operator运算符,需要将Date里面的成员变量改为公有,但是破坏了封装性
bool operator>(const Date& d1, const Date &d2)
{
if (d1._year < d2._year)
{
return true;
}
else if (d1._year == d2._year && d1._month > d2._month)
{
return true;
}
else if (d1._year == d2._year && d1._month == d2._month && d1._day > d2._day)
{
return true;
}
else
{
return false;
}
}
int main()
{
Date d1(2022, 1, 18);
Date d2(2022, 1, 30);
cout << (d1 > d2) << endl;;
return 0;
}
这里会发现运算符重载成全局的就需要成员变量是共有的,那么问题来了,封装性如何保证?
所以我们设计成重载成成员函数:
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
bool operator>(const Date& d1, const Date& d2)
{
if (d1._year < d2._year)
{
return true;
}
else if (d1._year == d2._year && d1._month > d2._month)
{
return true;
}
else if (d1._year == d2._year && d1._month == d2._month && d1._day > d2._day)
{
return true;
}
else
{
return false;
}
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022, 1, 18);
Date d2(2022, 1, 30);
//cout << operator >(d1, d2) << endl;//不使用这种方式,麻烦
cout << (d1 > d2) << endl;;
return 0;
}
编译发现错误了,并且是参数过多,这是为什么呢?因为隐藏的this指针:
我们可以更改:
#include <iostream>
#include <string.h>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
bool operator>(const Date& d2)
{
if (this->_year < d2._year)
{
return true;
}
else if (this->_year == d2._year && this->_month > d2._month)
{
return true;
}
else if (this->_year == d2._year && this->_month == d2._month && this->_day > d2._day)
{
return true;
}
else
{
return false;
}
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022, 1, 18);
Date d2(2022, 1, 30);
cout << (d1 > d2) << endl;//常用
cout << d1.operator>(d2) << endl;//一般不使用这种方式
return 0;
}
5.2 赋值运算符重载
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//d1 = d2 赋值重载
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;
};
赋值运算符主要有四点:
- 参数类型
- 返回值
- 检测是否自己给自己赋值
- 返回*this
- 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。(编译器默认生成赋值重载,跟拷贝构造做的事情类似)
六、const成员
6.1 const修饰类的成员函数
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
看下面代码:
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void PrintDate()//Date& this
{
cout << "_year" << "_" << _month << "_" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
const Date d1;
d1.PrintDate();
return 0;
}
加上const
思考下面问题:
- const对象可以调用非const成员函数吗?
- 非const对象可以调用const成员函数吗?
- const成员函数内可以调用其它的非const成员函数吗?
- 非const成员函数内可以调用其它的const成员函数吗?
七、取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容