实现类成员函数

///stock00.h
#ifndef STOCK00_H_INCLUDED
#define STOCK00_H_INCLUDED

#include<string>
class Stock
{
private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot()
    {
        total_val = shares * share_val;
    }
public:
    void acquire(const std::string & co, long n, double pr);
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    void show();

};

#endif // STOCK00_H_INCLUDED

</pre><pre name="code" class="cpp">///OOP方法: 将数据和方法 封装到对象中
#include <iostream>
#include"stock00.h"
using namespace std;

void Stock::acquire(const string & co, long n, double pr)
{
    company = co;
    if(n<0)
    {
        cout <<"Number of shares can't be negative;"
            << company << "shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}
void Stock::buy(long num, double price)
{
    if(num<0)
    {
        cout <<"Number of shares purchased can't be negative;"
            << "Transaction is aborted .\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}
void Stock::sell(long num, double price)
{
    if(num<0)
    {
        cout << "Numbers of shares sold can't be negative;"
             << "Transaction is aborted.\n";
    }
    else if (num > shares)
    {
        cout <<"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()
{
    cout <<"Company: " << company
        << " Shares: " << shares << "\n"
        << "Shares Price: $ " << share_val
        << " Total Worth: $ " <<total_val
        << endl;
}*/
void Stock::show()
{
    using std::cout;
    using std::ios_base;
    ///set format to #.###
    ios_base::fmtflags orig =
        cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);
    cout <<"Company: " << company
        << " Shares: " << shares << "\n"
        << "Shares Price: $ " << share_val;
    ///set format to #.##
    cout.precision(2);
    cout << " Total Worth: $ " <<total_val<< endl;
    ///restore original format
    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);
}
int main()
{
    Stock fluffy_the_cat;
    fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
    fluffy_the_cat.show();
    fluffy_the_cat.buy(15,18.125);
    fluffy_the_cat.show();
    fluffy_the_cat.sell(400, 20.00);
    fluffy_the_cat.show();
    fluffy_the_cat.buy(300000, 40.125);
    fluffy_the_cat.show();
    fluffy_the_cat.sell(300000,0.125);
    fluffy_the_cat.show();
    fluffy_the_cat.update(10.00);
    fluffy_the_cat.show();
    return 0;
}

/*
Company: NanoSmart Shares: 20
Shares Price: $ 12.5 Total Worth: $ 250
Company: NanoSmart Shares: 35
Shares Price: $ 18.125 Total Worth: $ 634.375
You can't sell more than you have; Transaction is aborted.
Company: NanoSmart Shares: 35
Shares Price: $ 18.125 Total Worth: $ 634.375
Company: NanoSmart Shares: 300035
Shares Price: $ 40.125 Total Worth: $ 1.20389e+007
Company: NanoSmart Shares: 35
Shares Price: $ 0.125 Total Worth: $ 4.375
Company: NanoSmart Shares: 35
Shares Price: $ 10 Total Worth: $ 350

Process returned 0 (0x0)   execution time : 0.663 s
Press any key to continue.

*/

///share 还是整数 是因为它是长整型的
///streamsize可以设置输出流的位数..
///cout.precision()就是返回当前输出流的位数,,
///streamsize prec = cout.precision();
///就是保存当前流的位数到prec中.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值