类的6个默认成员函数

8 篇文章 0 订阅
6 篇文章 0 订阅


一、构造函数

1.概念

构造函数主要完成初始化的工作,它是一个特殊的成员函数,名字和类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有一个合适的初始值,并且在对象的生命周期内只调用一次(类似人的出生)。

2.特性

构造函数时一个特殊的成员函数,它的主要任务并不是开空间创建对象,而是初始化对象。
特征如下:
1.函数名与类名相同
2.无返回值
3.对象实例化时编译器自动调用对应的构造函数
4.构造函数可以重载

class Date
{
public:
	Date(){};//无参构造函数
	Date(int year, int month, int day)//带参构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.display();
	return 0;
}

5.如果类中没有显示定义构造函数,则c++编译器会自动生成一个无参的默认构造函数,一旦用户显示定义编译器将不再生成。

6.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。

class Date
{
public:
	Date(){};//无参构造函数
	Date(int year=2021, int month=7, int day=26)//带参构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.display();
	return 0;
}

t在这里插入图片描述则会报错默认构造函数多个

二、析构函数

1.概念

析构函数与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的,而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。

2.特性

特征如下:
1.析构函数名在类名前加上符号~
2.无参数无返回值
3.一个类仅有一个析构函数,若未显示定义,系统会自动生成默认的析构函数
4.对象生命周期结束时,c++编译系统自动调用析构函数。

#include<malloc.h>
class String
{
public:
	String(const char* str = "jim")
	{
		cout << "String()" << endl;
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
		cout << _str << endl;
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};

void Test()
{
	String s;
}

int main()
{
	Test();
	return 0;
}

三、拷贝构造函数

1.概念

拷贝构造函数只有单个形参,该形参时对本类类型对象的引用(一般常用const修饰),在已存在的类类型对象创建新对象时由编译器自动调用

2.特征

1.拷贝构造函数是构造函数的一个重载形式
2.拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)//参数只有一个,必须使用引用传参,无返回值
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2021, 7, 27);
	Date d2(d1);
	return 0;
}

在这里插入图片描述
3.若未显示定义,系统生成默认的拷贝构造函数。默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//Date(const Date& d)//参数只有一个,必须使用引用传参,无返回值
	//{
	//	_year = d._year;
	//	_month = d._month;
	//	_day = d._day;
	//}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2021, 7, 27);
	Date d2(d1);
	return 0;
}

在这里插入图片描述
4.编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,但是我们仍然需要自己实现。

#include<iostream>
using namespace std;
class String
{
public:
	String(const char* str = "wak")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};

int main()
{
	String s1("hello");
	String s2(s1);
	return 0;
}

在调试时会发生错误
在这里插入图片描述
这是因为在拷贝的时候,s1拷贝s2,指向同一块内存,所有在释放内存的时候会发生两次析构,第二次析构已经释放的空间,所以会报错
在这里插入图片描述

注:
如果一个类中未涉及到资源管理时,拷贝函数是否提供都可以
如果一个类中涉及到资源管理时,拷贝函数是否提供可能出现问题

四、赋值运算符重载

1.运算符重载

c++为了增强代码的可读性引入了运算符重载,运算符重载时具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号

函数原型:返回值类型 operator操作符(参数列表)

注:
1.不能通过连接其他符号来创建新的操作符:比如operator@
在这里插入图片描述
2.重载操作符必须有一个类类型或者枚举类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如内置的整型+,不能改变其含义
4.作为类成员的重载函数时,其形参看起来比操作数数目少1个,因为有一个默认的形参this,限定为第一个形参。


#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	bool operator==(const Date& d2)
	{
		return _year == d2._year&&
			_month == d2._month&&
			_day == d2._day;
	}

private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1(2021, 7, 27);
	Date d2(2021, 7, 27);
	cout << (d1 == d2) << endl;
	return 0;
}

5.".*"、"::"、“sizeof”、"?:"、".",注意以上五个运算符不能重载。

2.赋值运算符重载

返回类型 operator=(参数类型)
注:
1.参数类型
2.返回值
3.检测是否自己给自己赋值
4.返回*this
5.一个类如果没有显示定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝(浅拷贝,拷贝构造函数说到过)

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d2)
	{
		_year = d2._year;
		_month = d2._month;
		_day = d2._day;
	}
	Date& operator=(const Date& d2)
	{
		if (this != &d2)
		{
			_year = d2._year;
			_month = d2._month;
			_day = d2._day;
		}
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2021, 7, 21);
	Date d2(2021,8,21);
	d2 = d1;
	return 0;
}

编译器生成的默认赋值重载函数已经可以完成字节序的值拷贝了,我们仍然需要自己实现。
否则会出现问题

#include<iostream>
#include<malloc.h>
using namespace std;

class String
{
public:
	String(const char* str = "")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		free(_str);
	}
private:
	char* _str;
};
int main()
{
	
	String s1("hello");
	String s2("world");
	s1 = s2;
	return 0;
}

在这里插入图片描述

在这里插入图片描述
在析构的时候连续两次释放同一空间,所以会发生中断。

五、取地址及const取地址操作符重载

1.取地址操作符重载

#include<iostream>

using namespace std;

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date* operator&()
	{
		cout << this << endl;
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1(2021, 7, 28);
	Date* pd = &d1;
	return 0;
}

在赋值地址的时候打印地址。

2.const取地址操作符重载

1.const修饰类的成员函数
将const修饰的类成员函数称为const成员函数,本质上修饰的是函数隐藏的this指针,表明在该成员函数中不能对类的任何成员进行修改。(只能读不能写)
格式:返回值类型 函数名(参数列表)const
如下:


	Date& operator=(Date& d)const
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}

此代码编译不通过,因为改变了this指向的值

思考:1.const对象可以调用非const成员函数吗?(不可以)

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void TestFunc1()
	{
		
	}
	void Print()const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	const Date d1(2021, 7, 28);
	d1.TestFunc1();
	return 0;
}

在这里插入图片描述

2.非const对象可以调用const成员函数吗?(可以)
3.const成员函数内可以调用其它的非const成员函数吗?(不可以)

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void TestFunc1()
	{
		
	}
	void Print()const
	{
		TestFunc1();
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

在这里插入图片描述
4.非const成员函数内可以调用其它的const成员函数吗?(可以)
注:
const成员函数是只可读不可写
普通成员函数即可读又可写
在普通成员函数调用const成员函数,相当于大权限函数调用小权限函数,是可以的。

2.const取地址操作符重载

const Date* operator&()const
	{
		return this;
	}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值