Skip to content

Commit c4c94bb

Browse files
committed
OOP2 Lab4 updated
1 parent 44b6440 commit c4c94bb

File tree

3 files changed

+139
-65
lines changed

3 files changed

+139
-65
lines changed
-15 KB
Binary file not shown.

OOP2_Lab4/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Practical Lab Assignment - Operator Overloading
2+
3+
### Program 1
4+
Define a class DayTime:
5+
```
6+
private:
7+
int hour, minute, second;
8+
public:
9+
• parameterized constructor to initialize value
10+
• int getHour() const{ return hour; }
11+
• int getMinute() const { return minute; }
12+
• int getSecond() const { return second; }
13+
• int asSeconds() const // Daytime in seconds
14+
• overload increment operator to increment the value of seconds.
15+
• Overload decrement operator to decrement the value of minutes.
16+
```
17+
Write a menu driven program and create following menu options:
18+
1. To Display Time.
19+
2. To Display Time in Seconds.
20+
3. To Increment seconds.
21+
4. To decrements minutes.
22+
0. To exit.
23+
24+
### PRogram 2
25+
Define a class Dollar:
26+
```
27+
private:
28+
float currency, mktrate, offrate
29+
public:
30+
• float getDollar() { return currency in dollar }
31+
• float getMarketSoums() { return currency in soums }
32+
• float getofficialSoums() { return currency in soums }
33+
• void setRates() { // input current date market and official rates }
34+
• overload operator "<<" to print the details of a Dollar.
35+
```
36+
Inside main declare one object and show the results similar to Program 1.

OOP2_Lab4/main1.cpp

Lines changed: 103 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#include <iostream>
22
#include <string>
3+
34
using namespace std;
4-
class DayTime {
5+
6+
class DayTime
7+
{
58
private:
69
int hour, minute, second;
10+
711
public:
8-
DayTime() {
12+
DayTime()
13+
{
914
hour = 0;
1015
minute = 0;
1116
second = 0;
@@ -17,64 +22,81 @@ class DayTime {
1722
minute = m;
1823
second = s;
1924
}
20-
int getHour() const {
25+
int getHour() const
26+
{
2127
return hour;
2228
}
23-
int getMinute() const {
29+
int getMinute() const
30+
{
2431
return minute;
2532
}
26-
int getSecond() const {
33+
int getSecond() const
34+
{
2735
return second;
2836
}
29-
30-
void DisplayTime() {
31-
cout << "HH: " << hour << endl << "MM: " << minute << endl << "SS: " << second << endl;
37+
38+
void DisplayTime()
39+
{
40+
cout << "HH: " << hour << endl
41+
<< "MM: " << minute << endl
42+
<< "SS: " << second << endl;
3243
}
33-
int asSeconds() const {
44+
int asSeconds() const
45+
{
3446
return (3600 * hour + 60 * minute + second);
3547
}
36-
37-
friend void operator <<(ostream& out, DayTime& h);
38-
friend void operator >>(istream& in, DayTime& h);
39-
friend void operator ++(DayTime &MainDayTime);
40-
friend void operator --(DayTime& MainDayTime2);
48+
49+
friend void operator<<(ostream &out, DayTime &h);
50+
friend void operator>>(istream &in, DayTime &h);
51+
friend void operator++(DayTime &MainDayTime);
52+
friend void operator--(DayTime &MainDayTime2);
4153
};
4254

43-
void operator >> (istream& in, DayTime& h) {
44-
cout << "Input hours ( 0 , 24 ): ";
55+
void operator>>(istream &in, DayTime &h)
56+
{
57+
cout << "Input hours ( 0 , 24 ): ";
4558
in >> h.hour;
4659

47-
cout << "Input minutes (0 , 60 ): ";
60+
cout << "Input minutes (0 , 60 ): ";
4861
in >> h.minute;
4962

5063
cout << "Input seconds ( 0 ,60 ): ";
5164
in >> h.second;
5265
}
5366

54-
void operator << (ostream& out, DayTime& h) {
55-
out << "HH: " << h.hour << endl << "MM: " << h.minute << endl << "SS: " << h.second << endl;
67+
void operator<<(ostream &out, DayTime &h)
68+
{
69+
out << "HH: " << h.hour << endl
70+
<< "MM: " << h.minute << endl
71+
<< "SS: " << h.second << endl;
5672
}
57-
void operator ++ (DayTime& MainDayTime) {
73+
void operator++(DayTime &MainDayTime)
74+
{
5875
MainDayTime.second++;
59-
if (MainDayTime.second >= 60) {
76+
if (MainDayTime.second >= 60)
77+
{
6078
MainDayTime.second = 0;
6179
MainDayTime.minute++;
6280
}
63-
if (MainDayTime.minute >= 60) {
81+
if (MainDayTime.minute >= 60)
82+
{
6483
MainDayTime.minute = 0;
6584
MainDayTime.hour++;
6685
}
6786
}
68-
void operator -- (DayTime& MainDayTime2) {
87+
void operator--(DayTime &MainDayTime2)
88+
{
6989
MainDayTime2.minute--;
70-
if (MainDayTime2.minute < 0) {
90+
if (MainDayTime2.minute < 0)
91+
{
7192
MainDayTime2.minute = 59;
7293
MainDayTime2.hour--;
7394
}
7495
}
7596

7697
// Class 'Dollar' for the program 2
77-
class Dollar {
98+
class Dollar
99+
{
78100
private:
79101
float currency, mktrate, offrate;
80102

@@ -85,34 +107,37 @@ class Dollar {
85107
mktrate = 9000;
86108
offrate = 7000;
87109
}
88-
float getDollar()
110+
float getDollar()
89111
{
90-
cout << endl<< "Currency: " << endl;
112+
cout << endl
113+
<< "Currency: " << endl;
91114
return currency;
92115
}
93-
float getMarketSoums()
116+
float getMarketSoums()
94117
{
95118
cout << "Markent currency: " << endl;
96-
return currency*mktrate;
119+
return currency * mktrate;
97120
}
98-
float getofficialSoums() {
121+
float getofficialSoums()
122+
{
99123
cout << "Official currency:" << endl;
100-
return currency*offrate;
124+
return currency * offrate;
101125
}
102-
void setRates()
126+
void setRates()
103127
{
104128
cout << "Enter current market rate: " << endl;
105129
cin >> mktrate;
106130
cout << "Enter current official rate: " << endl;
107131
cin >> offrate;
108132
}
109-
friend void operator<< (ostream &output, Dollar &p);
133+
friend void operator<<(ostream &output, Dollar &p);
110134
};
111135

112-
void operator << (ostream &output, Dollar &p)
136+
void operator<<(ostream &output, Dollar &p)
113137
{
114138
output << "Details of a dollar " << endl;
115-
output << "Currency is " << p.currency <<endl<< "Market rate is " << p.mktrate<<endl;
139+
output << "Currency is " << p.currency << endl
140+
<< "Market rate is " << p.mktrate << endl;
116141
output << "Official rate is " << p.offrate << endl;
117142
}
118143

@@ -131,40 +156,45 @@ int main()
131156
switch (choice)
132157
{
133158
// for program 1
134-
case 1: {
159+
case 1:
160+
{
135161
system("cls");
136162
int choice2;
137-
163+
138164
DayTime h1(10, 10, 10);
139165
DayTime h2;
140166
cin >> h2;
141-
167+
142168
showChoicesforMainMenu2();
143169

144-
do {
170+
do
171+
{
145172
cin >> choice2;
146173
switch (choice2)
147174
{
148175
case 1:
149176
cout << h2;
150-
cout << endl << endl;
177+
cout << endl
178+
<< endl;
151179
cout << "Enter your choice: ";
152180
break;
153181
case 2:
154-
cout << h2.asSeconds() <<endl;
182+
cout << h2.asSeconds() << endl;
155183
cout << "Enter your choice: ";
156184
break;
157185
case 3:
158186
++h2;
159-
h2.DisplayTime();
160-
cout << h2.asSeconds();
161-
cout << endl << endl;
187+
h2.DisplayTime();
188+
cout << h2.asSeconds();
189+
cout << endl
190+
<< endl;
162191
cout << "Enter your choice: ";
163192
break;
164193
case 4:
165194
--h2;
166195
h2.DisplayTime();
167-
cout << endl << endl;
196+
cout << endl
197+
<< endl;
168198
cout << "Enter your choice: ";
169199
break;
170200
case 5:
@@ -174,33 +204,37 @@ int main()
174204
default:
175205
system("cls");
176206
cout << "Invalid input! Try again." << endl;
177-
178207
}
179208

180209
} while (choice2 != 5);
181210
break;
182211
}
183212
// for program 2
184-
case 2: {
213+
case 2:
214+
{
185215
system("cls");
186216
cout << "\t[2] Currency converter" << endl;
187217
Dollar money;
188-
cout << money.getDollar() << endl;
189-
cout << money.getMarketSoums() << endl;
190-
cout << money.getofficialSoums() << endl << endl;
191-
money.setRates();
192-
cout << money.getDollar() << endl;
193-
cout << money.getMarketSoums() << endl;
194-
cout << money.getofficialSoums() << endl;
195-
cout << "Overloading: " << endl << endl;
196-
197-
cout << money;
198-
218+
cout << money.getDollar() << endl;
219+
cout << money.getMarketSoums() << endl;
220+
cout << money.getofficialSoums() << endl
221+
<< endl;
222+
money.setRates();
223+
cout << money.getDollar() << endl;
224+
cout << money.getMarketSoums() << endl;
225+
cout << money.getofficialSoums() << endl;
226+
cout << "Overloading: " << endl
227+
<< endl;
228+
229+
cout << money;
230+
}
231+
break;
232+
233+
case 3:
234+
{
199235
}
200-
break;
236+
break;
201237

202-
case 3: {}
203-
break;
204238
default:
205239
cout << "Invalid input! Try again." << endl;
206240
}
@@ -209,22 +243,26 @@ int main()
209243
return 0;
210244
}
211245

212-
void showChoicesforMainMenu(){
246+
void showChoicesforMainMenu()
247+
{
213248

214-
cout << endl << endl;
249+
cout << endl
250+
<< endl;
215251
cout << "\t\t\t M A I N M E N U" << endl;
216252
cout << "\t\t\t1: Day Time " << endl;
217253
cout << "\t\t\t2: Dollar & Soums" << endl;
218254
cout << "\t\t\t3: Exit " << endl;
219255
cout << "\t\t\tEnter your choice : ";
220256
}
221-
void showChoicesforMainMenu2() {
257+
258+
void showChoicesforMainMenu2()
259+
{
222260
cout << "\t[1] First program" << endl;
223261
cout << "\t\t\t M A I N M E N U 2" << endl;
224262
cout << "\t\t\t1. To Display Time." << endl;
225263
cout << "\t\t\t2. To Display Time in Seconds." << endl;
226264
cout << "\t\t\t3. To Increment seconds." << endl;
227265
cout << "\t\t\t4. To decrements minutes." << endl;
228266
cout << "\t\t\t5. Exit." << endl;
229-
cout << "\t\t\tEnter your choise : ";
267+
cout << "\t\t\tEnter your choice : ";
230268
}

0 commit comments

Comments
 (0)