-
Notifications
You must be signed in to change notification settings - Fork 1
/
Session9A.cpp
61 lines (44 loc) · 933 Bytes
/
Session9A.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
#include<iostream>
using namespace std;
class Counter{
int count;
static int sCount;
public:
Counter(){
count = 1;
sCount = 1;
}
Counter(int c){
count = c;
sCount = c;
}
void incrementCount(){
count++;
sCount++;
}
void showCount(){
cout<<"count is: "<<count<<" and sCount is: "<<sCount<<"\n";
}
~Counter(){
count = 0;
cout<<"==Object Destroyed==\n";
}
};
// Declare varibale outside if they are static
int Counter::sCount;
int main(int argc, char const *argv[]){
Counter *c1 = new Counter();
Counter* c2 = new Counter(2);
Counter *c3 = c1; // Reference Copy
c1->incrementCount();
c2->incrementCount();
c3->incrementCount();
c2->incrementCount();
c1->showCount(); // count is: 3 and sCount is: 6
c2->showCount(); // count is: 4 and sCount is: 6
c3->showCount(); // count is: 3 and sCount is: 6
delete c1;
delete c2;
// delete c3; -> error at run time !!
return 0;
}