找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1887|回复: 0

C++中在用new分配类成员的类在运行阶段程序终止问题,请大家帮忙找找错误原因 ...

[复制链接]
发表于 2009-11-4 01:35:26 | 显示全部楼层 |阅读模式 IP:江苏扬州
//头文件
#ifndef STOCK_H_H_
#define STOCK_H_H_
#include<iostream>
using std::ostream;
using std::istream;
class Stock
{
private:
    char * str;
    int shares;
    int len;
    double share_val;
    double total_val;
    void set_tot(){total_val=shares * share_val;}
public:
    Stock();
    Stock(const 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);

    Stock & operator=(const Stock & s);
   
    Stock & operator=(const char * s);
   
    const Stock & topval(const Stock & s)const;

friend ostream & operator<<(ostream & os,const Stock t);

};


#endif

//源文件
#include<iostream>
#include<cstring>
#include"stock.h"
//using std::ostream;
//using std::istream;
   
Stock::Stock()
{
    len=4;
    str=new char[1];
    str[0]='\0';
    shares=0;
    share_val=0.0;
    total_val=0.0;
   
}
Stock::Stock(const char * co,int n,double pr)
{
    len=strlen(co);
    str=new char[len+1];
    strcpy(str,co);
    shares=n;
    share_val=pr;
    set_tot();
}

Stock & Stock::operator=(const Stock & s)
{
    if(this==&s)
        return *this;
    if(str!=0)
    delete [] str;
    else if(s.len>0)
    {
        len=s.len;
        str=new char[len+1];}
    else
    {std::cerr<<"len < 0 ";
    return *this;
    }
    if(str==0)
    {
        std::cerr<<"memor error!\n";
        return *this;
    }

     
    strcpy(str,s.str);
    shares=s.shares;
    share_val=s.share_val;
    set_tot();
    return *this;
}
   
Stock & Stock::operator=(const char * s)
{
    if(str!=0)
    delete [] str;
    len=strlen(s);
    if(len>0)
    str=new char[len+1];
    else
        return *this;
    if(str==0)
        return *this;

    strcpy(str,s);
   
    return *this;

}

Stock::~Stock()
{
    delete [] str;
}

void Stock::buy(int num,double price)
{
    if(num<0)
    {
        std::cerr<<"Number of shares purchased can't be negative. "
        <<"Transaction is aborted.\n";
    }
    else
    {
   
    shares=shares+num;
    share_val=price;
    set_tot();
    }
}
void Stock::sell(int num,double price)
{
    using std::cerr;
    if(num<0)
    {
        cerr<<"Number of shares purchased can't be negative. "
        <<"Transaction is aborted.\n";
    }
    else if(num>shares)
    {
        cerr<<"You can't sell more than you have! "
            <<"Transaction is aborted.\n";
    }
    else
    {
        sha
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 03:33 , Processed in 0.180410 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表