日韩性视频-久久久蜜桃-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++构造函数和析构函数的学习(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 污视频在线 | free黑人多人性派对hd | 国产精品第6页 | 国产一区二区三区免费观看视频 | 一色综合 | 欧美乱欲视频 | 国产视频播放 | 日韩精品一区二区三区中文在线 | 男男受被啪到高潮自述 | 日韩黄片一区二区三区 | 黄色1级视频 | 国产乱子伦精品视频 | 91av麻豆| 精品无码人妻一区 | 91伦理在线 | 国产精品99精品久久免费 | 91欧美在线视频 | 亚洲免费三级 | xxxxx国产| 亚洲五月激情 | 色哟哟官网 | 超碰免费成人 | 中日韩精品在线 | 恶虐女帝安卓汉化版最新版本 | 青青草原伊人网 | 日韩不卡在线观看 | 伊伊综合网 | 免费国产羞羞网站视频 | 五月天婷婷爱 | 亚洲av高清一区二区三区 | 黑人玩弄人妻一区二区三区 | 欧美精品xx | 神马午夜电影一区二区三区在线观看 | 国产久草视频 | 亚洲欧洲综合在线 | 国产精品无码毛片 | se94se欧美| 天天插天天狠 | 久久综合91 | 欧美日日日| 国产激情一区二区三区四区 | 午夜国产在线 | 欧美专区在线视频 | 美女下部无遮挡 | 中文字幕乱码在线观看 | 亚洲一区日本 | 免费在线不卡视频 | 欧美亚洲网站 | 久久久高清视频 | 草av| av午夜激情| 秋霞电影一区二区 | 每日av在线| www.com色| 青青草视频在线观看 | 日韩三级免费看 | 在线观看免费视频一区 | 亚洲欧美一区二区三区四区五区 | 欧美精品久久 | 91精品播放| 国产在线看片 | 亚洲九九九九 | 欧美午夜精品 | av成人免费 | 国产精品成人国产乱 | 高清视频一区二区三区 | 久久国产柳州莫菁门 | 98堂 最新网名 | 午夜在线观看影院 | 韩国美女一区二区 | 国产香蕉在线观看 | 国产主播一区二区 | 91尤物在线 | 亚洲一区二区久久久 | 91正在播放 | 免费观看国产视频 | 免费v片在线观看 | aaa在线 | 神马午夜伦理影院 | 一区二区不卡免费视频 | 久草免费在线视频观看 | 亚洲性猛交富婆 | 在线观看成人一区 | 久久av色| 日本福利视频导航 | 91蜜桃网站 | 亚洲国产亚洲 | 欧美日韩高清在线 | 国产精品99久久久精品无码 | 音影先锋av资源 | 国产成人无码精品久久二区三 | 亚洲欧美乱综合图片区小说区 | 黄色在线观看www | 亚洲综合色一区 | 欧美激情爱爱 | 91九色国产 | 欧美日韩1区 | 91资源在线播放 | 97精品在线 |