解析C++中構(gòu)造函數(shù)的默認參數(shù)和構(gòu)造函數(shù)的重載
C++構(gòu)造函數(shù)的默認參數(shù)
和普通函數(shù)一樣,構(gòu)造函數(shù)中參數(shù)的值既可以通過實參傳遞,也可以指定為某些默認值,即如果用戶不指定實參值,編譯系統(tǒng)就使形參取默認值。
【例】
#include <iostream> using namespace std; class Box { public : Box(int h=10,int w=10,int len=10); //在聲明構(gòu)造函數(shù)時指定默認參數(shù) int volume( ); private : int height; int width; int length; }; Box::Box(int h,int w,int len) //在定義函數(shù)時可以不指定默認參數(shù) { 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; }
程序運行結(jié)果為:
The volume of box1 is 1000 The volume of box2 is 1500 The volume of box3 is 4500 The volume of box4 is 9000
程序中對構(gòu)造函數(shù)的定義(第12-16行)也可以改寫成參數(shù)初始化表的形式:
Box::Box(int h,int w,int len):height(h),width(w),length(len){ }
可以看到,在構(gòu)造函數(shù)中使用默認參數(shù)是方便而有效的,它提供了建立對象時的多種選擇,它的作用相當(dāng)于好幾個重載的構(gòu)造函數(shù)。
它的好處是,即使在調(diào)用構(gòu)造函數(shù)時沒有提供實參值,不僅不會出錯,而且還確保按照默認的參數(shù)值對對象進行初始化。尤其在希望對每一個對象都有同樣的初始化狀況時用這種方法更為方便。
關(guān)于構(gòu)造函數(shù)默認值的幾點說明:
應(yīng)該在聲明構(gòu)造函數(shù)時指定默認值,而不能只在定義構(gòu)造函數(shù)時指定默認值。
程序第5行在聲明構(gòu)造函數(shù)時,形參名可以省略。
如果構(gòu)造函數(shù)的全部參數(shù)都指定了默認值,則在定義對象時可以給一個或幾個實參,也可以不給出實參。
在一個類中定義了全部是默認參數(shù)的構(gòu)造函數(shù)后,不能再定義重載構(gòu)造函數(shù)。
C++構(gòu)造函數(shù)的重載
在一個類中可以定義多個構(gòu)造函數(shù),以便提供不同的初始化的方法,供用戶選用。這些構(gòu)造函數(shù)具有相同的名字,而參數(shù)的個數(shù)或參數(shù)的類型不相同。這稱為構(gòu)造函數(shù)的重載。
通過下面的例子可以了解怎樣應(yīng)用構(gòu)造函數(shù)的重載。
【例】定義兩個構(gòu)造函數(shù),其中一個無參數(shù),一個有參數(shù)。
#include <iostream> using namespace std; class Box { public : Box( ); //聲明一個無參的構(gòu)造函數(shù) //聲明一個有參的構(gòu)造函數(shù),用參數(shù)的初始化表對數(shù)據(jù)成員初始化 Box(int h,int w,int len):height(h),width(w),length(len){ } int volume( ); private : int height; int width; int length; }; Box::Box( ) //定義一個無參的構(gòu)造函數(shù) { height=10; width=10; length=10; } int Box::volume( ){ return (height*width*length); } int main( ) { Box box1; //建立對象box1,不指定實參 cout<<"The volume of box1 is "<<box1.volume( )<<endl; Box box2(15,30,25); //建立對象box2,指定3個實參 cout<<"The volume of box2 is "<<box2.volume( )<<endl; return 0; }
在本程序中定義了兩個重載的構(gòu)造函數(shù),其實還可以定義其他重載構(gòu)造函數(shù),其原型聲明可以為:
Box::Box(int h); //有1個參數(shù)的構(gòu)造函數(shù) Box::Box(int h,int w); //有兩個參數(shù)的構(gòu)造函數(shù)
在建立對象時分別給定1個參數(shù)和2個參數(shù)。
關(guān)于構(gòu)造函數(shù)的重載的幾點說明:
調(diào)用構(gòu)造函數(shù)時不必給出實參的構(gòu)造函數(shù),稱為默認構(gòu)造函數(shù)(default constructor)。顯然,無參的構(gòu)造函數(shù)屬于默認構(gòu)造函數(shù)。一個類只能有一個默認構(gòu)造函數(shù)。
如果在建立對象時選用的是無參構(gòu)造函數(shù),應(yīng)注意正確書寫定義對象的語句。
盡管在一個類中可以包含多個構(gòu)造函數(shù),但是對于每一個對象來說,建立對象時只執(zhí)行其中一個構(gòu)造函數(shù),并非每個構(gòu)造函數(shù)都被執(zhí)行。
相關(guān)文章
C++實現(xiàn)LeetCode(2.兩個數(shù)字相加)
這篇文章主要介紹了C++實現(xiàn)LeetCode(兩個數(shù)字相加),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07C++20 新特性 協(xié)程 Coroutines(2)
上篇文章簡單給大介紹了 C++20 特性 協(xié)程 Coroutines co_yield 和 co_return 那么這篇文章繼續(xù)給大家介紹C++20 的新特性協(xié)程 Coroutines co_await,需要的朋友可以參考一下2021-10-10