日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++构造函数和析构函数的学习(一)

發布時間:2025/6/15 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++构造函数和析构函数的学习(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? 構造函數是類中特殊的成員函數。

? ? 創建類類型的新對象的,系統會自動調用構造函數。

? ? 構造函數的調用是為了保證每個數據成員都能被正確初始化。

? ? 構造函數的作用初始化。

? ? 通常情況下,構造函數應聲明為公有函數,構造它不能像其他成員函數那樣被顯式的調用。

? ? 構造函數被聲明為私有有特殊的用途。

? ? 構造函數可以有任意類型和任意個數的參數,一個類可以有多個構造函數。

? ??如果程序未聲明構造函數,默認會生成一個空的構造函數。

? ??不帶參數的構造函數稱為默認構造函數。

? ? 如果有一個構造函數,系統不再生成默認構造函數

?

Test.h

  • //Test.h?
  • #?ifndef?_TEST_H_?
  • #?define?_TEST_H_?
  • ?
  • class?Test?
  • {?
  • public:?//如果程序未聲明構造函數,默認會生成一個空的構造函數
  • ????Test();?
  • private:?
  • ????int?num_;?
  • };?
  • ?
  • #?endif?//_TEST_H_?
  • Test.cpp

  • //Test.cpp?
  • #?include?"Test.h"?
  • #?include?<iostream>?
  • using?namespace?std;?
  • ?
  • Test::Test()?
  • {?
  • ????num_?=?0;?
  • ????cout?<<?"Initializing?Default?"?<<?endl;?
  • }?
  • main.cpp

    ?

  • #?include?<iostream>?
  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • int?main(void)?
  • {?//自動調用構造函數
  • ????Test?t;?
  • ?
  • ????return?0;?
  • }?
  • ?運行結果:

    ?// ? ?如果有一個構造函數,系統不再生成默認構造函數

    ?

    Test.h

  • //Test.h?
  • #?ifndef?_TEST_H_?
  • #?define?_TEST_H_?
  • ?
  • class?Test?
  • {?
  • public:?//如果程序未聲明構造函數,默認會生成一個空的構造函數
  • ????Test(int num);?
  • private:?
  • ????int?num_;?
  • };?
  • ?
  • #?endif?//_TEST_H_?
  • Test.cpp

  • //Test.cpp?
  • #?include?"Test.h"?
  • #?include?<iostream>?
  • using?namespace?std;?
  • ?
  • Test::Test(int num)?
  • {?
  • ????num_?=?num;?
  • ????cout?<<?"Initializing?" << num_ ?<<?endl;?
  • }?
  • main.cpp

    ?

  • #?include?<iostream>?
  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • int?main(void)?
  • {?//自動調用構造函數
  • ????Test?t(10);?
  • ?
  • ????return?0;?
  • }?
  • ?運行結果:

    ?

    ?

    構造函數重載的實例:

    ?

    Test.h

  • //Test.h?
  • #?ifndef?_TEST_H_?
  • #?define?_TEST_H_?
  • ?
  • class?Test?
  • {?
  • public:?//如果程序未聲明構造函數,默認會生成一個空的構造函數
  • Test();
  • ????Test(int num);?
  • private:?
  • ????int?num_;?
  • };?
  • ?
  • #?endif?//_TEST_H_?
  • Test.cpp

  • //Test.cpp?
  • #?include?"Test.h"?
  • #?include?<iostream>?
  • using?namespace?std;?
  • ?
  • Test::Test()
  • {
  • num_ = 0;
  • ????cout?<<?"Initializing?default " ?<<?endl;?
  • }?
  • ?
  • Test::Test(int num)?
  • {?
  • ????num_?=?num;?
  • ????cout?<<?"Initializing?" << num_ ?<<?endl;?
  • }?
  • main.cpp

  • #?include?<iostream>?
  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • int?main(void)?
  • {?//自動調用構造函數
  • ????Test t1;
  • Test?t2(10);?
  • ?
  • ????return?0;?
  • }?
  • ?運行結果:

    ?

    ?構造函數與new運算符

  • //構造函數與new運算符?
  • ?
  • #?ifndef?_TEST_H_?
  • #?define?_TEST_H_?
  • ?
  • class?Test?
  • {?
  • public:?
  • ????可以顯式的寫一個默認構造函數,這樣兩個函數就成了重載?
  • ????Test();?
  • ????Test(int?num);?
  • ????//析構函數不能重載?
  • ????//析構函數不能帶參數,如果帶參數就可以重載?
  • ????~Test();?
  • ????void?Display();?
  • private:?
  • ????int?num_;?
  • };?
  • ?
  • #?endif??
  • Test.cpp

  • #?include?"Test.h"?
  • #?include?<iostream>?
  • using?namespace?std;?
  • ?
  • void?Test::Display()?
  • {?
  • ????cout?<<?num_?<<?endl;?
  • }?
  • Test::Test()?
  • {?
  • ????num_?=?0;?
  • ????cout<<"Initializing?Default"?<<?endl;?
  • ?????
  • }?
  • Test::Test(int?num)?
  • {?
  • ????num_?=?num;?
  • ????cout<<"Initializing?"<<num_?<<?endl;?
  • }?
  • ?
  • Test::~Test()?
  • {?
  • ????cout?<<?"Destory?"?<<?num_?<<?endl;?
  • }?
  • main.cpp

  • #?include?<iostream>?
  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • int?main(void)?
  • {?
  • ????Test?t;?
  • ????t.Display();??
  • ?
  • ????Test?t2(20);?????????
  • ????t2.Display();??
  • //不僅僅分配了內存,還調用了構造函數?
  • ????Test?*?t3?=?new?Test(20);??//new?operator?
  • ????t3->Display();?
  • //不僅僅釋放了內存,也調用了析構函數??????
  • ????delete?t3;?
  • ?
  • ????return?0;?
  • }?

  • ?

    運行結果:

    ?

    //全局對象的構造先于main函數?

    ?

  • ?
  • #?ifndef?_TEST_H_?
  • #?define?_TEST_H_?
  • ?
  • class?Test?
  • {?
  • public:?
  • ????可以顯式的寫一個默認構造函數,這樣兩個函數就成了重載?
  • ????Test();?
  • ????Test(int?num);?
  • ????//析構函數不能重載?
  • ????//析構函數不能帶參數,如果帶參數就可以重載?
  • ????~Test();?
  • ????void?Display();?
  • private:?
  • ????int?num_;?
  • };?
  • ?
  • #?endif??
  • Test.cpp

  • #?include?"Test.h"?
  • #?include?<iostream>?
  • using?namespace?std;?
  • ?
  • void?Test::Display()?
  • {?
  • ????cout?<<?num_?<<?endl;?
  • }?
  • Test::Test()?
  • {?
  • ????num_?=?0;?
  • ????cout<<"Initializing?Default"?<<?endl;?
  • ?????
  • }?
  • Test::Test(int?num)?
  • {?
  • ????num_?=?num;?
  • ????cout<<"Initializing?"<<num_?<<?endl;?
  • }?
  • ?
  • Test::~Test()?
  • {?
  • ????cout?<<?"Destory?"?<<?num_?<<?endl;?
  • }?
  • ?

    main.cpp

  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • //全局對象的構造先于main函數?
  • Test?t(10);?
  • ?
  • int?main(void)?
  • {?
  • ????cout?<<?"Entering?main?..."?<<?endl;?
  • ????cout?<<?"Exiting??main?..."?<<?endl;?
  • ?
  • ????return?0;?
  • }?
  • 運行結果:

    ?

    ?

    ? ? 默認析構函數是一個空函數

    ? ? 析構函數沒有參數

    ? ? 析構函數不能被重載

    ?

    ? ? 析構函數與數組

    Test.h

  • //Test.h?
  • #?ifndef?_TEST_H_?
  • #?define?_TEST_H_?
  • ?
  • class?Test?
  • {?
  • public:?
  • ????可以顯式的寫一個默認構造函數,這樣兩個函數就成了重載?
  • ????Test();?
  • ????Test(int?num);?
  • ????//析構函數不能重載?
  • ????//析構函數不能帶參數,如果帶參數就可以重載?
  • ????~Test();?
  • ????void?Display();?
  • private:?
  • ????int?num_;?
  • };?
  • ?
  • #?endif?//_TEST_H_?
  • Test.cpp

  • //Test.cpp?
  • #?include?<iostream>?
  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • void?Test::Display()?
  • {?
  • ????cout?<<?num_?<<?endl;?
  • }?
  • Test::Test()?
  • {?
  • ????num_?=?0;?
  • ????cout<<"Initializing?Deafule?"?<<?endl;?
  • }?
  • Test::Test(int?num)?
  • {?
  • ????num_?=?num;?
  • ????cout<<"Initializing?"<<num_?<<?endl;?
  • }?
  • ?
  • Test::~Test()?
  • {?
  • ????cout?<<?"Destory?"?<<?num_?<<?endl;?
  • }?
  • ?

    main.cpp

  • //main.cpp?
  • #?include?<iostream>?
  • #?include?"Test.h"?
  • using?namespace?std;?
  • ?
  • int?main(void)?
  • {??????//定義對象數組?
  • ????Test?t[2]?=?{10,20};?
  • ????//動態對象?
  • ????Test*?t2?=?new?Test(2);?//傳遞參數2?
  • ????delete?t2;?
  • ????//沒有傳遞任何參數,創建了兩個對象?
  • ????Test*?t3?=?new?Test[2];?
  • ????delete[]?t3;?
  • ?????
  • ????return?0;?
  • }?
  • 運行結果:

    ? ? 析構函數不能重載

    ?

    ? ? 析構函數不能帶參數,如果帶參數就可以重載

    ?

    ? ? 析構函數可以顯式調用,一般很少用到,會實現一些特殊的效果:

  • //main.cpp?
  • #?include?"Test.h"?
  • ?
  • int?main(void)?
  • {?
  • ????Test?t;?
  • ????t.~Test();?//析構函數被調用了兩次?
  • ????//析構函數很少調用,可以顯式調用?
  • ????return?0;?
  • }?
  • ?

    轉載于:https://blog.51cto.com/liam2199/1172398

    總結

    以上是生活随笔為你收集整理的C++构造函数和析构函数的学习(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。