实现类成员函数

时间:2022-09-07 23:30:50
///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中.