类与对象(中)

1.类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类,但是空类中并非什么都没有,任何类在什么都不写时,编译器会自动生成6个默认的成员函数,但是如果我们实现了,编译器就不会生成了。

默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
例如:

class className {};

在这里插入图片描述
6个默认的成员函数分别是:
构造函数,主要完成初始化工作
析构函数,主要完成清理工作
拷贝构造函数,是使用同类型的对象初始化创建对象
赋值运算符重载,主要把一个对象赋值给另一个对象
const成员函数
取地址及const取地址操作符重载

2.构造函数

2.1概念

对于一下的一个Date类

class Date{
public:
	/*Date(int year = 1,int month = 1,int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}*/
	Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}
	Date(int year ,int month ,int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{	
		cout << _year << '-' << _month << '-' << _day <<endl; 
	}
private:
	int _year;
	int _month;
	int _day; 
};
int main()
{
	Date d1;//无参的时候不需要加括号,否则就成了函数声明
	//对象实例化的时候必须调用构造函数,如果没有的话就会报错
	d1.Print();
	Date d2(2024,1,1);
	d2.Print();
	return 0;
}

引入构造函数之后,就不需要重新再调用Init函数去初始化对象,而是在创建对象的同时就将对象进行初始化了

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

2.2特性

构造函数是特殊的成员函数,构造函数虽然名为构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象。

其特征如下:
1.函数名与类名相同
2.无返回值
3.对象实例化时编译器就会自动调用对应的构造函数,如果没有对应的构造函数就会报错
4.构造函数可以重载(可以有多个构造函数)

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

class Date
{
public:
	/*Date(int year,int month,int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}*/
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl; 
	}
private:
	int _year;
	int _month;
	int _day; 
};
int main()
{
	//将Date类中的构造函数屏蔽之后,代码可以通过编译,因为编译器会自动生成一个无参的默认构造函数
	//将Date类中的构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成
	// error C2512: “Date”: 没有合适的默认构造函数可用
	Date d1;
	return 0;
}

6.默认生成的构造函数,对于内置类型不做处理,自定义类型会去调用它的默认构造
C++把类型分为内置类型和自定义类型,内置类型就是语言提供的数据类型,如:int char…等,自定义类型就是我们使用的class/struct/union等自己定义的类型

如果我们没有写任何一个构造函数,编译器才会自动生成,如果我们写了就不会生成了

class A
{
public:
	A()
	{
		cout << "A()" << endl;
		_a = 0;
	}
private:
	int _a;
}
class Date
{
public:
	void Print()
	{
		cout << _year << "-" << _month << _day << endl; 
	}
private:
	int _year;
	int _month;
	int _day;
	A _a; 
}
int main()
{
	Date d;//调用编译器自动生成的默认构造函数,对于内置类型不作处理,自定义类型去调用它的默认构造函数
	return 0;
}

C++11又对这个语法进行大补丁,对于内置类型在声明的位置给缺省值

class A
{
public:
	A()
	{
		cout << "A()" << endl;
		_a = 0;
	}
private:
	int _a = 1;
}
class Date
{
public:
	void Print()
	{
		cout << _year << "-" << _month << _day << endl; 
	}
private:
	int _year = 2024;
	int _month = 1;
	int _day = 1;//内置类型
	A _a; //自定义类型
}
int main()
{
	Date d;//调用编译器自动生成的默认构造函数,对于内置类型不作处理,自定义类型去调用它的默认构造函数
	return 0;
}

7.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认的构造函数只能有一个,否则就会出现歧义
注意默认的构造函数包括三种:1.无参的构造函数 2.全缺省的构造函数 3.我们没写编译器自动生成的无参的默认的构造函数
总结:不需要传参就可以调用构造函数,都可以叫做默认的构造函数

3.析构函数

3.1概念

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

3.2特性

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

typedef int DateType;
class Stack
{
public: 
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if(_array == NULL)
		{
			perror("malloc fail\n");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	void Push(DataType data)
	{
		_array[_size] = data;
		_size ++;
	}
	...
	~Stack()
	{
		if(_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DateType* _array;
	int _capacity;
	int _size;
};
int main()
{
	Stack s;
	s.Push(1);
	s.Push(2);
	return 0;
}

5.对于编译器自动生成的析构函数,对内置类型,销毁时不需要清理资源,最后系统直接将其内存回收即可
而对于自定义类型成员,要去调用它对应的析构函数

class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	int _year = 2024;
	int _month = 1;
	int _day = 1;//内置类型

	Time _t;//自定义类型
};
int main()
{
	Date d;//程序最终输出 ~Time()
	return 0;
}

6.如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date类;有额外资源申请时,一定要写,否则就会造成内存泄漏,比如Stack类

4拷贝构造函数

4.1概念

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

4.2特性

拷贝构造函数的特性如下:
1.拷贝构造函数是构造函数的一个重载形式
2.拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用

class Date {
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//拷贝构造函数
 //	Date(Date d)//调用拷贝构造函数,要先传参,这里传值传参,会形成一个新的拷贝构造,会引起无穷递归
	//{
	//	_year  = d._year;
	//	_day   = d._day;
	//	_month = d._month;
	//}
	//Date(const Date &d)//这里要加引用否则会导致无穷递归
	//{
	//	_year  = d._year;
	//	_day   = d._day;
	//	_month = d._month;
	//}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 1, 28);
	Date d2(d1);
	d1.Print();
	d2.Print();
	return 0;
}

3.若未显式定义,编译器会生成默认的拷贝构造函数。默认的拷贝构造函数,内置类型是按照字节序进行拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的

class Time{
public :
	~Time()
	{
		cout << "~Time()" << endl;
	}
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time(const Time& t)
	{
		_hour = t.hour;
		_minute = t.minute;
		_second = t.second;
		cout << "Time :: Time(const Time& t)" << endl;
	}
private:
	int _hour;
	int _minute;
	int second; 
}
class Date {
public:
	Date(int year = 1,int month = 1,int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//编译器会自动生成一个默认的拷贝构造函数,对内置类型进行按字节序拷贝,对于自定义类型会调用其拷贝构造函数
private:
	int _year = 2024;
	int _month = 9;
	int _day = 5;
	
	Time _t;
};

int main()
{
	Date d1(2024,9,9);
	Date d2(d1);//这里会调用自定义类型Time的拷贝构造函数
	d1.print();
	d2.print();
	return 0;
}

4.类中如果没有涉及资源申请时,拷贝构造是否写都可以;但是一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝

typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (_array == nullptr)
		{
			perror("malloc fail");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_size = 0;
			_capacity = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);//s1对象调用构造函数创建,在构造函数中,默认申请了10个元素的空间,然后里面存了4个元素1,2,3,4

	Stack s2(s1);
	//s2对象使用s1拷贝构造,而Stack类中没有显式定义拷贝构造,则编译器会给Stack类生成一份默认的拷贝构造函数,默认的拷贝构造函数是按照值进行
	//拷贝,即将s1中的内容原封不动的拷贝到s2中,因此s1和s2指向同一块空间
	//因此当程序退出时,s2和s1要销毁,s2先销毁(先构造的后销毁,后构造的先销毁),s2销毁调用析构函数,已经将申请的空间释放了,但是s1不知道,
	//到s1销毁时,会将原来它们指向的同一块空间再释放一次,一块空间被释放多次,肯定会造成程序崩溃
	return 0;
}

如下图所示:
在这里插入图片描述

5.拷贝构造函数典型调用场景

使用已存在对象创建新对象
函数参数类型为类类型对象
函数返回值类型为类类型对象

class Date
{
public:
	Date(int year, int minute, int day)
	{
		cout << "Date(int,int,int)" << endl;
	}
	Date(const Date& d)
	{
		cout << "Date(const Date& d)" << endl;
	}
	~Date()
	{
		cout << "~Date()" << this << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

Date Test(Date d)//以值的方式进行传递,传参的时候调用拷贝构造函数创建d
{
	Date temp(d);//调用拷贝构造函数创建temp
	return temp;//函数以值的方式返回,返回时使用temp拷贝构造临时对象来返回
}
int main()
{
	Date d1(2024, 9, 5);//调用构造函数创建d1
	Test(d1);
	return 0;
}

为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用

5.赋值运算符重载

5.1运算符重载

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

函数名字为:关键字operator后面接需要重载的运算符符号
函数原型:返回值类型 operator操作符(参数列表)

注意:
不能通过连接其他符号来创建新的操作符:比如operator@
重载运算符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置类型+,不能改变其含义
作为类成员函数重载时,其形参看起来比操作数目少一,因为成员函数的第一个参数为隐藏的this

.* :: sizeof ?: .
注意以上5个运算符不能重载

class Date
{
public:
	Date(int year = 2000,int month = 1,int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
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;
}
int main()
{
	Date d1(2024,1,1);
	Date d2(2024,2,1);
	//cout << operator==(d1,d2) << endl;
	cout << (d1 == d2) << endl;//这里要加括号,因为流插入的优先级更高
	return 0;
}
class Date
{
public:
	Date(int year = 2000,int month = 1,int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//bool operator==(Date* this,const Date& d2)
	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(2024,1,1);
	Date d2(2024,1,2);
	cout << d1.operator==(d2) << endl;
	//cout << (d1 == d2) << endl;意义同上
	return 0;	
}

5.2 赋值运算符重载

1.赋值运算符重载格式:
参数类型:const T&,传递引用可以提高传参效率
返回值类型:T& ,引用返回可以提高返回的效率,有返回值的目的是为了支持连续赋值
检查是否给自己赋值
返回*this:要符合连续赋值的含义

class Date
{
public: 
	Date(int year = 2024, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)
	{
		_year = d._day;
		_month = d._month;
		_day = d._day;
	}
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2024, 1, 1);
	Date d2(2024, 1, 2);
	d2 = d1;
	d1.Print();
	d2.Print();
	return 0;
}

2.赋值运算符只能重载成类的成员函数不能重载成全局函数
赋值运算符是六个默认成员函数之一,如果不显示实现,编译器会生成一个默认的赋值运算符重载,此时如果用户在类外自己实现一个全局的赋值运算符重载,就和编译器在类中,生成的默认的赋值运算符重复了,所以赋值运算符重载只能是类的成员函数

3.用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。
注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值

class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time& operator=(const Time& t)
	{
		if(this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			_second = t._second;
		}
		return *this;
	}
private:
	int _hour;
	int _minute;
	int _second;	
}
class Date
{
private:
	int _year = 2024;
	int _month = 1;
	int _day = 1;
	Time _t;
};

int main()
{
	Date d1(2024, 1, 1);
	Date d2(2024, 1, 2);
	d2 = d1;
	d1.Print();
	d2.Print();
	return 0;
}

4.如果类中未涉及到资源管理(可以理解为资源申请),赋值运算符是否实现都可以;一旦涉及到了资源申请必须要实现

typedef int DataType;
class Stack
{
public:
 	Stack(size_t capacity = 10)
 	{
		 _array = (DataType*)malloc(capacity * sizeof(DataType));
		 if (nullptr == _array)
		 {
			 perror("malloc申请空间失败");
			 return;
		 }
		 _size = 0;
		 _capacity = capacity;
	 }
	 void Push(const DataType& data)
	 {
		 // CheckCapacity();
		 _array[_size] = data;
		 _size++;
	 }
	 ~Stack()
	 {
		 if (_array)
		 {
			 free(_array);
			 _array = nullptr;
			 _capacity = 0;
			 _size = 0;
		 }
	 }
private:
 DataType *_array;
 size_t _size;
 size_t _capacity;
};
int main()
{
 Stack s1;
 s1.Push(1);
 s1.Push(2);
 s1.Push(3);
 s1.Push(4);
 //s1对象调用构造函数创建,在构造函数中,默认申请了10个元素的空间,然后存了4个元素1 2 3 4
 Stack s2;
 //s2对象也是同样调用构造函数,申请了10个元素的空间,没有存元素
 s2 = s1;
 /*由于Stack没有显式实现赋值运算符重载,编译器会以浅拷贝的方式实现一份默认的赋值运算符重载,即只要Stack的对象之间互相赋值,就会将一个对象中的内容原封不动的拷贝给另一个对象*/
 //s2 = s1	当s1给s2赋值时,编译器会将s1中的内容原封不动的拷贝到s2中,这样就会导致两个问题:
 //a. s2原来的空间丢失了,就会导致内存泄漏问题
 //b. s1和s2共享同一块空间,最后销毁时,会导致同一块空间释放两次从而引起程序崩溃
 return 0;
}

7.const成员

将const修饰的成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类中的任何成员进行修改
总结:
成员函数,如果只对成员变量进行读访问的函数,建议加上const,这样对const对象进行保护,防止其被修改以及权限放大
如果是一个对成员变量要进行读写访问的函数,不建议加上const,否则不能修改成员变量

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

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

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

int main()
{
	A a1;
	const A a2;
	cout << (&a1) << endl;
	cout <<  (&a2) << endl;
	return 0;
}

这两个运算符不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容(即当我们不想让别人获取对应的地址时可以重载)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值