C++调用无参构造函数

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

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
class people{
    public:
        people(){
            printf("123
");
        }
};
class student :public people{
    
    public:
        int x=1;
        //调用下面这个构造函数,如果不传参数会和 student()重复,如果传一个参数会和 student(int x)重复
        // student(int x=2,int y=1){
        //     printf("111
");
        // }
        student(int x){
            printf("222
");
        }
        student(){
            printf("5555
");
        }
};
int main()
{
    //下面这样写不是调用类的无参构造函数,而是一个函数的声明(可不是创建一个student类型的对象)。所以调用无参构造函数不要加()
    //student stu();
    student stu;
    printf("%d
",stu.x);

    return 0;
}

 

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/14724091.html