首页 > 编程语言 > 详细

C++调用无参构造函数

时间:2021-05-01 16:00:58      阅读:18      评论:0      收藏:0      [点我收藏+]

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

#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;
}

 

 

C++调用无参构造函数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!