C++类和对象——构造函数与解析函数介绍

目录

1.构造函数和析构函数

1.构造函数,进行初始化

2.析构函数,进行清理

2.构造函数的分类及调用

1.括号法

注意:

2.显示法

3.隐式转化法 

匿名对象

3.拷贝构造函数调用时机

4.构造函数调用规则

1.定义有参构造函数,不提供默认无参构造,会默认提供拷贝构造 

2.定义拷贝构造函数,不会提供其他构造函数


1.构造函数和析构函数

如果我们不写,编译器提供的是空实现 ,即这两种函数存在,但里面什么都没写

1.构造函数,进行初始化

下为代码演示:

 

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:
	person(){
		cout << "调用了一次";
	}
};

void test(){
	person p;
	//会自动调用一次构造函数
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	test();
	return 0;
}

 

2.析构函数,进行清理

 

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:
	person(){
		cout << "调用了一次" << '\n';
	}
	~person(){
		cout << "析构函数调用" << endl;
	}
};

void test(){
	person p;
	//会自动调用一次构造函数
	//这是栈上的数据,执行完后会自动释放,在释放前就会自动调用一次析构函数
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	test();
	
	return 0;
}

 

2.构造函数的分类及调用

 

无参构造又叫默认构造 

代码示例:

1.括号法

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:

	int age;
	
	person(){
		cout << "无参构造函数调用" << '\n';
	}
	
	person(int a){
		age = a;
		cout << "有参构造函数调用" << '\n';
	}
	//拷贝构造函数
	person(const person &p){
		//将传入的拷贝到我身上
		age = p.age;
		cout << "拷贝构造函数调用" << '\n';
	}
	~person(){
		cout << "析构函数调用" << endl;
	}
};

void test(){
	//1.括号法
	person p1;
	person p2(10);
	person p3(p2);
	//2.显示法
	//3.隐式转化法
	
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	test();
	
	return 0;
}

 

注意:

1.调用默认函数构造不能

person p()

因为这段话编译器会认为是函数的声明 

2.调用拷贝函数不能person(p3)

编译器会认为这个括号不存在,以为这是一个变量的声明

2.显示法

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:

	int age;
	
	person(){
		cout << "无参构造函数调用" << '\n';
	}
	
	person(int a){
		age = a;
		cout << "有参构造函数调用" << '\n';
	}
	//拷贝构造函数
	person(const person &p){
		//将传入的拷贝到我身上
		age = p.age;
		cout << "拷贝构造函数调用" << '\n';
	}
	~person(){
		cout << "析构函数调用" << endl;
	}
};

void test(){
	//1.括号法
	// person p1;
	// person p2(10);
	// person p3(p2);
	
	//2.显示法
	person p4;
	person p5 = person(10);
	person p6 = person(p5);
	//3.隐式转化法
	
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	test();
	
	return 0;
}

 

3.隐式转化法 

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:

	int age;
	
	person(){
		cout << "无参构造函数调用" << '\n';
	}
	
	person(int a){
		age = a;
		cout << "有参构造函数调用" << '\n';
	}
	//拷贝构造函数
	person(const person &p){
		//将传入的拷贝到我身上
		age = p.age;
		cout << "拷贝构造函数调用" << '\n';
	}
	~person(){
		cout << "析构函数调用" << endl;
	}
};

void test(){
	//1.括号法
	// person p1;
	// person p2(10);
	// person p3(p2);
	
	//2.显示法
	// person p4;
	// person p5 = person(10);
	// person p6 = person(p5);
	
	//3.隐式转化法
	person p4 = 10;//相当于 person p4(10)或者person p4 = person(10)
	person p5 = p4;//拷贝构造
	
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	test();
	
	return 0;
}

 

匿名对象

 

3.拷贝构造函数调用时机

 

 

 

 

 

4.构造函数调用规则

 

 

1.定义有参构造函数,不提供默认无参构造,会默认提供拷贝构造 

如下代码,调用了无参构造函数,结果报错了

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:

	int age;
	
	// person(){
		// cout << "无参构造函数调用" << '\n';
	// }
	
	person(int a){
		age = a;
		cout << "有参构造函数调用" << '\n';
	}
	//拷贝构造函数
	person(const person &p){
		age = p.age;
		cout << "拷贝构造函数调用" << '\n';
	}
	~person(){
		cout << "析构函数调用" << endl;
	}
};

void test(){
	person p;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	test();
	
	return 0;
}

2.定义拷贝构造函数,不会提供其他构造函数

这里调用了无参构造函数,结果报错了

 

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

class person
{
public:

	int age;
	
	// person(){
		// cout << "无参构造函数调用" << '\n';
	// }
	
	// person(int a){
		// age = a;
		// cout << "有参构造函数调用" << '\n';
	// }
	//拷贝构造函数
	person(const person &p){
		age = p.age;
		cout << "拷贝构造函数调用" << '\n';
	}
	~person(){
		cout << "析构函数调用" << endl;
	}
};

void test(){
	person p;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	test();
	
	return 0;
}

 

  • 17
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏箱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值