-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprice.cpp
More file actions
68 lines (62 loc) · 1.47 KB
/
price.cpp
File metadata and controls
68 lines (62 loc) · 1.47 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
#include "price.h"
#include <iostream>
using namespace std;
Price::Price() //default constructor
{
dollar = 0;
cent = 0;
}
Price::Price(int d, int c) //non-default constructor
{
dollar = d;
cent = c;
}
//this function is an overloading operator. + is overloaded. rhs would be the second values.
Price Price::operator+(const Price& rhs)
{
double Tdollar = this-> dollar + rhs.dollar;
double Tcent = this-> cent + rhs.cent;
if(Tcent >= 100) //if cent is greater than 100
{
Price p(Tdollar+1, Tcent-100);
return p;
}
else //if cent is any number more than or equal to 0 and less than 100
{
Price p(Tdollar, Tcent);
return p;
}
}
//this function has - overloading.
Price Price::operator-(const Price& rhs)
{
double Tdollar = this-> dollar - rhs.dollar;
double Tcent = this-> cent - rhs.cent;
if(Tcent < 0) //if cent is less than 100
{
Price p(Tdollar - 1, Tcent + 100); //makes dollar subtract 1 and adds 100 so it is positive
return p;
}
else
{
Price p(Tdollar, Tcent);
return p;
}
}
bool Price::operator>(const Price& rhs)
{
return(this->dollar > rhs.dollar || this->dollar == rhs.dollar && this->cent > rhs.cent) ? true: false
}
//cout operator
ostream& operator<<(ostream& o, const Price& p)
{
if(p.cent < 10) //if cents are less than 0
{
o << p.dollar << ".0" << p.cent; //so it prints 5.05 instead of 5.5
}
else
{
o << p.dollar << "." << p.cent;
}
return o;
}