1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Stock
{
public:
string sym;
int noShares;
double shareVal;
double totVal;
Stock(); //ctor
Stock(string s, int n, double v);
void setSym(string s);
void buy(int n, double p);
void sell(int n, double p);
void update(double p);
string getSym();
double getVal();
int getnoShares();
void prnt();
Stock operator++();
Stock operator++(int);
Stock operator--();
Stock operator--(int);
private:
void setTot();
};//endclass
void Stock::setSym(string s)
{ s = sym;}
string Stock::getSym()
{return sym;}
double Stock::getVal()
{return shareVal;}
int Stock::getnoShares()
{return noShares;}
Stock::Stock(string s, int n, double v)
{
sym = s;
noShares = n;
shareVal = v;
setTot();
}
void Stock::buy(int n, double p)
{
noShares = n;
shareVal = p;
}
void Stock::sell(int n, double p)
{
noShares = n;
shareVal = p;
}
void Stock::update(double p)
{
shareVal = p;
}
void Stock::prnt()
{
cout << "SYMBOL: " << getSym();
cout << "Number of Shares: " << getnoShares();
cout << "Current Price of Share: " << getVal();
cout << "Total Value of Holding: " << totVal;
}
void Stock::setTot()
{
totVal = noShares * shareVal;
}
Stock Stock::operator++()
{
++noShares;
setTot();
}
Stock Stock::operator--()
{
Stock(sym,noShares, shareVal);
--noShares;
setTot();
return Stock(sym,noShares, shareVal);
}
int main()
{
Stock sally;
Stock bob("TKX", 101, 4.56231);
Stock will("ITK", 71, 7.06319);
cout << fixed << showpoint << setprecision(2);
cout << "\nBob's " << bob.getSym() << " stock is worth $" << bob.getVal() * bob.getnoShares();
cout << "\nWill's " << will.getSym() << " stock is worth $" << will.getVal() * will.getnoShares();
sally.setSym("GBM");
sally.buy(33, 103.65907);
cout << "\nSally's " << sally.getSym() << " stock is worth $" << sally.getVal() * sally.getnoShares();
cout << "\nSally's stock information:";
sally.prnt();
bob.sell(31, 5.42384);
will.buy(117, 7.33288);
cout << "\nAfter selling 31 shares at $5.42384 per share,";
cout << "\nBob's " << bob.getnoShares() << " shares of " << bob.getSym() << " stock are worth $" << bob.getVal() * bob.getnoShares();
cout << "\nAfter buying 117 shares at $7.33288 per share, Will's stock information:";
will.prnt();
bob.sell(222, 1.1); // data validation test
will.buy(-99, 6.64281); // data validation test
cout << "\nTesting overloaded increment and decrement operators:";
(++bob).prnt();
bob++.prnt();
bob.prnt();
(--bob).prnt();
bob--.prnt();
bob.prnt();
return 0;
}//endmain
|