C++构造函数实例

#include<iostream>
#include <string>
using namespace std;

class Person
{
public:
//无参(默认)构造函数
    Person()
    {
        cout << "no" << endl;
    }
//有参构造函数
    Person(int a)
    {
        age = a;
        cout << "is" << endl;
    }
//拷贝构造函数
    Person(const Person& p)
    {
        age = p.age;
        cout << "copy" << endl;
    }
//析构函数
    ~Person()
    {
        cout << "de" << endl;
    }
public:
    int age;
};
void test01()
{
    Person p1(18);
//如果不写拷贝构造,编译器会自动添加拷贝构造,并且做浅拷贝操作
    Person p2(p1);
    cout << "p2 age = " << p2.age << endl;
}
void test02()
{
//如果用户提供有参构造,编译器不会提供默认构造,会提供拷贝构造
    Person p1; //此时如果用户自己没有提供默认构造,会出错
    Person p2(10); //用户提供的有参
    Person p3(p2); //此时如果用户没有提供拷贝构造,编译器会提供
//如果用户提供拷贝构造,编译器不会提供其他构造函数
    Person p4; //此时如果用户自己没有提供默认构造,会出错
    Person p5(10); //此时如果用户自己没有提供有参,会出错
    Person p6(p5); //用户自己提供拷贝构造
}
int main()
{
    test02();
    system("pause");
    return 0;
}

  

posted @ 2019-06-27 17:12  wdliming  阅读(663)  评论(0编辑  收藏  举报