析构函数
用构造函数创建对象后,程序负责跟踪该对象,直到其过期为止。
对象过期时,程序将自动调用一个特殊的成员函数——析构函数来完成清理工作。
析构函数的声明
~
Share();
析构函数的定义
Share::~
Share()
{
cout << "ByeBye!!!" << company << endl;
}
析构函数的调用
析构函数的调用是由编译器决定的,通常不应该在代码中显示地调用析构函数
如果创建的是静态存储类对象,则其析构函数将在程序结束时自动被调用。如果创建的是自动存储类对象,则其析构函数将在程序执行完代码块时自动被调用。如果对象是通过new创建的,则它将驻留在栈内存或自由存储中,当使用delete来释放内存时,其析构函数将自动被调用。
改进的Share类
类的声明
#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
类的定义
#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
从中我们可以看出析构函数调用的一些规则!
转载请注明原文地址: https://ju.6miu.com/read-10941.html