![]() |
#2
debroa7232009-08-05 19:51
|

//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
#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 "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;
}
为何这个程序在最后不显示析构函数中的内容,另外请问下对于此调用应该在何处调用析构函数
求教,谢谢