-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTask2_UML Class Diagram to Coding.cpp
More file actions
213 lines (175 loc) · 5.01 KB
/
Task2_UML Class Diagram to Coding.cpp
File metadata and controls
213 lines (175 loc) · 5.01 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @file Task2_UML_Class_Diagram_to_Coding.cpp
*
* @brief C++ Program to demostrate Room class.
*
* @author Saif Ullah Ijaz
*
*/
// SYSTEM INCLUDES
#include <iostream>
using namespace std;
// Room class definition
class Room {
public:
// LIFECYCLE
/** Default + Overloaded constructor.
*/
Room(float = 0.0, float = 1.0);
// Use compiler-generated copy constructor, assignment, and destructor.
// Room(const Room&);
// Room& operator=(const Room&);
// ~Room();
// OPERATORS
/** Stream Insertion operator.
*
* @param os Standard Output Stream.
* @param from The value to be inserted to the output stream.
*
* @return A reference to the standard output stream.
*/
friend std::ostream& operator <<(std::ostream& os, const Room& from);
/** Stream Extraction operator.
*
* @param is Standard Intput Stream.
* @param to The value to be extracted from the input stream.
*
* @return A reference to the standard input stream.
*/
friend std::istream& operator >>(std::istream& is, Room& to);
/** Addition operator.
*
* @param addend Constant reference of the addend room to be added.
*
* @return Sum of areas of 2 rooms.
*/
float operator +(const Room& addend)const;
// ACCESS
// setters
void SetWidth(float = 0.0);
void SetLength(float = 0.0);
void SetRoom(float = 0.0, float = 0.0);
/**
# @overload void SetRoom(const Room& aRoom);
*/
void SetRoom(const Room& aRoom);
// getters
float GetWidth() const;
float GetLength() const;
float GetArea() const;
const Room& GetRoom()const;
private:
// DATA MEMBERS
float mWidth;
float mLength;
float mArea;
};
// end class Room
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Room Default+Overloaded Constructor
Room::Room(float aWidth, float aLength) : mWidth(aWidth), mLength(aLength), mArea(aWidth*aLength) {
this->SetRoom(aWidth, aLength);
}
// end Room constructor
//============================= OPERATORS ====================================
// Stream Insertion
ostream& operator <<(ostream& os, const Room& r) {
os << endl;
os << "Width = " << r.GetWidth() << endl;
os << "Length = " << r.GetLength() << endl;
os << "Area = " << r.GetArea() << endl << endl;
return os; // enables cout<<Room1<<Room2;
}
// end stream insertion
// Stream Extraction
istream & operator >> (istream& in, Room& r) {
float aWidth, aLength;
cout << endl;
cout << "Width = ";
in >> aWidth; // input room Width
cout << "Length = ";
in >> aLength; // input room Length
cout << endl;
r.SetRoom(aWidth, aLength);
return in; // enables cin>>Room1>>Room2;
}
// end stream extraction
// Binary Addition operator
float Room::operator +(const Room& addend)const {
return this->GetArea() + addend.GetArea();
}
// end Binary Addition
//============================= ACESS ===================================
// function that sets width of Room
void Room::SetWidth(float aWidth) {
if (aWidth < 0.0)
cout << "Error: Cannot set a negative width." << endl;
else {
this->mWidth = aWidth;
this->mArea = this->GetWidth()*this->GetLength();
}
}
// end function SetWidth
// function that sets length of Room
void Room::SetLength(float aLength) {
if (aLength < 0.0)
cout << "Error: Cannot set a negative length." << endl;
else {
this->mLength = aLength;
this->mArea = this->GetWidth()*this->GetLength();
}
}
// end function SetLength
// function that sets Room
void Room::SetRoom(float aWidth, float aLength) {
this->SetWidth(aWidth);
this->SetLength(aLength);
}
// end function SetRoom
// Overloaded function that sets Room
void Room::SetRoom(const Room& aRoom) {
this->SetRoom(aRoom.GetWidth(), aRoom.GetLength());
}
// end function SetRoom
// function that gets the width of Room
float Room::GetWidth() const {
return (this->mWidth);
}
// end function GetWidth
// function that gets the length of Room
float Room::GetLength() const {
return (this->mLength);
}
// end function GetLength
// function that gets the Area of Room
float Room::GetArea() const {
return (this->mArea);
}
// end function GetArea
// function that gets the Room
const Room& Room::GetRoom()const {
return *this;
}
// end function GetRoom
/////////////////////////////// MAIN ///////////////////////////////////////
// function main begins program execution
void main() {
Room Room1, Room2;
float TotalArea;
cout << "Enter Room 1 Measurement in feet : " << endl;
cin >> Room1;
cout << "Enter Room 2 Measurement in feet : " << endl;
cin >> Room2;
TotalArea = Room1 + Room2;
cout << "========== Room 1 Detail ==========" << endl;
cout << Room1;
cout << "========== Room 2 Detail ==========" << endl;
cout << Room2;
cout << endl;
cout << "Total Area to accommodate order = " << TotalArea << " Sq feet" << endl;
cout << endl;
system("pause");
}
// end main