C++调用无参构造函数

之前不知道为什么调用无参构造函数不能加(),是因为加上了()这样的形式就和函数声明语句一样了

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
class people{
    public:
        people(){
            printf("123\n");
        }
};
class student :public people{

    public:
        int x=1;
        //调用下面这个构造函数,如果不传参数会和 student()重复,如果传一个参数会和 student(int x)重复
        // student(int x=2,int y=1){
        //     printf("111\n");
        // }
        student(int x){
            printf("222\n");
        }
        student(){
            printf("5555\n");
        }
};
int main()
{
    //下面这样写不是调用类的无参构造函数,而是一个函数的声明(可不是创建一个student类型的对象)。所以调用无参构造函数不要加()
    //student stu();
    student stu;
    printf("%d\n",stu.x);

    return 0;
}

原文链接: https://www.cnblogs.com/kongbursi-2292702937/p/14724091.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/210089

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午12:09
下一篇 2023年2月13日 上午12:09

相关推荐