TinanBao 2016-09-23 02:52 采纳率: 100%
浏览 1544
已采纳

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 2016-09-23 03:02
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?