-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCounter.cpp
More file actions
52 lines (47 loc) · 1.16 KB
/
Counter.cpp
File metadata and controls
52 lines (47 loc) · 1.16 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
// counten.cpp
// inheritance with Counter class
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter //base class
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count(0) //no-arg constructor
{ }
Counter(int c) : count(c) //1-arg constructor
{ }
unsigned int get_count() const //return count
{
return count;
}
Counter operator ++ () //incr count (prefix)
{
return Counter(++count);
}
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter //derived class
{
public:
Counter operator -- () //decr count (prefix)
{
return Counter(--count);
}
};
////////////////////////////////////////////////////////////////
int main()
{
CountDn c1; //c1 of class CountDn
cout << "\nc1=" << c1.get_count(); //display c1
Counter c= ++c1;
++c1;
++c1; //increment c1, 3 times
cout << "\nc1=" << c1.get_count(); //display it
--c1;
--c1; //decrement c1, twice
cout << "\nc1=" << c1.get_count(); //display it
cout << endl;
return 0;
}