-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock.cpp
More file actions
140 lines (129 loc) · 2.71 KB
/
stock.cpp
File metadata and controls
140 lines (129 loc) · 2.71 KB
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
// enable Visual C++ memory leak checking
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#ifdef _DEBUG
#include <ostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
#include <iomanip>
#include "stock.h"
stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate) :
symbol(NULL),
name(NULL),
sharePrice(sharePrice),
priceDate(priceDate)
{
this->symbol = new char[strlen(symbol)+1];
strcpy(this->symbol, symbol);
this->name = new char[strlen(name)+1];
strcpy(this->name, name);
}
stock::stock(const stock& s) :
symbol(NULL),
name(NULL),
sharePrice(s.sharePrice),
priceDate(s.priceDate)
{
symbol = new char[strlen(s.symbol)+1];
strcpy(symbol, s.symbol);
name = new char[strlen(s.name)+1];
strcpy(name, s.name);
}
stock::stock(void) :
symbol(NULL),
name(NULL),
sharePrice(0),
priceDate(date::date())
{
}
char const * const stock::getSymbol(void) const
{
return(symbol);
}
stock& stock::operator=(const stock& s)
{
if (this == &s)
{
return *this;
}
else
{
if (symbol)
{
delete [] symbol;
}
if (name)
{
delete [] name;
}
symbol = new char[strlen(s.symbol)+1];
strcpy(symbol, s.symbol);
name = new char[strlen(s.name)+1];
strcpy(name, s.name);
sharePrice = s.sharePrice;
priceDate = s.priceDate;
return *this;
}
}
stock& stock::operator=(stock const * const s)
{
if (this == s)
{
return *this;
}
else
{
symbol = s->symbol;
name = s->name;
sharePrice = s->sharePrice;
priceDate = s->priceDate;
return *this;
}
}
stock::~stock(void)
{
if (symbol)
{
delete [] symbol;
}
if (name)
{
delete [] name;
}
}
// write the fixed headers to the output stream:
//
// symbol name price date
// ------ ---- ----- ----
void stock::displayHeaders(ostream& out)
{
out << setw(8) << "symbol";
out << setw(40) << "name";
out << setw(8) << "price";
out << "date" << endl;
out << setw(8) << "------";
out << setw(40) << "----";
out << setw(8) << "-----";
out << "----" << endl;
}
// prints share price as DOLLARS
// (e.g. 2483 would print out as 24.83 and 200 would print out as 2.00)
ostream& operator<<(ostream& out, const stock& s)
{
out << setw(8) << s.symbol;
out << setw(40) << s.name;
out << setw(2) << right << s.sharePrice/100 << ".";
if (s.sharePrice%100 >= 10)
{
out << setw(5) << left << s.sharePrice%100;
}
else
{
out << left << "0" << setw(4) << s.sharePrice%100;
}
out << s.priceDate;
return out;
}