1.构造函数分类
按照参数分类分为有参和无参构造 无参又称为默认函数构造
按照类型分为普通构造和拷贝构造
#include <iostream>
using namespace std;
class Person
{
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()
{
//1,括号法
// Person p1;//默认构造函数,不要加括号,不然会被当成函数声明。
// Person p2(18);//有参构造函数
// Person p3(p2);//拷贝构造函数
// cout << "p2的年龄为" << p2.age << endl;
// cout << "p3的年龄为" << p3.age << endl;
//2.显示法
// Person p1;
// Person p2 = Person(10);//有参构造
// Person p3 = Person(p2);//拷贝构造
Person(10);//匿名对象 特点:当前行执行结束后,系统会立即回收掉匿名对象
cout << "aaaaaaaaaaaaaa" << endl;
}
int main()
{
system("color f5");
test01();
system("pause");
return 0;
}