C++中类的6个默认函数

类的6个默认函数

1.构造函数

1.1概念

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

1.2特性

构造函数是特殊的成员函数,构造函数虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象
其特征如下:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载。
  5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
  6. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。
代码示例1:自动调用构造函数
class Student {
    
    
public:
	Student() {
    
    
		cout << "无参构造函数" <<this <<endl;
	}
	Student(int age, string name, string sex) {
    
    
		_age = age;
		_name = name;
		_sex = sex;
		cout << "带参构造函数" << this << endl;
	}
private:
	int _age;
	string _name;
	string _sex;
};
int main()
{
    
    
	Student s1; //调用无参构造函数
	Student s2(18,"zhangsan","男"); //调用带参构造函数
	
	//以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象。
	Student s3();
	 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明。
    return 0;
}
//运行结果:
无参构造函数0028F774
带参构造函数0028F730
代码示例2:默认构造函数
class Student {
    
    
public:
//如果用户显示定义了构造函数(不论是无参还是带参),编译器将不再生成
	Student(int age, string name, string sex) {
    
    
		_age = age;
		_name = name;
		_sex = sex;
		cout << "带参构造函数" << this << endl;
	}
private:
	int _age;
	string _name;
	string _sex;
};
int main()
{
    
    
	Student s1; //此时代码不会通过编译,“Student”: 没有合适的默认构造函数可用
	Student s2(18, "zhangsan", "男"); //可通过编译
	Student s3; 
    return 0;
}
//在注释掉显示定义的构造函数时,发现s3是可以成功创建的
代码示例3:默认构造函数的作用

C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语法已经定义好的类型:如 int/char…,自定义类型就是我们使用class/struct/union自己定义的类型,编译器生成默认的构造函数会对自定类型成员调用的它的默认成员函数。

class Person {
    
    
public:
	Person() {
    
    
		cout << "无参构造函数Person()" <<this <<endl;
	}
private:
	int age;
	string name;
};
class Student {
    
    
public:
	Student() {
    
    
		cout << "无参构造函数Student()" <<this <<endl;
	}
private:
	int _age;
	string _name; 
	string _sex;
	Person _p;
};
int main()
{
    
    
	Student s1;
    return 0;
}
//运行结果:
无参构造函数Person()00AFFBD4
无参构造函数Student()00AFFB98
1.3成员变量的定义风格
class Student {
    
    
public:
	Student(int age, string name, string sex) {
    
    
		age = age;
		name = name;   //这里的age,name,sex 到底是函数形参还是成员变量?
		sex = sex;
	}
//一般最好在定义的时候前面带上标识符
public:
	Student(int age, string name, string sex) {
    
    
	 	_age = age;
	 	_name = name;
	 	_sex = sex;
	}
};

2.析构函数

2.1概念

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

2.2 特性

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

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
  5. 析构函数调用与构造函数调用顺序相反,全局先于局部,静态先于普通对象。
  6. 如果类中涉及到资源管理时,该类必须要显式提供析构函数,否则会造成资源泄露。

代码示例:

class Person {
    
    
public:
	Person() {
    
    
		cout << "无参构造函数Person()" <<this <<endl;
	}
	~Person() {
    
    
		cout << "析构函数~Person()" << this << endl;
	}
private:
	int age;
	string name;
};
class Student {
    
    
public:
	Student() {
    
    
		cout << "无参构造函数Student()" <<this <<endl;
	}
	~Student() {
    
    
		cout << "析构函数~Student()" << this << endl;
	}
private:
	int _age;
	string _name;
	string _sex;
	Person p;
};
int main()
{
    
    
	Student s1;
   return 0;
}
//运行结果:
无参构造函数Person()00EFFB1C
无参构造函数Student()00EFFAE0
析构函数~Student()00EFFAE0
析构函数~Person()00EFFB1C

3.拷贝构造函数

3.1概念

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

3.2特征

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

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
  3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
  4. 如果类中涉及到资源管理时,该类必须要显式提供拷贝构造函数。默认构造函数执行完成后,两个对象共用一块堆内存空间。如果不显式提供,会造成多个对象共同使用同一份资源,在这对象销毁时,同一资源被释放多次而导致程序崩溃。

如果传值就会引发无穷递归,如图所示:
在这里插入图片描述
如果涉及到资源管理,不显式定义拷贝构造函数,多个对象共用同一份资源,如图所示:
在这里插入图片描述
显示定义拷贝构造函数后,如图所示:
在这里插入图片描述

代码示例:

class Student {
    
    
public:
	Student() {
    
    
		arr = (int*)malloc(sizeof(int) * 10);
		cout << "无参构造函数Student()" <<this <<endl;
	}
	Student(int age, string name, string sex) {
    
    
		_age = age;
		_name = name;
		_sex = sex;
		cout << "带参构造函数Student(int age, string name, string sex)" << this << endl;
	}
	~Student() {
    
    
		free(arr);
		cout << "析构函数~Student()" << this << endl;
	}
	Student(const Student &d) {
    
    
		cout << "拷贝构造函数Student(const Student& d)" << this << endl;
		_age = d._age;
		_name = d._name;
		_sex = d._sex;
	}
private:
	int _age;
	string _name;
	string _sex;
	int* arr;
};
int main()
{
    
    
	Student s1;
	Student s2(s1);
    return 0;
}
//运行结果:
无参构造函数Student()0135F84C  //构造s1
拷贝构造函数Student(const Student& d)0135F804 用s1拷贝构造s2
析构函数~Student()0135F804 //析构s2
析构函数~Student()0135F84C //析构s1

编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,如果不涉及到资源管理,所以我们自己没有必要实现。

4赋值运算符重载

4.1运算符重载

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

函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
一般定义形式:返回值类型 operator 运算符(const 类名& d) {
	 }
注意:
  1. 不能通过连接其他符号来创建新的操作符:比如operator@ 。
  2. 重载操作符必须有一个类类型或者枚举类型的操作数。
  3. 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义。
  4. 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参。
  5. .* 、::sizeof?:. 注意以上5个运算符不能重载。
    代码示例:
class Student {
    
    
public:
	Student() {
    
    
		arr = (int*)malloc(sizeof(int) * 10);
	}
	Student(int age , string name, string sex) {
    
    
		_age = age;
		_name = name;
		_sex = sex;
	}
	~Student() {
    
    
		free(arr);
	}
	Student(const Student &d) {
    
    
		_age = d._age;
		_name = d._name;
		_sex = d._sex;
	}
	//重载在类中
	bool operator==(const Student& d) {
    
    
		return _age == d._age
			&& _name == d._name
			&& _sex == d._sex;
	}
public:
	int _age;
	string _name;
	string _sex;
};
//重载在类外
bool operator==(const Student& d1, const Student& d2) {
    
    
	return d1._age == d2._age
	&& d1._name == d2._name
		&& d1._sex == d2._sex;
}
int main()
{
    
    
	Student s1;
	Student s2(18, "zhangsan", "男");
	Student s3(18, "zhangsan", "男");
	cout << (s1 == s2) << endl;
	cout << (s2 == s3) << endl;
    return 0;
}
运行结果: //C/C++bool类型返回值结果为真返回1,为假返回0
0
1
4.2赋值运算符重载
class Student {
    
    
public:

	Student(int age , string name, string sex) {
    
    
		_age = age;
		_name = name;
		_sex = sex;
	}
	//赋值运算符重载
	Student& operator=(const Student& d) {
    
    
		if (this != &d) {
    
    
			_age = d._age;
			_name = d._name;
			_sex = d._sex;
		}
		return *this;
	}
public:
	int _age;
	string _name;
	string _sex;
};
int main()
{
    
    
	Student s1;
	Student s2(18, "zhangsan", "男");
	Student s3;
	s3 = s2;
	cout << (s3 == s2) << endl;
    return 0;
}
运行结果:
1
注意:
  1. 参数类型
  2. 返回值
  3. 检测是否自己给自己赋值
  4. 返回*this
  5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
  6. 赋值运算符只能作为类的成员函数重载。
  7. 在区分前置++和后置++时,重载后置++时需要加一个 int 参数

如果不显示定义赋值运算符重载:

class Student {
    
    
public:

	Student(int age , string name, string sex) {
    
    
		_age = age;
		_name = name;
		_sex = sex;
	}
		//重载在类中
	bool operator==(const Student& d) {
    
    
		return _age == d._age
			&& _name == d._name
			&& _sex == d._sex;
	}
public:
	int _age;
	string _name;
	string _sex;
};
int main()
{
    
    
	Student s1;
	Student s2(18, "zhangsan", "男");
	Student s3;
	s3 = s2;
	cout << (s3 == s2) << endl;
    return 0;
}
运行结果:
1

一般重载类型:

Student &operator+=(int); 
Student &operator+(int); 
Student &operator-(int); 
Student &operator-=(int); 
//前置++
Student &operator++(); 
//前置--
Student &operator--(); 
//后置++
Student operator++(int); 
//后置--
Student operator--(int); 
// >运算符重载
bool operator>(const Student& d);
// <运算符重载
bool operator>(const Student& d);
// >= 运算符重载
bool operator>=(const Student& d);
// <=运算符重载
bool operator<=(const Student& d);
// !=运算符重载
bool operator!=(const Student& d);

5.const成员

5.1 const修饰类的成员函数

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

class Student{
    
    
	void Show() {
    
    
		cout << "非const成员函数" << endl;
	}
	void Show1()const{
    
    
		cout << "const成员函数" << endl;
	}
	void Show() const{
    
    
		cout << "const成员函数" << endl;
	}
public:
	int _age;
	string _name;
	string _sex;
};
int main()
{
    
    
	Student s1;
	s1.Show();
	s1.Show1();
	
	const Student s2;
	s2.Show();
	s2.Show1();
}
//运行结果:const成员函数
const成员函数

const成员函数
const成员函数

那么,可以总结 出以下几点:

  1. const对象不能调用非const成员函数。
  2. 非const对象可以调用const成员函数。
  3. const成员函数内不能调用其他的非const成员函数。
  4. 非const成员函数内可以调用其他的const成员函数。

6.取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Student
{
    
     
public :
 Date* operator&()
 {
    
    
 return this ;
 }
 
 const Date* operator&()const
 {
    
    
 return this ;
 }
private :
	int _age;
	string _name;
	string _sex;
};

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

7.小结

任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46078890/article/details/109264265