An Array of Objects

Example program:
头文件:
 
  

// stock2.h -- augmented version
#ifndef STOCK2_H_
#define STOCK2_H_
class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock(); // default constructor
Stock(const char * co, int n = 0, double pr = 0.0);
~Stock(); // do-nothing destructor
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


cpp部分:
 
  

// stock2.cpp -- improved version
#include <iostream>
#include <cstring>
#include "stock2.h"

const int STKS = 4;

// constructors
Stock::Stock() // default constructor
{
std::strcpy(company, "no name");
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const char * co, int n, double pr)
{
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.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
// class destructor
Stock::~Stock() // quiet class destructor
{
}

// other methods
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 += num;
share_val = price;
set_tot();
}
}

void Stock::sell(int num, double price)
{
using std::cerr;
if (num < 0)
{
cerr << "Number of shares sold 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
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show() const
{
using std::cout;
using std::endl;
cout << "Company: " <<company
<< " Shares: " <<shares << endl
<< " Share Price:$" << share_val
<< " Total Worth:$" << total_val << endl;
}


const Stock & Stock::topval(const Stock & s) const
{
using std::cout;
using std::endl;
cout<<"s.total_val="<<s.total_val<<endl;
cout<<" total_val="<<total_val<<endl;

if (s.total_val > total_val)
return s;
else
return *this;
}

int main()
{
using std::cout;
using std::ios_base;
// create an array of initialized objects
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.0),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Obelisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};
cout.precision(2); // #.## format
cout.setf(ios_base::fixed, ios_base::floatfield); // #.## format
cout.setf(ios_base::showpoint); // #.## format
cout << "Stock holdings:\n";
int st;
for (st = 0; st < STKS; st++)
stocks[st].show();
Stock top = stocks[0];
using std::cout;
using std::endl;
cout<<"start print"<<endl;
for (st = 1; st < STKS; st++)
{
top = top.topval(stocks[st]);
top.show();
}
cout << "\nMost valuable holding:\n";
top.show();
return 0;
}




运行结果:
 
  

Stock holdings:
Company: NanoSmart Shares: 12
Share Price:$20.00 Total Worth:$240.00
Company: Boffo Objects Shares: 200
Share Price:$2.00 Total Worth:$400.00
Company: Monolithic Obelisks Shares: 130
Share Price:$3.25 Total Worth:$422.50
Company: Fleep Enterprises Shares: 60
Share Price:$6.50 Total Worth:$390.00
start print
s.total_val=400.00
total_val=240.00
Company: Boffo Objects Shares: 200
Share Price:$2.00 Total Worth:$400.00
s.total_val=422.50
total_val=400.00
Company: Monolithic Obelisks Shares: 130
Share Price:$3.25 Total Worth:$422.50
s.total_val=390.00
total_val=422.50
Company: Monolithic Obelisks Shares: 130
Share Price:$3.25 Total Worth:$422.50

Most valuable holding:
Company: Monolithic Obelisks Shares: 130
Share Price:$3.25 Total Worth:$422.50



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值