#include #include "./Stock.h" using std::cout; using std::endl; using std::ostream; using std::setw; using std::string; // Constructs a new Stock with the given symbol and current price per share. Stock::Stock(const string &symbol, double share_price) { symbol_ = symbol; share_price_ = share_price; cost_ = 0.0; shares_ = 0; } // Returns this asset's total cost spent on all shares. double Stock::GetCost() const { return cost_; } // Returns the market value of this stock, which is the total number // of shares times the share price. double Stock::GetMarketValue() const { return shares_ * share_price_; } // Returns the profit earned on shares of this asset. double Stock::GetProfit() const { return GetMarketValue() - GetCost(); } // Records a purchase of the given number of shares of stock at the // given price per share. void Stock::Purchase(int shares, double share_price) { shares_ += shares; cost_ += shares * share_price; } // Sets the current share price of this asset. void Stock::set_share_price(double share_price) { share_price_ = share_price; } // Print the stock to the ostream; note this is a non-member function. ostream &operator<<(ostream &out, const Stock &rhs) { out << "Stock (" << rhs.symbol() << "):" << endl; out << " value: $" << setw(8) << rhs.GetMarketValue() << endl; out << " cost: $" << setw(8) << rhs.GetCost() << endl; out << " profit: $" << setw(8) << rhs.GetProfit() << endl; return out; }