-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.cpp
More file actions
97 lines (74 loc) · 2.42 KB
/
Counter.cpp
File metadata and controls
97 lines (74 loc) · 2.42 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
#include "Counter.h" // class implemented
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Counter Default + Overloaded Constructor
Counter::Counter(int aCount) : mSerialNo(Counter::sGetTotalCounters()+1), mCount(aCount) {
this->SetCounter(aCount);
Counter::msNoOfCounters++;
}
// end Counter constructor
//============================= OPERATORS ====================================
// Increment operator
Counter& Counter::operator ++() {
this->SetCount(this->GetCount() + 1);
return *this;
}
// end increment operator
// Negation operator
int Counter::operator -() {
return -(this->GetCount());
}
// end negation operator
// Assignment operator
Counter& Counter::operator =(int aCount) {
this->SetCount(aCount);
return *this;
}
// end assignment operator
// Overloaded Assignment operator
Counter& Counter::operator =(const Counter& rhs) {
this->SetCount(rhs.GetCount());
return *this;
}
// end assignment operator
//============================= ACESS ===================================
// function that sets count value of Counter
void Counter::SetCount(int aCount) {
this->mCount = aCount;
}
// end function SetCount
// function that sets the Counter
void Counter::SetCounter(int aCount) {
this->SetCount(aCount);
}
// end function SetCounter
// overloaded function that sets the Counter
void Counter::SetCounter(const Counter& obj) {
this->SetCounter(obj.GetCount());
}
// end function SetCounter
// function that gets serial no. of the Counter
int Counter::GetSerialNo()const {
return this->mSerialNo;
}
// end function GetSerialNo
// function that gets count value of the Counter
int Counter::GetCount()const {
return this->mCount;
}
// end function GetCount
// function that gets the Counter
const Counter& Counter::GetCounter()const {
return *this;
}
// end function GetCounter
// static function that gets the total no. of Counters
int Counter::sGetTotalCounters() {
return Counter::msNoOfCounters;
}
// end function sGetTotalCounters
/////////////////////////////// PRIVATE ///////////////////////////////////
/* private static member cannot be accessed outside the class except for initialization */
int Counter::msNoOfCounters = 0; // intitalize class variable