zoukankan      html  css  js  c++  java
  • 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;
    }

     

  • 相关阅读:
    字典列表元组的推导式使用方法
    算法之排序
    闭包与装饰器
    循环(打印一些小东西)
    iOS内存小知识
    NSset
    字典
    NSNumber
    NSString
    NSArray
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/14724091.html
Copyright © 2011-2022 走看看