欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹

 更新時(shí)間:2021年09月07日 14:34:09   作者:我是小白呀  
這篇文章主要介紹了C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

構(gòu)造函數(shù)

構(gòu)造函數(shù) (constructor) 是一種特殊的成員函數(shù). 它會(huì)在每次創(chuàng)建類的新對(duì)象時(shí)執(zhí)行. 構(gòu)造函數(shù)的名稱與類的名稱是完全相同的, 并且不會(huì)返回任何類型. 構(gòu)造函數(shù)可用于為某些成員變量設(shè)置初始值.

在這里插入圖片描述

格式:

Class::Class(); // 構(gòu)造函數(shù)

默認(rèn)構(gòu)造函數(shù)

如果用戶自己沒有定義構(gòu)造函數(shù), C++ 系統(tǒng)會(huì)自動(dòng)生成一個(gè)默認(rèn)構(gòu)造函數(shù). 這個(gè)構(gòu)造函數(shù)體是空的, 沒有參數(shù), 不執(zhí)行初始化操作.

例如:

Time.h:

class Time {
private:
    int hour;
    int minute;
    int second;
public:
    Time(); // 默認(rèn)構(gòu)造函數(shù)
    void set_time(int h, int m, int s);
    void show_time();
};

Time.cpp:

#include "Time.h"
#include <iostream>

using namespace std;

Time::Time() {
    hour = 0;
    minute = 0;
    second = 0;
}

void Time::set_time(int h, int m, int s) {
    hour = h;
    minute = m;
    second = s;
}

void Time::show_time() {
    cout << hour << ":" << minute << ":" << second << endl;
}

main:

#include "Time.h"
#include <iostream>

using namespace std;

int main() {

    Time time1;  // 實(shí)例化time
    time1.show_time();  // 調(diào)用show_time

    return 0;
}

輸出結(jié)果:

0:0:0

重點(diǎn):

  • 即使提供了其他構(gòu)造函數(shù), 提供一個(gè)默認(rèn)構(gòu)造函數(shù)幾乎總是對(duì)的
  • 通常在默認(rèn)構(gòu)造函數(shù)中, 給成員提供的初始值應(yīng)該指出該對(duì)象是 “空” 的

有參構(gòu)造函數(shù)

構(gòu)造函數(shù)中參數(shù)可以指定默認(rèn)值. 如果童虎不指定實(shí)參指, 編譯西永就使形參取默認(rèn)值.

例如:

Time 類:

#ifndef PROJECT1_TIME_H
#define PROJECT1_TIME_H

class Time {
private:
    int hour;
    int minute;
    int second;
public:
    Time();  // 默認(rèn)構(gòu)造函數(shù)
    Time(int h, int m=0, int s=0);  // 有參構(gòu)造函數(shù)
    void show_time();
};

#endif //PROJECT1_TIME_H

Time.cpp:

#include "Time.h"
#include <iostream>
using namespace std;

// 默認(rèn)構(gòu)造函數(shù)
Time::Time() : hour(0), minute(0), second(0) {}

// 有參構(gòu)造函數(shù)
Time::Time(int h, int m, int s) : hour(h), minute(m), second(s) {}


void Time::show_time() {
    cout << hour << ":" << minute << ":" << second << endl;
}

main:

#include "Time.h"
#include <iostream>
using namespace std;

int main() {

    Time time1;
    time1.show_time();

    Time time2(8);
    time2.show_time();

    Time time3(8, 8);
    time3.show_time();
    
    Time time4(8, 8, 8);
    time4.show_time();

    return 0;
}

輸出結(jié)果:

0:0:0
8:0:0
8:8:0
8:8:8

析構(gòu)函數(shù)

析構(gòu)函數(shù) (destructor) 也是一個(gè)特殊的成員函數(shù). 當(dāng)對(duì)象的生命期結(jié)束時(shí), 會(huì)自動(dòng)執(zhí)行析構(gòu)函數(shù). 析構(gòu)函數(shù)的名字是類名前面加一個(gè) “~” 符號(hào).

在這里插入圖片描述

格式:

Class::~Class();  // 析構(gòu)函數(shù)

析構(gòu)函數(shù)的作用在撤銷對(duì)象占用的內(nèi)存之前完成一些清理 & 善后的工作.

析構(gòu)函數(shù)例子

Student 類:

#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H

#include <string>
using namespace std;

class Student {
private:
    int num;
    string name;
    char gender;
public:
    Student();
    Student(int num, string name, char gender);
    ~Student();
    void display();
};

#endif //PROJECT1_STUDENT_H

Student.cpp:

#include "Student.h"
#include <iostream>
using namespace std;

// 無參構(gòu)造
Student::Student() : num(-1), name("None"), gender('N') {}

Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
    cout << "執(zhí)行構(gòu)造函數(shù): " << "Welcome, " << name << endl;
}

void Student::display() {
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "gender: " << gender << endl;
    cout << "===============" << endl;
}

Student::~Student() {
    cout << "執(zhí)行析構(gòu)函數(shù): " << "Bye bye, " << name << endl;
}

main:

#include "Student.h"
#include <iostream>
using namespace std;

int main() {

    Student student1(1, "Little white", 'f');
    Student student2(2, "Big white", 'f');

    student1.display();
    student2.display();

    return 0;
}

輸出結(jié)果:

執(zhí)行構(gòu)造函數(shù): Welcome, Little white
執(zhí)行構(gòu)造函數(shù): Welcome, Big white
num: 1
name: Little white
gender: f
===============
num: 2
name: Big white
gender: f
===============
執(zhí)行析構(gòu)函數(shù): Bye bye, Big white
執(zhí)行析構(gòu)函數(shù): Bye bye, Little white

析構(gòu)函數(shù)執(zhí)行時(shí)機(jī)

對(duì)于函數(shù)中定義的自動(dòng)局部對(duì)象, 當(dāng)函數(shù)被調(diào)用結(jié)束時(shí), 對(duì)象釋放. 在對(duì)象釋放前自自動(dòng)執(zhí)行析構(gòu)函數(shù).

在這里插入圖片描述

局部對(duì)象

static 局部對(duì)象只在 main 函數(shù)結(jié)束或調(diào)用 exit 函數(shù)結(jié)束程序時(shí), 調(diào)用 static 局部對(duì)象的洗后函數(shù).

全局對(duì)象

對(duì)于全局對(duì)象, 在程序的流程離開其作用域時(shí) (如 main 函數(shù)結(jié)束或調(diào)用 exit 函數(shù)) 時(shí), 調(diào)用該全局對(duì)象的析構(gòu)函數(shù).

到此這篇關(guān)于C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++ 構(gòu)造函數(shù) 析構(gòu)函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Qt中connect()函數(shù)及用法詳解

    Qt中connect()函數(shù)及用法詳解

    connect() 函數(shù)就是Qt 框架中用于將信號(hào)(SIGNAL)和槽(SLOT)關(guān)聯(lián)起來的核心函數(shù),本文給大家介紹Qt中connect()函數(shù),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • 基于C/C++ 常見誤區(qū)詳解

    基于C/C++ 常見誤區(qū)詳解

    本篇文章介紹了在c和c++中一些常見誤區(qū)的詳細(xì)概述。需要的朋友參考下
    2013-05-05
  • C++實(shí)現(xiàn)LeetCode(32.最長(zhǎng)有效括號(hào))

    C++實(shí)現(xiàn)LeetCode(32.最長(zhǎng)有效括號(hào))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(32.最長(zhǎng)有效括號(hào)),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • VS2022+libtorch+Cuda11.3安裝測(cè)試教程詳解(調(diào)用cuda)

    VS2022+libtorch+Cuda11.3安裝測(cè)試教程詳解(調(diào)用cuda)

    這篇文章主要介紹了VS2022+libtorch+Cuda11.3安裝測(cè)試(調(diào)用cuda),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C語言中#if的使用詳解

    C語言中#if的使用詳解

    #if和#endif是一組同時(shí)使用的,叫做條件編譯指令。#if與#define、#include等指令一樣是由預(yù)處理器這個(gè)強(qiáng)大的工具處理的,預(yù)處理器可以在編譯前處理c程序,這篇文章主要介紹了C語言中#if的使用,需要的朋友可以參考下
    2022-11-11
  • C語言輸入一個(gè)字符串的方法有哪些

    C語言輸入一個(gè)字符串的方法有哪些

    字符串輸入是C語言編程中非常重要的部分,其中scanf函數(shù)是一種廣泛使用的輸入字符串的方法,下面這篇文章主要給大家介紹了關(guān)于C語言輸入一個(gè)字符串的方法有哪些的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • C語言的隨機(jī)數(shù)rand()函數(shù)詳解

    C語言的隨機(jī)數(shù)rand()函數(shù)詳解

    這篇文章主要為大家詳細(xì)介紹了C語言的隨機(jī)數(shù)rand()函數(shù),使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C++ 中

    C++ 中"priority_queue" 優(yōu)先級(jí)隊(duì)列實(shí)例詳解

    這篇文章主要介紹了C++ 中"priority_queue" 優(yōu)先級(jí)隊(duì)列實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • VC實(shí)現(xiàn)的病毒專殺工具完整實(shí)例

    VC實(shí)現(xiàn)的病毒專殺工具完整實(shí)例

    這篇文章主要介紹了VC實(shí)現(xiàn)的病毒專殺工具完整實(shí)例,詳細(xì)講述了針對(duì)病毒的進(jìn)程終止、刪除文件及回復(fù)注冊(cè)表與啟動(dòng)項(xiàng)等,同時(shí)介紹了與之相關(guān)的系統(tǒng)函數(shù),非常具有參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C語言實(shí)現(xiàn)三子棋小游戲詳解

    C語言實(shí)現(xiàn)三子棋小游戲詳解

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)三子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11

最新評(píng)論