按照参数分类 无参构造 有参构造
按照类型 普通构造和拷贝构造
括号法
- Person p; //默认构造函数的调用
- Person p1; //默认构造函数调用
- Person p2(10); //有参构造函数调用
- Person p3(p2); //拷贝构造函数调用
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
using namespace std;
class Person {
private:
public:
Person() {
cout << "Person构造函数的调用" << endl;
}
Person(int a) {
age = a;
cout << "Person构造函数的调用" << endl;
}
Person(const Person &p)
{
age = p.age;
cout << "Person拷贝构造函数的调用" << endl;
}
~Person() {
cout << "Person析构函数的调用" << endl;
}
int age;
};
void test01()
{
Person p1;
Person p2 = Person(10);
Person p3 = Person(p2);
Person p4 = 10;
Person p5 = p4;
}
int main()
{
test01();
return 0;
}