析構函數

析構函數

用構造函數創建對象後,程序負責跟蹤該對象,直到其過期爲止。

對象過期時,程序將自動調用一個特殊的成員函數——析構函數來完成清理工作。

析構函數的聲明

~Share();

析構函數的定義

Share::~Share() {
    cout << "ByeBye!!!" << company << endl;
}

析構函數的調用

析構函數的調用是由編譯器決定的,通常不應該在代碼中顯示地調用析構函數

  1. 如果創建的是靜態存儲類對象,則其析構函數將在程序結束時自動被調用。
  2. 如果創建的是自動存儲類對象,則其析構函數將在程序執行完代碼塊時自動被調用。
  3. 如果對象是通過new創建的,則它將駐留在棧內存或自由存儲中,當使用delete來釋放內存時,其析構函數將自動被調用。

改進的Share類

類的聲明

//
// Created by Administrator on 2017/3/7.
//
#ifndef PROJECT1_SHARE_H
#define PROJECT1_SHARE_H

#include <array>

using namespace std;
class Share {
private:
    string company;
    long shares;
    double share_value;
    double total_value;
    void set_total(){
            total_value = shares * share_value;
    }
public:
    Share();//默認構造函數
    Share(const string &comp , long number , double price);//構造函數
    ~Share();//析構函數
    void buy(long number , double price);
    void sell(long number , double price);
    void update(double price);
    void show();
};


#endif //PROJECT1_SHARE_H

類的定義

//
// Created by Administrator on 2017/3/7.
//

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

Share::Share(const string &comp, long number, double price) {
    company = comp;
    if (number < 0){
        cout << "The number of shares purchased can not be less than zero, the transaction canceled!" << endl;
        shares = 0;
    } else
        shares = number;
    share_value = price;
    set_total();
}

Share::Share() {

}

Share::~Share() {
    cout << "ByeBye!!!" << company << endl;
}


void Share::buy(long number, double price) {
    if (number < 0){
        cout << "The number of shares purchased can not be less than zero, the transaction canceled!" << endl;
    } else{
        shares += number;
        share_value = price;
        set_total();
    }
}

void Share::sell(long number, double price) {
    if (number < 0){
        cout << "The number of shares sold can not be less than zero, the transaction canceled!" << endl;
    } else if (number > shares){
        cout << "The number of shares sold can not be greater than the number of existing shares, the transaction canceled!" << endl;
    } else{
        shares -= number;
        share_value = price;
        set_total();
    }
}

void Share::update(double price) {
    share_value = price;
    set_total();
}

void Share::show() {
    cout << "Company Name: " << company << endl;
    cout << "Number of shares held:" << shares << endl;
    cout << "Stock price:" << share_value << endl;
    cout << "Total stock:" << total_value << endl;
}

類的使用

#include <iostream>
#include <string>
#include "cmake-build-debug/Share.h"

using namespace std;

int main() {
    cout << "Using constructors to create new object : " << endl;
    Share xiong1("alibabab" , 1000 , 18.88);
    xiong1.show();
    Share xiong2 = Share("LENOVO" , 1000 , 9.88);
    xiong2.show();

    cout << "Assigning xiong1 to xiong2 : " << endl;
    xiong2 = xiong1;
    xiong1.show();
    xiong2.show();

    cout << "Using constructors to reset an object : " << endl;
    xiong1 = Share("JD" , 10000 , 6.4);
    xiong1.show();

    return 0;
}

測試結果

H:\Project1\cmake-build-debug\Project1.exe
Using constructors to create new object :
Company Name: alibabab
Number of shares held:1000
Stock price:18.88
Total stock:18880
Company Name: LENOVO
Number of shares held:1000
Stock price:9.88
Total stock:9880
Assigning xiong1 to xiong2 :
Company Name: alibabab
Number of shares held:1000
Stock price:18.88
Total stock:18880
Company Name: alibabab
Number of shares held:1000
Stock price:18.88
Total stock:18880
Using constructors to reset an object :
ByeBye!!!JD
Company Name: JD
Number of shares held:10000
Stock price:6.4
Total stock:64000
ByeBye!!!alibabab
ByeBye!!!JD

Process finished with exit code 0

從中我們可以看出析構函數調用的一些規則!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章