-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoperator_overloading_1.cpp
115 lines (84 loc) · 2.35 KB
/
operator_overloading_1.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
// Practice session: operator overloading
#include <iostream>
#include <string>
using namespace std;
class PowerUpTime{
private:
double minutes; //<-- notice this is double and params are int below
double hours; //<-- is double to take decimals after the / operator
string name;
public:
PowerUpTime(int hrs = 0, int min = 0, string param = "-"){ //<-- always define
name = param;
hours = hours + (hrs + (min/60)); //<-- added compensation formula
minutes = minutes + (min % 60); //<-- compensation is a min to hour converter
//minutes = min; //<-- old method for simplfication
//hours = hrs; //<-- old method for simplification
}
PowerUpTime operator+(int rhsNum){
PowerUpTime addInt;
addInt.hours = hours + rhsNum;
addInt.minutes = minutes;
return addInt;
}
PowerUpTime operator+(PowerUpTime rhs){
PowerUpTime totalTime;
totalTime.hours = hours + rhs.hours;
totalTime.minutes = minutes + rhs.minutes;
return totalTime;
}
PowerUpTime operator/(PowerUpTime rhs){
PowerUpTime avgTime;
avgTime.hours = (hours + rhs.hours) / 2;
avgTime.minutes = (minutes + rhs.minutes) / 2;
return avgTime;
}
void Print(){
//this will count how many dashes to compensate so that the top and bottom dashes are always equal
int dashes = 0;
for(int i = 0; i < name.size(); i++){
dashes = 22 + i;
}
cout << endl;
cout << "-----------" << name << "-----------" << endl;
cout << "Hours: " << hours << endl;
cout << "minutes: " << minutes << endl;
for(int i = 0; i < dashes + 1; i++){
cout << "-";
}
}
void AddTime(){
char usrAns = '?';
cout << "Add time for " << name << " (y/n): ";
cin >> usrAns;
if((usrAns == 'y') || (usrAns == 'Y')){
int usrHrs = 0;
int usrMin = 0;
cout << "How many more hours: ";
cin >> usrHrs;
cout << "How many more minutes: ";
cin >> usrMin;
hours = hours + usrHrs;
minutes = minutes + usrMin;
}
}
};
//**************************** MAIN ************************************
int main(int argc, char const *argv[]) {
//Hour:Minutes:Name
PowerUpTime goku(1, 70, "Goku");
PowerUpTime vegeta(2, 61, "Vegeta");
PowerUpTime gogeta;
PowerUpTime gohan;
//gohan = goku + 5;
//goku.Print();
//gohan.Print();
//goku.AddTime();
//vegeta.AddTime();
//goku.Print();
//vegeta.Print();
//gogeta = goku / vegeta;
//gogeta.Print();
cout << endl;
return 0;
}