zoukankan      html  css  js  c++  java
  • C++ 构造函数与默认构造函数

    构造函数:C++用于构建类的新对象时需要调用的函数,该函数无返回类型!(注意:是“无”! 不是空!(void))。

    默认构造函数:未提供显式初始值时,用来穿件对象的构造函数。

    以上是二者的定义,但是单从定义很难对二者进行区别,下面依然看代码。

     1 class testClass
     2 {
     3 public:
     4     testClass();                    /* 默认构造函数 */
     5     testClass(int a, char b);        /* 构造函数 */
     6     testClass(int a=10,char b='c');    /* 默认构造函数 */
     7 
     8 private:
     9     int  m_a;
    10     char m_b;
    11 };

    上面的注释中已标出了默认构造函数和构造函数。下面,对二者的区别进行简单直白易懂的总结:

    1. 默认构造函数主要是用来完成如下形式的初始化的:

    1 testClass classA;
    2 // 或者  testClass *classA = new testClass;

    在这种情况下,如果没有提供默认构造函数,编译器会报错;

    非默认构造函数在调用时接受参数,如以下形式:

    1 testClass classA(12,'H');
    2 //或者  testClass *classA = new testClass(12,'H');

    2. 如果程序猿没有定义任何构造函数,则编译器会自动定义默认构造函数,其形式如  testClass() {};   可以看出,编译器自动提供的默认构造函数是 啥也没有啊 ;

    3. 定义默认构造函数有两种方式,如上述代码展示的,一是定义一个无参的构造函数,二是定义所有参数都有默认值的构造函数 ;

    4. 注意:一个类只能有一个默认构造函数!也就是说上述两种方式不能同时出现,一般选择 testClass(); 这种形式的默认构造函数 ;

    5. 只要程序猿定义了构造函数,编译器就不会再提供默认构造函数了,所以,程序猿最好再手动定义一个默认构造函数,以防出现 testClass a; 这样的错误。

    举例:

    #include <iostream> 
    using namespace std; 
    class Box {   
    public :   
        Box(int h=10,int w=10,int len=10); //在声明构造函数时指定默认参数--默认构造函数
        int volume( );   
    private :   
        int height;   
        int width;   
        int length; 
    }; 
    Box::Box(int h,int w,int len) 
    {   
        height=h;   
        width=w;   
        length=len; 
    }
    //或者
    //Box::Box(int h,int w,int len):height(h),width(w),length(len)
    //{
    //
    //}
    
    int Box::volume( )
    {   
        return (height*width*length);
    } 
    
    int main( ) 
    {  
        Box box1; //没有给实参   
        cout<<"The volume of box1 is "<<box1.volume( )<<endl;   
    
        Box box2(15); //只给定一个实参   
        cout<<"The volume of box2 is "<<box2.volume( )<<endl;   
    
        Box box3(15,30); //只给定2个实参   
        cout<<"The volume of box3 is "<<box3.volume( )<<endl; 
    
        Box box4(15,30,20); //给定3个实参  
        cout<<"The volume of box4 is "<<box4.volume( )<<endl;  
        return 0; 
    }

    可以看到,在构造函数中使用默认参数是方便而有效的,它提供了建立对象时的多种选择,它的作用相当于好几个重载的构造函数。 
    它的好处是,即使在调用构造函数时没有提供实参值,不仅不会出错,而且还确保按照默认的参数值对对象进行初始化。尤其在希望对每一个对象都有同样的初始化状况时用这种方法更为方便。

    二、构造函数重载 
    在一个类中可以定义多个构造函数,以便提供不同的初始化的方法,供用户选用。这些构造函数具有相同的名字,而参数的个数或参数的类型不相同。这称为构造函数的重载。

     1 #include <iostream> 
     2 using namespace std; 
     3 class Box {   
     4 public : 
     5     Box(); //声明一个无参的构造函数   
     6     //Box(int h); //有1个参数的构造函数 
     7     //Box(int h,int w); //有两个参数的构造函数 
     8     Box(int h, int w, int len) :height(h), width(w), length(len);//声明一个有参的构造函数,用参数的初始化表对数据成员初始化
     9     int volume( );   
    10 private :   
    11     int height;   
    12     int width;   
    13     int length; 
    14 }; 
    15 Box::Box() //定义一个无参的构造函数 
    16 {   
    17     height=10; 
    18     width=10; 
    19     length=10; 
    20 } 
    21 //Box::Box(int h)
    22 //{
    23 //
    24 //}
    25 //
    26 //Box::Box(int h,int w)
    27 //{
    28 //
    29 //}
    30 
    31 Box::Box(int h, int w, int len) :height(h), width(w), length(len)
    32 {
    33 }
    34 
    35 int Box::volume( )
    36 {   
    37     return (height*width*length); 
    38 } 
    39 
    40 int main( ) 
    41 {   
    42     Box box1; //建立对象box1,不指定实参  
    43     cout<<"The volume of box1 is "<<box1.volume( )<<endl;   
    44     Box box4(15,30,25); //建立对象box4,指定3个实参   
    45     cout<<"The volume of box4 is "<<box4.volume( )<<endl;   
    46     return 0; 
    47 } 

    以上代码定义了四个重载构造和拿书,在建立对象时不指定参数,或者分别给定1个参数,2个参数和3个参数。

    关于构造函数的重载的几点说明: 
    1、调用构造函数时不必给出实参的构造函数,称为默认构造函数(default constructor)。显然,无参的构造函数属于默认构造函数。一个类只能有一个默认构造函数。如果在建立对象时选用的是无参构造函数,应注意正确书写定义对象的语句。 
    2、尽管在一个类中可以包含多个构造函数,但是对于每一个对象来说,建立对象时只执行其中一个构造函数,并非每个构造函数都被执行。

  • 相关阅读:
    Lumen源码分析之 一步一步带你实现Lumen容器(一)
    php 注册器模式 工厂模式
    理解 PHP 依赖注入 和 控制反转
    composer使用git作为仓储
    monolog记录日志
    Jupyter Notebook快捷键
    图像灰度化
    一道算法题:拼数字
    [转]Vue生态系统中的库
    window.postMessage实现网页间通信
  • 原文地址:https://www.cnblogs.com/Bella2017/p/9364545.html
Copyright © 2011-2022 走看看