【c++师傅领进门,修行靠个人】第四篇:你所不知道的那些默认成员函数

1 类里还有这6个默认成员函数

在这里插入图片描述

1.1 构造函数

class Data
{
    
    
public:
	void SetDate(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()
{
    
    
	Data d;
	d.SetDate(2021, 6, 13);
	d.Display();

	return 0;
}

在这里插入图片描述
对于Date类,可以通过SetDate公有的方法给对象设置内容,但是如果每次创建对象都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有
一个合适的初始值,并且在对象的生命周期内只调用一次。

构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象。值得一提的是,构造函数也可以用缺全缺省的办法进行初始化,这会更方便。

	// 3.带参全缺省构造函数
	Date(int year = 0, int month = 1, int day = 1)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}

在这里插入图片描述

其特征如下:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载
class Date
{
    
    
public:
	// 1.无参构造函数
	Date()
	{
    
    }

	// 2.带参构造函数
	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;
	Date d2(2021,3,4);
	d1.Display();
	d2.Display();

	return 0;
}

在这里插入图片描述
成员变量的命名风格

// 我们看看这个函数,是不是很僵硬?
class Date
{
    
    
public:
	Date(int year)
	{
    
    
		// 这里的year到底是成员变量,还是函数形参?
		year = year;
	}
private:
	int year;
};
// 所以我们一般都建议这样
class Date
{
    
    
public:
	Date(int year)
	{
    
    
		_year = year;
	}
private:
	int _year;
};

1.2 析构函数

析构函数是特殊的成员函数。
其特征如下:

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。

关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器生成的默认
析构函数,对会自定类型成员调用它的析构函数。

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;
}

1.3 拷贝构造函数

在创建对象时,可否创建一个与一个对象一某一样的新对象呢?

在这里插入图片描述

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

拷贝构造函数也是特殊的成员函数,其特征如下:

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
	Date(const Date& d)
	{
    
    
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

注意这里要传引用调用,而不能传值,否则会一直递归循环
在这里插入图片描述

int main()
{
    
    
	Date d1(2021,3,4);
	Date d2(d1);
	d1.Display();
	d2.Display();

	return 0;
}

在这里插入图片描述

1.4 赋值运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。函数名字为:关键字operator后面接需要重载的运算符符号。函数原型:返回值类型 operator操作符(参数列表)


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

注意:

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型或者枚举类型的操作数
  • 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
  • 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
  • .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现
// 全局的operator==
class Date
{
    
    
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};

bool operator==(const Date& d)
{
    
    
	return _year == d._year
	    && _month == d._month
		&& _day == d._day;
}
void Test()
{
    
    
	Date d1(2018, 9, 26);
	Date d2(2018, 9, 27);
	cout << (d1 == d2) << endl;
}

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

赋值运算符重载

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

赋值运算符主要有四点:

  1. 参数类型
  2. 返回值
  3. 检测是否自己给自己赋值
  4. 返回*this
  5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。

1.5 取地址及const取地址操作符重载

const修饰类的成员函数
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
在这里插入图片描述

class Date
{
    
    
public:
	Date* operator&()
	{
    
    
		return this;
	}

	const Date* operator&()const
	{
    
    
		return this;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容!

在这里插入图片描述

好了,本文就总结到这。各位看官如果觉得文章写的可以,不妨点个赞再走,如果想一起交流学习经验,不妨点个关注。

猜你喜欢

转载自blog.csdn.net/weixin_42907822/article/details/117873938
今日推荐