| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 401 人关注过本帖
标题:关于析构函数的调用
只看楼主 加入收藏
bryantism
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-8-5
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
关于析构函数的调用
程序代码:
//stock1.h--------stock class declaration with constructions destructior added
#ifndef STOCK1_H_
#define STOCK1_H_



class Stock
{
private:
    char company[30];
    int shares;
    double share_val;
    double total_val;
    void set_tol()
    {
        total_val=share_val*shares;
    }
public:
    Stock();
    Stock(char *co,int n=0,double pr=0.0);
    ~Stock();
    void buy(int num,double price);
    void sell(int num,double price);
    void update(double price);
    void show()const;
    const Stock &topval(const Stock &s)const;
};

#endif



程序代码:
#include <iostream>
#include "stock.h"



//constructors
Stock::Stock()
{
    std::cout<<"Default constructor called"<<std::endl;
    std::strcpy(company,"no name");
    shares=0;
    share_val=0.0;
    total_val=0.0;
}

Stock::Stock(char *co,int n,double pr)
{
    std::cout<<"Constructor using "<<co<<" called"<<std::endl;
    std::strncpy(company,co,29);
    company[29]='\0';
    if(n<0)
    {
        std::cerr<<"Number of shares can't be negative. "<<company<<" shares set to 0"<<std::endl;
        shares=0;
    }
    else
    {
        shares=n;
    }
        share_val=pr;
        set_tol();
}

//destructor
Stock::~Stock()
{
    std::cout<<" Bye, "<<company<<"!"<<std::endl;
    system("pause");
}

void Stock::buy(int num,double price)
{
    if(num<0)
    {
        std::cerr<<"Numbers of shares purchased can't be negtive. Transaction is aborted. "<<std::endl;
    }
    else
    {
        shares+=num;
        share_val=price;
        set_tol();
    }
}

void Stock::sell(int num,double price)
{
    if(num<0)
    {
        std::cerr<<"Number of shares to be sold can't be negative. Transaction is aborted. "<<std::endl;
    }
    else if(num>shares)
    {
        std::cerr<<"you can't sell more than you have! Transaction is aborted. "<<std::endl;
    }
    else
    {
        shares-=num;
        share_val=price;
        set_tol();
    }
}

void Stock::update(double price)
{
    share_val=price;
    set_tol();
}

void Stock::show()const
{
    std::cout<<"Company: "<<company<<" Shares: "<<shares<<std::endl;
    std::cout<<"Share Price: $"<<share_val<<" Total worth: $"<<total_val<<std::endl;
}

const Stock &Stock::topval(const Stock &s)const
{
      if(s.total_val>total_val)
                  return s;
      else
                  return *this;
}





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

const int STKS=4;
int main()
{
    using std::cout;
    using std::ios_base;
    using std::endl;
   
    Stock stocks[STKS]={
          Stock("NanoSmart",12,20.0),
          Stock("Bofffss",300,2.0),
          Stock("qiodfd",323,2.6),
          Stock("wewe",212,3.2)
          };
          cout.precision(2);
          cout.setf(ios_base::fixed,ios_base::floatfield);
          cout.setf(ios_base::showpoint);
         
          cout<<"Stock holding:"<<endl;
          int st;
          for(st=0;st<STKS;st++)
                stocks[st].show();
          Stock top=stocks[0];
          for(st=1;st<STKS;st++)
                  top=top.topval(stocks[st]);
          cout<<"\n Most valuable hoding:";
          top.show();
          system("pause");
  
    return 0;
}

" border="0" />

为何这个程序在最后不显示析构函数中的内容,另外请问下对于此调用应该在何处调用析构函数

求教,谢谢
搜索更多相关主题的帖子: 函数 
2009-08-05 13:09
debroa723
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:23
帖 子:862
专家分:1954
注 册:2008-10-12
收藏
得分:14 
析构不需要程序员手动调用,系统自动调用,如果你按下任意键来结束程序,析构会被调到,只是太快你看不到。
如果想看到,把
Stock stocks[STKS]={
          Stock("NanoSmart",12,20.0),
          Stock("Bofffss",300,2.0),
          Stock("qiodfd",323,2.6),
          Stock("wewe",212,3.2)
          };
          cout.precision(2);
          cout.setf(ios_base::fixed,ios_base::floatfield);
          cout.setf(ios_base::showpoint);
           
          cout<<"Stock holding:"<<endl;
          int st;
          for(st=0;st<STKS;st++)
                stocks[st].show();
          Stock top=stocks[0];
          for(st=1;st<STKS;st++)
                  top=top.topval(stocks[st]);
          cout<<"\n Most valuable hoding:";
          top.show();
写到一个函数里,在main函数里调用这个函数,就可以看到析构的输出。
2009-08-05 19:51
bryantism
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-8-5
收藏
得分:0 
还是看不到对这个数组的析构
2009-08-07 08:43
快速回复:关于析构函数的调用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058063 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved