在異常處理中處理析構(gòu)函數(shù)

例1:在異常處理中處理析構(gòu)函數(shù)。

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),成都創(chuàng)新互聯(lián)面向各種領(lǐng)域:成都塔吊租賃成都網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷推廣解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。


程序:

#include<iostream>

#include<string>

using namespace std;

class Student

{

public:

Student(int n, string nam)//定義構(gòu)造函數(shù)

{

cout << "constructor-" << n << endl;

num = n;

name = nam;

}

~Student()//定義析構(gòu)函數(shù)

{

cout << "destructor-" << num << endl;

}

void get_data();

private:

int num;

string name;

};

void Student::get_data()

{

if (num == 0)//如果num=0,拋出int型變量num

{

throw num;

}

else//如果num不等于0,輸出num,name

{

cout << num << " " << name << endl;

}

cout << "in get_data()" << endl;

}

void fun()

{

Student stud1(1101, "tan");

stud1.get_data();

Student stud2(0, "li");

stud2.get_data();

}

int main()

{

cout << "main begin" << endl;//表示主函數(shù)開始了

cout << "call fun()" << endl;//調(diào)用fun()函數(shù)

try

{

fun();

}

catch (int n)

{

cout << "num=" << n << ",error!" << endl;//num=0出錯(cuò)

}

cout << "main end" << endl;//表示主函數(shù)結(jié)束

system("pause");

return 0;

}

程序分析:

在異常處理中處理析構(gòu)函數(shù)在異常處理中處理析構(gòu)函數(shù)

main begin

call fun()

constructor-1101

1101 tan

in get_data()

constructor-0

destructor-0

destructor-1101

num=0,error!

main end

請(qǐng)按任意鍵繼續(xù). . .

例2:在上題的基礎(chǔ)上進(jìn)行變形,分析執(zhí)行過程,由于異常處理調(diào)用了哪些析構(gòu)函數(shù)。

程序:

#include<iostream>

#include<string>

using namespace std;

class Student

{

public:

Student(int n, string nam)//定義構(gòu)造函數(shù)

{

cout << "constructor-" << n << endl;

num = n;

name = nam;

}

~Student()//定義析構(gòu)函數(shù)

{

cout << "destructor-" << num << endl;

}

void get_data();

private:

int num;

string name;

};

void Student::get_data()

{

if (num == 0)//如果num=0,拋出int型變量num

{

throw num;

}

else//如果num不等于0,輸出num,name

{

cout << num << " " << name << endl;

}

cout << "in get_data()" << endl;

}

void fun()

{

Student stud1(1101, "tan");

stud1.get_data();

try

{

Student stud2(0, "li");

stud2.get_data();

}

catch (int n)

{

cout << "num=" << n << ",error!" << endl;//num=0出錯(cuò)

}

}

int main()

{

cout << "main begin" << endl;//表示主函數(shù)開始了

cout << "call fun()" << endl;//調(diào)用fun()函數(shù)

fun();

cout << "main end" << endl;//表示主函數(shù)結(jié)束

system("pause");

return 0;

}

程序分析:和上題的不同之處在與,本題在輸出“destructor-0”后,再執(zhí)行catch語句,輸出“num=0,error!”,fun函數(shù)執(zhí)行完畢,在流程轉(zhuǎn)回main函數(shù)之前先調(diào)用stud1的析構(gòu)函數(shù),輸出“destructor-1101”,最后執(zhí)行main函數(shù)中最后一行cout語句,輸出“main end”。

運(yùn)行結(jié)果:

main begin

call fun()

constructor-1101

1101 tan

in get_data()

constructor-0

destructor-0

num=0,error!

destructor-1101

main end

請(qǐng)按任意鍵繼續(xù). . .

文章標(biāo)題:在異常處理中處理析構(gòu)函數(shù)
轉(zhuǎn)載來于:http://muchs.cn/article48/iehoep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、響應(yīng)式網(wǎng)站網(wǎng)站排名、域名注冊(cè)、搜索引擎優(yōu)化、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司