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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#include <iostream> // for I/O functions
#include <vector> // for vector
using namespace std;
template <class DT>
class DocComponent
{
// class variables - protected
protected:
DT top; //the top margin - the distance from the top of the page in inches
DT left; // the left margin - where the text starts
DT right; // the right margin - where the text ends
// class methods - public
public:
/* DocComponent constructor with no parameters */
DocComponent();
/* DocComponent constructor
* 1. Margins - top, left, right */
DocComponent(DT top, DT left, DT right);
/* DocComponent copy constructor - copies one instance of DocComponent with another */
DocComponent(const DocComponent<DT> &component);
/* DocComponent destructor */
virtual ~DocComponent();
/* formats the object for display to the user */
virtual void display(ostream &s);
/* overloaded ostream << operator - displays the DocComponent object variables wih help from display() */
friend ostream& operator<<(ostream &s, DocComponent<DT> &obj);
/* overloaded assignment = operator
* it assigns the values of one DocComponent instance to another */
DocComponent<DT>& operator=(const DocComponent<DT> &doc);
/* overloaded comparison > operator
*
* ALGORITHM:
* first compares the 'top' field of obj1 with obj2:
* 1. if obj1.top > obj2.top, returns TRUE
* 2. if obj1.top < obj2.top, returns FALSE
* 3. if obj1.top = obj2.top, it then compares the 'left' field of obj1 with obj2 to see which is greater:
*
* 3a. if obj1.left > obj2.left, returns TRUE
* 3b. if obj1.left < obj2.left, returns FALSE
* 3c. if obj1.left = obj2.left, returns FALSE */
friend bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
/* overloaded comparison < operator
*
* ALGORITHM:
* first compares the 'top' field of obj1 with obj2:
* 1. if obj1.top < obj2.top, returns TRUE
* 2. if obj1.top > obj2.top, returns FALSE
* 3. if obj1.top = obj2.top, it then compares the 'left' field of obj1 with obj2 to see which is greater:
*
* 3a. if obj1.left < obj2.left, returns TRUE
* 3b. if obj1.left > obj2.left, returns FALSE
* 3c. if obj1.left = obj2.left, returns FALSE */
friend bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
/* overloaded comparison >= operator
*
* ALGORITHM:
* compares the 'top' field of obj1 with obj2:
* 1. if obj1.top >= obj2.top, returns TRUE
* 2. if obj1.top < obj2.top, returns FALSE */
friend bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
/* overloaded comparison <= operator
* ALGORITHM:
* compares the 'top' field of obj1 with obj2:
* 1. if obj1.top <= obj2.top, returns TRUE
* 2. if obj1.top > obj2.top, returns FALSE */
friend bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
/* overloaded comparison == operator
* ALGORITHM:
* compares the 'top' field of obj1 with obj2:
* 1. if obj1.top == obj2.top, then compares the the 'left' field of obj1 with obj2 to see which is greater:
* 1a. if obj1.left == obj2.left, returns TRUE
* 2. if the 'top' fields do NOT match, returns FALSE */
friend bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
/* overloaded comparison != operator
* ALGORITHM:
* compares the 'top' field of obj1 with obj2:
* 1. returns the NOT of the == operation */
friend bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
};
DocComponent<DT>::DocComponent()
{
// initialize default variable values
this->top = 0;
this->left = 0;
this->right = 0;
}
DocComponent<DT>::DocComponent(DT top, DT left, DT right)
{
// initialize variables with respectful parameters
this->top = top;
this->left = left;
this->right = right;
}
template <class DT>
DocComponent<DT>::DocComponent(const DocComponent<DT> &component)
{
// initialize variables with the same values as those of the component object, thus copying them
this->top = component.top;
this->left = component.left;
this->right = component.right;
}
template <class DT>
DocComponent<DT>::~DocComponent()
{
// do nothing - compiler default
}
template <class DT>
void DocComponent<DT>::display(ostream &s)
{
// visual formatting
s << "*DOC_COMPONENT*\n" <<
"Top Margin (in inches): " << top << "\n" <<
"Left Margin (in inches): " << left << "\n" <<
"Right Margin (in inches): " << right << "\n\n";
}
//------------------------------------------------------------------------------------------------------
template <class DT>
ostream& operator<<(ostream &s, DocComponent<DT> &doc)
{
// output to the user via cout command
doc.display(s);
return s;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>& DocComponent<DT>::operator= (const DocComponent<DT> &doc)
{
this->top = doc.top;
this->left = doc.left;
this->right = doc.right;
return *this;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
if (obj1.top > obj2.top)
{
// obj1 is greater than obj2
return true;
}
else if (obj1.top < obj2.top)
{
// obj1 is less (NOT GREATER) than obj2
return false;
}
else // obj1 and obj2 'left' fields are equal so continue by comparing the 'left' field
{
if (obj1.left > obj2.left)
{
// obj1 is greater than obj2
return true;
}
else if (obj1.left < obj2.left)
{
// obj1 is less (NOT GREATER) than obj2
return false;
}
else
{
// obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
return false;
}
}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
if (obj1.top < obj2.top)
{
// obj1 is less than obj2
return true;
}
else if (obj1.top > obj2.top)
{
// obj1 is greater (NOT LESS THAN) than obj2
return false;
}
else // obj1 and obj2 'top' fields are equal so continue by comparing the 'left' field
{
if (obj1.left < obj2.left)
{
// obj1 is less than obj2
return true;
}
else if (obj1.left > obj2.left)
{
// obj1 is greater (NOT LESS THAN) than obj2
return false;
}
else // obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
{
return false;
}
}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
if (obj1.top >= obj2.top)
{
// obj1 is greater than or equal to obj2
return true;
}
else
{
// obj1 has to be less than obj2
return false;
}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
if (obj1.top <= obj2.top)
{
// obj1 is less than or equal to obj2
return true;
}
else
{
// obj1 has to greater than obj2
return false;
}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
if (obj1.top == obj2.top) // first field 'top' is equal, so compares the next field 'left'
{
if (obj1.left == obj2.left)
{
// 'left' fields are equal, the objects are equal
return true;
}
else
{
// 'left' fields ARE NOT equal, the objects ARE NOT equal
return false;
}
}
return false; // first field 'top' is NOT equal, so they cannot be equal, false!
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
// returns the opposite of the == operation
return !(obj1 == obj2);
}
void main()
{
DocComponent<float>* d = new DocComponent<float>(1, 2, 3);
cout << *d << endl;
delete d; d = NULL;
}
|