2021-04-17

1.构造函数

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
特性:构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象。
特征:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载。
//如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
//无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数
class Date{
public:
	//无参构造函数,可以不用写,系统自动生成
	Date(){
	}
	//带参构造函数
	Date(int year,int month,int day){
	_year=year;
	_month=month;
	_day=day;
	cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
void test (){
	Date d1(1949, 1, 1);
	Date d2;
	Date d3();//无参构造函数创建对象时不要带(),否则就成了函数声明,不再是创建对象
}
int main()
{
	test();
	system("pause");
	return 0;
}
//C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语法已经定义好的类型:如int / char...,自定义类型就是我们使用class / //struct / union自己定义的类型,看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数
class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour   ;
		_minute = 0;
		_second = 0;
		cout << _hour;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	// 基本类型(内置类型)
	int _year;
	int _month;
	int _day;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	system("pause");
	return 0;
}

2.析构函数

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

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
//析构函数
class seqlist{
public:
	seqlist(int  capacity = 10)
	{
		_data = (int*)malloc(capacity * sizeof(int));
		cout << _data << endl;
		assert(_data);
		_size = 0;
		_capacity = capacity;
	}
	~seqlist(){
		if (_data)
		{
			free(_data);
			_data = nullptr;
			_size = 0;
			_capacity = 0;
		}
	}
private:
	int* _data;
	size_t _size;
	size_t _capacity;
};
int main(){
	seqlist d1(10);
	system("pause");
	return 0;
}
//译器生成的默认析构函数,对会自定类型成员调用它的析构函数
class string1{
public:
	string1(const char* str="theshy"){
		_str = (char*)malloc(strlen(str)+1);
		strcpy(_str,str);
	}
	~string1(){
		cout << "~string1()" << endl;
		free(_str);
	}
private:
	char* _str;
};
class Person
{
private:
	string1 _name;
	int _age;
};
int main(){
    Person d1;
	system("pause");
	return 0;
}

3.拷贝构造

是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:
通过使用另一个同类型的对象来初始化新创建的对象。
复制对象把它作为参数传递给函数。
复制对象,并从函数返回这个对象。
特征:

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
//拷贝构造
class Date{
public:
	Date(){
		 _year = 1998;
		 _month = 1;
		 _day = 1;
		cout << _year << "-" << _month << "-" << _day<<endl;
	}
	Date(const Date &d){
		_year = d._year;
		_month = d._month;
		_day =  d._day;
		cout << _year << "-" << _month << "-" << _day<<endl;
	};
private:
	int _year;
	int _month;
	int _day;
};
int main(){
	Date d1;
	Date d2(d1);
	system("pause");
	return 0;
}

4.赋值重载

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

//赋值运算符重载
class Date{
public:
	Date(int year=1900,int month=1,int day=1){
		_year = year;
		_month = month;
		_day = day;
	}
	Date&operator=(const Date &d2){     //Date &operater用了&可以减少拷贝构造
		if (this != &d2){
			_year = d2._year;
			_month = d2._month;
			_day = d2._day;
		}
		return *this;
	}
	void fun(){
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main(){
	Date d1;
	d1.fun();
	Date d2(1900,6,6);
	d2.fun();
	d2 = d1;
	d2.fun();
	system("pause");
	return 0;
}

赋值运算符就这几点:

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

5.取地址操作符重载

//基本不用,编译器自动生成
const Date * operator&()  const  //这里的const修饰的是默认参数this
	{  
        	return this;  
	} 

6.const修饰的取地址操作符重载

//基本不用,编译器自动生成
	Date * operator&()    
	{  
        return this;  
	} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值