#include<iostream>
using namespace std;
//构造函数的调用规则
//1、创建一个类 C++编译器会给每个类都添加至少3个函数
//默认构造(空实现)
//析构函数(空实现)
//拷贝构造(值拷贝)
//2、如果我们写了有参构造函数 ,编译器就不再提供默认构造,依然提供拷贝构造
class person
{
public:
person() //默认构造函数
{
cout << "person的默认构造函数调用" << endl;
}
person(int age1)//有参构造函数
{
age = age1;
cout << "person的有参构造函数调用" << endl;
}
person(const person& p)//拷贝构造函数
{
age = p.age;
cout << "person的拷贝构造函数调用" << endl;
}
~person()//析构函数
{
cout << "person的析构函数调用" << endl;
}
int age;
};
//void test01()
//{
// person p;//创建一个对象p
// p.age = 18;
// person p2(p);
// cout << "p2的年龄为: " << p2.age << endl;
//}
void test02()
{
person p(30);
person p1(p);
cout << "p1的年龄为: " << p1.age << endl;
}
int main()
{
//test01();
test02();
system("pause");//按任意键继续
return 0;//关闭程序
}
所有笔记全部自用 !