#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
//构造函数;调用对象前调用,用于初始化对象;
//无参构造;默认构造
Person()
{
cout << "无参构造函数" << endl;
}
//有参构造;
Person(int a)
{
age = a;
cout << "有参构造函数" << endl;
}
//拷贝构造函数;
Person(const Person& p)
{
//将拷贝对象的属性拷贝到自己身上;
age = p.age;
cout << "拷贝构造函数" << endl;
}
//析构函数;销毁对象前调用,用于清理对象;
~Person()
{
cout << "Person的析构函数" << endl;
}
int age;
};
int main()
{
//cout << "1、括号法调用" << endl;
//Person p1; //默认构造函数调用;
//Person p2(10); //有参构造函数调用;
//Person p3(p2); //拷贝构造函数调用;
//调用默认构造函数时不要加();
cout << "2、显示法调用" << endl;
Person p1; //默认构造函数调用;
Person p2 = Person(10); //有参构造函数调用;
Person p3 = Person(p2); //拷贝构造函数调用;
//Person(20);//匿名对象构造 特点:当前行执行结束后,系统就立即回收掉匿名对象;
//不要利用拷贝构造函数初始化匿名对象;
//Person(p3); //p3重定义;编译器会认为Person(p3)===Person p3;
cout << "3、隐式转换法" << endl;
Person p4 = 10;
Person p5 = p4;
system("pause");
return 0;
}