-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cost.cpp
117 lines (105 loc) · 3.42 KB
/
Cost.cpp
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
//
// Cost.cpp
//
// Pathfinder, an optimal path finding program for graphs with
// multi-weighted edges under specified constraints.
//
// Copyright (c) 2013 Bradley Denby.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see {http://www.gnu.org/licenses/}.
//
#include "Cost.h"
/*
* DEFAULT_COST = 0.0
*/
const double Cost::DEFAULT_COST = 0.0;
/*
* DEFAULT_UNITS = "none"
*/
const std::string Cost::DEFAULT_UNITS = "none";
/*
* A pointer to the same Cost object constructed from the default
* constructor is available to all classes that include Cost.h. This
* object is known as the INVALID_COST
*/
const Cost* const INVALID_COST = new Cost();
/*
* The Cost constructor sets the __cost field to cost and the __units
* field to units. It uses fast instiation for only one trip to memory.
*/
Cost::Cost(const double cost, const std::string& units) : __cost(cost), __units(units) {}
/*
* The Cost copy constructor sets the __cost field and the __units field
* to the values of the passed Cost object. It uses fast instiation.
*/
Cost::Cost(const Cost::Cost& cost) : __cost(cost.getCost()), __units(cost.getUnits()) {}
/*
* There is nothing to delete in the Cost class. The only time new is
* called is to instantiate the static INVALID_COST, and INVALID_COST
* should not be deleted when a Cost object is deleted.
*/
Cost::~Cost() {}
/*
* Returns the double __cost of the Cost object. Cannot modify the Cost
* object.
*/
double Cost::getCost() const {
return __cost;
}
/*
* Return the string __units of the Cost object. Cannot modify the Cost
* object.
*/
std::string Cost::getUnits() const {
return __units;
}
const Cost* Cost::clone() const {
if (this != INVALID_COST) {
return new Cost(*this);
}
else {
return this;
}
}
bool Cost::operator==(const Cost& cost) const {
return ((this->getCost() == cost.getCost())
&& (this->getUnits() == cost.getUnits()));
}
/*
* The Cost class is not virtual and non-trivial Cost object may be
* constructed. However, the overloaded operator * automatically
* returns INVALID_COST regardless of the Cost objects being operated
* on. Cannot modifiy the Cost object.
*/
const Cost* Cost::operator*(const Cost& cost) const {
return INVALID_COST;
}
/*
* Returns a formatted string that represents the Cost object calling
* toString. Cannot modify the Cost object.
*/
std::string Cost::toString() const {
std::stringstream ss;
ss << std::fixed << std::showpoint << std::setprecision(4) << this->getCost() << " " << this->getUnits();
return ss.str();
}
/*
* The operator << for sending output to the cout object exists outside
* the Cost class. Allows the use of just the operator << when
* outputting a text representation of a Cost object.
*/
std::ostream& operator<<(std::ostream& output, const Cost& cost) {
output << cost.toString() << std::endl;
return output;
}