美文网首页
9.构造函数调用规则

9.构造函数调用规则

作者: lxr_ | 来源:发表于2021-01-30 11:16 被阅读0次
#include<iostream>
using namespace std;
//***默认情况下,编译器至少给一个类添加三个函数***
//1.默认构造函数(无参函数,函数体为空)
//2.默认析构函数(无参函数,函数体为空)
//3.默认拷贝构造函数,对属性进行值拷贝

//***构造函数定义规则如下:***
//1.如果用户定义有参构造函数,c++不再提供默认无参构造,但是会提供默认拷贝构造
//2.如果用户定义拷贝构造函数,c++不再提供其他构造函数

class student
{
public:
    int m_Age;
    student()
    {
        cout << "student的默认构造函数" << endl;
    }
    student(int age)
    {
        m_Age = age;
        cout << "student的有参构造函数" << endl;
    }
    student(const student& s)
    {
        m_Age = s.m_Age;
        cout << "student的拷贝构造" << endl;
    
    }
    ~student()
    {
        cout << "student析构函数" << endl;
    }

};
void test06()
{
    /*student s1;
    s1.m_Age = 90;*/
    student s1(33);

    student s2(s1);
    cout << "s2年龄:" << s2.m_Age << endl;
}
int main()
{
    test06();

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:9.构造函数调用规则

      本文链接:https://www.haomeiwen.com/subject/kqihtltx.html