12.构造函数的分类及调用

//2022年9月9日09:21:51
#include <iostream>
using namespace std;

class Maker
{
public:
    //按照参数分类
    Maker()
    {
        cout << "无参构造" << endl;
    }
    Maker(int a)
    {
        cout << "有参构造" << endl;
    }
    //按照类型:普通构造函数,拷贝构造函数
    Maker(const Maker &m)
    {
        cout << "拷贝构造" << endl;
    }

    //默认的赋值函数,后面讲
};

void test01()
{
    Maker m;//调用无参构造函数
    Maker m1(10);//调用有参构造函数
    Maker m2(m1);//调用拷贝构造函数

    //不常用
    Maker m4 = Maker(10);//调用的是有参构造函数
    Maker m3 = m2;//调用拷贝构造函数
    cout << "=========" << endl;
    Maker m5 = 10;//Maker m5 = Maker(10);
    cout << "=========" << endl;

    Maker m6;
    m6 = m5;//赋值操作
}

int main()
{
    test01();
    system("pause");
    return EXIT_SUCCESS;
}

参考资料

参考资料来源于黑马程序员等

posted @   CodeMagicianT  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示