-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShape_Task2.cpp
105 lines (86 loc) · 2.57 KB
/
Shape_Task2.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
#include "Shape_Task2.h" // class implemented
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Shape Default+Overloaded Constructor
Shape::Shape(char* aColor, int aCoord) : mCoord(aCoord) {
this->SetShape(aColor, aCoord);
}
// end Shape constructor
// Shape Copy Constructor
Shape::Shape(const Shape& rhs) : mCoord(rhs.GetCoord()) {
this->SetShape(rhs.GetColor(), rhs.GetCoord());
}
// end Shape constructor
// Shape Destructor
Shape::~Shape() {
// freeing the alloted memory
if (this->mColor)
delete[] this->mColor;
}
// end Shape Destructor
// Shape assignment operator.
Shape& Shape:: operator =(const Shape& rhs) {
this->SetShape(rhs.GetColor(), rhs.GetCoord());
return *this;
}
// end Shape assignment operator.
//============================= OPERATIONS ===================================
// function that depicts rotation of Shape.
void Shape::Rotate() {
cout << "Rotating a Shape of " << this->GetColor() << " color." << endl;
}
// end function Rotate
//============================= ACESS ===================================
// function that sets color of Shape
void Shape::SetColor(char* aColor) {
// freeing the previously alloted memory
if (mColor)
delete[] mColor;
// reserving new memory space for updated name
if (aColor != NULL) {
int len = strlen(aColor) + 1;
this->mColor = new char[len];
strcpy_s(this->mColor, len, aColor);
}
else {
this->mColor = new char;
strcpy_s(this->mColor, 1, "\0");
}
}
// end function SetColor
// function that sets coordinate(s) of Shape
void Shape::SetCoord(int aCoord) {
if (aCoord < 0)
cout << "Error: Coord cannot be neagtive." << endl;
else
this->mCoord = aCoord;
}
// end function SetCoord
// function that sets Shape
void Shape::SetShape(char* aColor, int aCoord) {
this->SetColor(aColor);
this->SetCoord(aCoord);
}
// end function SetShape
// function that sets the Shape
void Shape::SetShape(const Shape &obj) {
this->SetShape(obj.GetColor(), obj.GetCoord());
}
// end function SetShape
// function that gets color of Shape
char* Shape::GetColor() const {
return (this->mColor);
}
// end function GetColor
// function that gets coordinate(s) of Shape
int Shape::GetCoord() const {
return this->mCoord;
}
// end function GetCoord
// function that gets the Shape
const Shape& Shape::GetShape()const {
return *this;
}
// end function GetShape