2 tinanbao TinanBao 于 2016.09.23 10:52 提问

c++析构函数不调用,第一行cout也不输出。

头文件

 #ifdef STOCK10_H_
#define STOCK10_H_
#endif
#include <string>
class Stock
{
public:
    Stock();
    Stock(const std::string & co,long n=0,double pr=0);
    void buy(long number,double price);
    void sell(long num,double price);
    void update(double price);
    void show();
    ~Stock();

private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot(){total_val=shares*share_val;}
};

 #include "stdafx.h"
#include "stock10.h"
#include <iostream>
Stock::Stock()
{
    std::cout<<"Default constructor called "<<std::endl;
    company ="no name ";
    shares=0;
    share_val=0;
    total_val=0;
}
Stock::Stock(const std::string &co,long n, double pr)
{
    std::cout<<"Constructor using "<<co<<" called"<<std::endl;
    company=co;
    if (n<0)
    {
        std::cout<<"Number of shares can't be negative;"<<company<<"share set to 0."<<std::endl;
        shares=0;
    }
    else
    {
        shares=n;
    }
    share_val=pr;
    set_tot();
}
Stock::~Stock()
{
    std::cout<<"Bye, "<<company<<" !"<<std::endl;
}

void Stock::buy(long num,double pr)
{
    if (num<0)
    {
        std::cout<<"Number of shares purchased can't be negative. Transaction is aborted"<<std::endl;
    } 
    else
    {
        shares+=num;
        share_val=pr;
        set_tot();
    }
}
void Stock::sell(long num,double pr)
{
    using std::cout;
    using std::endl;
    if(num<0)
    {
        cout<<"Number of shares sold can't be negative.Transaction is aborted"<<endl;
    }else if(num>shares)
    {
        cout<<"You cant sell more than you have!Transaction is aborted."<<endl; 
    }
    else
    {
        shares-=num;
        share_val=pr;
        set_tot();
    }
}

void Stock::update(double pr)
{
    share_val=pr;
    set_tot();
}
void Stock::show()
{
    using std::cout;
    using std::ios_base;
    ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);
    std::streamsize prec=cout.precision(3);
    cout<<"Company: "<<company<<"Shares: "<<shares<<std::endl;
    cout<<"Share Price:$ "<<share_val<<std::endl;
    cout.precision(2);
    cout<<"Total Worth:$ "<<total_val<<std::endl;
    cout.setf(orig,ios_base::floatfield);
    cout.precision(prec);
}

#include "stdafx.h"
#include <iostream>
#include "stock10.h"
int _tmain(int argc, _TCHAR* argv[])
{
    {
        using std::cout;
        cout<<"Using constructors to create new object "<<std::endl;
        Stock stock1("NanoSmart ",12, 20.0);
        stock1.show();
        Stock stock2=Stock("Boffo Objects ",2,2.0);
        stock2.show();

        cout<<"Assigning stock1 to stock2: "<<std::endl;
        stock2=stock1;
        cout<<"Listing stock1 and stock2:"<<std::endl;
        stock1.show();
        stock2.show();

        cout<<"Using a constructor to reset an object "<<std::endl;
        stock1=Stock("Nifty Foods ",10,50);
        cout<<"Revised stock1: "<<std::endl;
        stock1.show();
        cout<<"Done "<<std::endl;
        std::cin.get();
    }

    return 0;
}

结果main函数中第一行

        cout<<"Using constructors to create new object "<<std::endl;

不执行,而且最后也不调用析构函数,为啥呢??
输出结果如下
图片说明

1个回答

TinanBao
TinanBao   2016.09.23 11:02
已采纳

找到问题了,是因为main函数最后的cin.get();
程序还在等待输入。

Csdn user default icon
上传中...
上传图片
插入图片