目录
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;
} 
 
 
  
  
  
  
 



















