C++:建構函式1——普通建構函式

來源:互聯網
上載者:User

標籤:size   不同   ret   16px   資料   返回   規則   有一個   初始化   

前言:建構函式是C+中很重要的一個概念,這裡對其知識進行一個簡單的總結

一、建構函式的定義

1.類中的建構函式名與類名必須相同

2.建構函式沒有函數的返回類值型說明符

[特別注意]:

a.建構函式的傳回值類型不是void,而是沒有

b.建構函式雖然沒有傳回值類型說明符,但建構函式是有傳回值的,建構函式的傳回值是其所建立的對象

1 class Student{2 public:3     Student(){} //建構函式的函數名與類名必須相同4                 //建構函式沒有傳回值類型說明符5     .....//其他動作6 }

3.在程式運行時,當新的對象被建立時,該對象所屬的類的建構函式自動被調用,且在該物件存留期中只調用這一次

4.建構函式可以重載。在類中可以定義多個建構函式,它們由不同的參數列表區分,系統在自動調用時按一般函數重載的規則選一個執行

 1 class Student{ 2 public: 3     Student(){}//建構函式1 4     Student(string name);//建構函式2 5     Student(string name,int age);//建構函式3 6     Student(string name,int age,string gender);//建構函式4 7 private: 8     string Name; 9     int Age;10     string Gender;    11 }12 13 int main(){14     Student stu1;//調用建構函式115     Student stu2("Tomwenxing");//調用建構函式216     Student stu3("Tomwenxing",23);//調用建構函式317     Student stu4("Tomwenxing",23,"male");//調用建構函式418     return 0;19 }

 5.建構函式可以在類中定義,也可以在類外定義

 1 #include<iostream> 2 #include<string>  3 using namespace std; 4 class Student{ 5 public: 6     Student(){} 7     Student(string name){//類內定義建構函式  8         this->name=name; 9     }10     Student(string name,int age);//類內聲明建構函式,類外定義建構函式 11 private:12     string name;13     int age;14 }; 15 16 Student::Student(string name,int age){ //類外定義建構函式 17     this->name=name;18     this->age=age;19 } 20 21 int main(){22     Student stu1;23     Student stu2("Tomwenxing");24     Student stu3("Tomwenxing",23);25     return 0;26 } 

6.如果類定義中沒有定義任何一個建構函式,C++編譯器會自動給出一個預設的建構函式(稱為預設建構函式):

預設建構函式:類名(){}

但是,只要我們手動定義了一個建構函式,系統就不會再為我們自動產生預設的預設建構函式了,如果仍希望類中由預設建構函式,需要編程者自己手動定義

[特別注意]:

只要建構函式是無參的或者只要各參數均有預設轉值的,C++編譯器都認為是預設的建構函式,因此預設的建構函式在類定義中至多隻能有一個

 1 #include<iostream> 2 using namespace std; 3 class Student{ 4 public: 5     Student(){} //建構函式1  6     Student(int value=0){//建構函式2  7         this->value=value; 8     }  9     Student(int value=0,int value2=0){//建構函式3 10         this->value=value;11         this->value2=value2;12     }13 private:14     int value;15     int value2; 16 };17 int main(){18     Student stu1; //錯誤:不知道調用哪個建構函式 19     return 0;20 } 

 7.建構函式初始化列表:負責為新建立的對象的一個或幾個資料成員賦初值。其中建構函式初始值是成員名字的一個列表,每個名字後面緊跟括弧括起來的(或者在花括弧內的)成員初始值,且不同成員的初始化通過逗號分開。

 1 #include<iostream> 2 using namespace std; 3 class Student{ 4 public: 5     Student(){}//手動定義預設建構函式 6     Student(int value1):Value(value1){}//建構函式初始化列表  7     Student(int value1,int value2);   8 private: 9     int Value;10     int Value2;  11 };12 Student::Student(int value1,int value2):Value(value1),Value2(value2){}13 int main(){14     Student stu1(10);15     Student stu2(10,20);16     return 0;17 } 

 

C++:建構函式1——普通建構函式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.