类的对象中(构造函数和析构函数)

类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数
图片
在这里插入图片描述

1.构造函数

以下面代码举例

#include<iostream>
using namespace std;

class Date
{
public:
void Init(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 a1;
	a1.Print();
	return 0;
}

上面代码是假如我们忘了初始化,打印了出来一堆乱码,那么写了c++那些大佬是怎么解决这个问题的吗,这就要讲到构造函数了,有了构造函数就可以不要初始化函数了

1.构造函数的特性

讲构造函数的时候先讲讲它的特性
构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象。
以之前的日期代码为例我们来使用下构造函数
代码

#include<iostream>
using namespace std;

class Date
{
public:
//构建函数
	Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};

int main()
{
	Date a1;
	a1.Print();
	return 0;
}

运行一下
在这里插入图片描述
通过上面使用可以看到下面三点

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。

如果我想要传些指定的值过去呢?这个时候就到第4点了支持函数重载
代码

#include<iostream>
using namespace std;

class Date
{
public:
	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 a1;
	Date a2(100, 100, 100);
	a2.Print();
	a1.Print();
	return 0;
}

运行结果
在这里插入图片描述
不过这里要注意的是构造函数和普通的函数不一样,通常调用的时候是对象调用类,而构造函数就反了过来

构造函数是支持半缺省和全缺省的
看看下面代码能不能运行

#include<iostream>
using namespace std;


class Date
{
public:
	Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}


	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; // 年
	int _month; // 月
	int _day; // 日
};

int main()
{
	//Date d1;
	return 0;
}

运行结果
在这里插入图片描述
可以看到是没有问题的,因为这里是没调用这个类,但是一旦用了这个类就报错了
看下面截图
在这里插入图片描述
为什么不能,因为编译器不知道调用那个函数
但是这种在语法方面可以同时存在,但是使用的时候就不行
所以通常构造函数只用这种

	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

这种有默认值,有指定值

2.构造函数的坑

如果类中显式没有定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成
图片
在这里插入图片描述
可以看到打印出来了随机值
但是可以感觉到这个默认生成的构造函数没有任何作用
我们再来了解一下
如果我们不写构造函数,编译器会生成一个默认无参构造函数,之前讲过了
c++分为二种类型
1.是内置类型/基本类型:int/char/double/指针…
2.自定义类型:class/struct去定义类型对象
默认生成构造函数对于内置类型成员变量不做处理,对于自定义类型成员变量才会处理
很奇怪吧,我们用调试来证明一下
代码

#include<iostream>
using namespace std;


class A
{
public:
	A()
	{
		cout <<"_a" << _a << endl;
		_a = 0;
	}
private:
	int _a;
};

class Date
{
public:
// 我们不写,编译器会生成一个默认无参构造函数
// 内置类型/基本类型:int/char/double/指针...
// 自定义类型:class/struct去定义类型对象
// 默认生成构造函数对于内置类型成员变量不做处理,对于自定义类型成员变量才会处理
void Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
A _aa;
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};

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

调试图片
在这里插入图片描述
那么问题来了,什么时候需要使用这个函数

总结:如果一个类中的成员全是自定义类型,我们就可以使用默认生成的函数。如果有内置类型或者需要传参初始化的时候,我们就要自己构建构造函数
默认构造函数的意思是不用传参就可以调用的就叫,默认构造函数

要使用的例子
这个例子是想用栈模拟队列

#include<iostream>
using namespace std;


class stack
{
public:
    stack()
    {
        int* _a = nullptr;
        _top = _capacity = 0;
    }
private:
    	int* _a;
    	int _top;
    	int _capacity;
};


class MyQueue
{
public:
    MyQueue() 
    {

    }

    void push(int x) 
    {

    }

    int pop() 
    {

    }

   private:
    stack s1;
    stack s2; 
};

int main()
{
    MyQueue q;
    
    return 0;
}

编译截图
在这里插入图片描述
这串代码还有一个问题,如果不用它的默认无参构造函数,我们是无法完成初始化的,因为我们对栈初始化的话。我们会发现他里面的变量都是私有的

那么什么条件会构成默认构造函数

1.我们不写默认生成的
2.我们写的无参
3.我们写的全缺省

像下面代码就不会构成默认构造函数
在这里插入图片描述

因为这个不符合上面三个条件
按上面来讲这里必须是全自定义参数才有默认构造函数
那么想要初始化_size怎么办
在这里插入图片描述
在这里插入图片描述
这不就有点像C语言的初始化函数?

2.析构函数

1.析构函数的概念

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

2.析构函数的特性

其特征如下:

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
    析构函数有点像C语言的销毁函数
    代码
#include<iostream>
using namespace std;


class stack
{
public:
    stack(int capacity =10)
    {
         _a = (int*)malloc(sizeof(int)*capacity);
         _top = 0;
         _capacity = capacity;
    }
    ~stack()
        {
        
        		free(_a);
        		_a = nullptr;
        		_top = _capacity = 0;
        }
private:
    	int* _a;
    	int _top;
    	int _capacity;
};


class MyQueue
{
public:
    MyQueue() 
    {

    }

    void push(int x) 
    {

    }

    int pop() 
    {

    }

 private:
     // C++11打的补丁,针对编译器自己生成默认成员函数不初始化的问题
     // 给的缺省值,编译器自己生成默认构造函数用
     int _size=0;
    stack s1;
    stack s2; 
};

int main()
{
    MyQueue q;
    return 0;
}

析构函数是为了完成资源清理工作。所以要把stack回收一下资源

3.什么时候调用析构函数

调用析构函数是出了函数作用域的时候就调用
在这里插入图片描述
出了花括号就调用了析构函数,s那里显示是灰色代表的是被释放的空间

在栈里面的析构函数是后进先析构,和构造相反

#include<iostream>
using namespace std;


class stack
{
public:
    stack(int capacity =10)
    {
         _a = (int*)malloc(sizeof(int)*capacity);
         _top = 0;
         _capacity = capacity;
    }
    ~stack()
        {
        
        		free(_a);
        		_a = nullptr;
        		_top = _capacity = 0;
        }
private:
    	int* _a;
    	int _top;
    	int _capacity;
};


class MyQueue
{
public:
    MyQueue() 
    {

    }

    void push(int x) 
    {

    }

    int pop() 
    {

    }

 private:
     // C++11打的补丁,针对编译器自己生成默认成员函数不初始化的问题
     // 给的缺省值,编译器自己生成默认构造函数用
     int _size=0;
    stack s1;
    stack s2; 
};

int main()
{
    MyQueue q;
    int a = 1;
    stack st1(1);
    stack st2(2);


    return 0;
}

在这里调试发现析构的_capacity是2
所以后进先析构
在这里插入图片描述
它和构造函数一样,默认生成析构函数对于内置类型成员变量不做处理,对于自定义类型成员变量才会处理

证明
代码

#include<iostream>
using namespace std;


class stack
{
public:
    stack(int capacity =10)
    {
         _a = (int*)malloc(sizeof(int)*capacity);
         _top = 0;
         _capacity = capacity;
    }
    ~stack()
        {
             cout << "~stack:" << this << endl;
        		free(_a);
        		_a = nullptr;
        		_top = _capacity = 0;
        }
private:
    	int* _a;
    	int _top;
    	int _capacity;
};


class MyQueue
{
public:
    MyQueue() 
    {

    }

    void push(int x) 
    {

    }

    int pop() 
    {

    }

 private:
    stack s1;
    stack s2; 
};

int main()
{
    stack st1(1);
    stack st2(2);
    MyQueue q;

   return 0;
}

打印结果
在这里插入图片描述
可以看到了打印了四个地址,这说明了,MyQueue q;这串代码默认调用了二次析构函数
再来一个坑,看代码
下面代码会怎么样,能正常编过吗?

#include<iostream>
using namespace std;


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

private:
	int year;
	int _month;
	int _day;
};


int main()
{
	Date d1(2022,5,18);
	return 0;
}

运行截图
能正常编过
在这里插入图片描述
但是为什么这个year是随机值呢?
明明year传过去了,他这个调用是那个year
因为有this指针所以会替换成

	Date(int year, int month, int day)
	{
		year = year;
		this->_month = month;
		this->_day = day;
	}

而year没有替换是因为就近原则,他都找到了,所以就没有调用this指针
那么我要是就像这样子想要名字相同year怎么做

	Date(int year, int month, int day)
	{
		this->year = year;
		this->_month = month;
		this->_day = day;
	}

运行结果
在这里插入图片描述

这样子就没问题,不过推荐还是要把名字区分开,要不然一直写this->很浪费时间,有点本末倒置的意思了

3.拷贝构造函数

1. 拷贝函数的概念

就像复制粘贴的感觉,比如把test1这个文件的内容粘贴到test2里面

2.拷贝函数的特性

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

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

解释为什么会传值会无限递归调用
代码

#include<iostream>
using namespace std;


class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date( Date d)
	{
	    _year = year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(d1);
	return 0;
}

运行截图
在这里插入图片描述
虽然死递归被编译器发现了所以直接报错了

其实就是也很简单,传参就要拷贝构造,拷贝构造又要传参这样死递归下去

#include<iostream>
using namespace std;


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

	Date(const Date& d)
	{
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};

void func(Date d)
{
	;
}

int main()
{
	Date d1;
	Date d2(d1);
	func(d1);
	return 0;
}

在c语言func()是实参传给形参来进行运行的
在c++里面是在同一个类型里面是要调用拷贝函数的
调试,我在调用func函数的时候他先调用拷贝函数在调用func
在这里插入图片描述
那么使用不想func但是不想调用拷贝函数,那就在func(Date& d)加个引用
那么调用fuc的时候就不会调用拷贝函数了
拷贝构造函数尽量加上const,因为不加上const可能有其他因为粗心而编译报错的问题,比如写反了被拷贝的值。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值