-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
134 lines (104 loc) · 2.82 KB
/
main.cc
File metadata and controls
134 lines (104 loc) · 2.82 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
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
#include <iostream>
#include <sstream>
#include <exception>
#include <string>
using namespace std;
class Cpp {
protected:
int _v;
const string _some;
static int counter;
static string _static_str;
public:
Cpp() : _v(0), _some("thing") { counter++; }
virtual ~Cpp() {
cout << "Destructing in " << _v << endl;
cout << "Instances: " << counter << endl;
counter--;
}
Cpp(const Cpp& rhs) : _v(rhs._v), _some(rhs._some) {
//_v = rhs._v;
cout << "Cpp copy constructor" << endl;
}
Cpp& operator=(const Cpp& rhs) {
cout << "Cpp assignment operator" << endl;
if(this == &rhs) return *this;
_v = rhs._v;
return *this;
}
// Cannot do Cpp p = 3; must Cpp p(3)
explicit Cpp(int val) : _v(val) { counter++; }
// Prefix operator
Cpp& operator++();
// Postfix operator
Cpp operator--(int);
// conversion operator
operator int() const { return _v; }
// Friend function, global Cpp postfix --
friend Cpp& operator--(Cpp& p);
int getValue() const { return _v; }
};
int Cpp::counter = 0;
string Cpp::_static_str = "aaa";
Cpp& Cpp::operator++() {
++_v;
return *this;
}
Cpp Cpp::operator--(int) {
Cpp copy(getValue());
--_v;
return copy;
}
Cpp& operator--(Cpp& p) {
p._v--;
return p;
}
//Templates
// Class with default type
template <class T=int, int SIZE=10> class Templ {
protected:
T _v;
T _array[SIZE];
int size;
public:
Templ() : _v(0), size(0) {}
Templ(T val);
operator string() {
ostringstream ss;
ss << "Value is " << _v << " and size is " << size;
return ss.str();
}
};
template <class T, int SIZE> Templ<T,SIZE>::Templ(T val) : _v(val) {
size = SIZE;
}
//Specialisation
template <> Templ<char>::Templ(char val) {
_v = int(val);
size = 11;
cout << "Specialised constructor used for type 'char'" << endl;
}
int main(int argc, char* argv[])
{
cout << "Starting now..." << endl;
Cpp dummy; // for instance counting
Cpp obj(5);
cout << "Prefix ++ after 5 is " << (++obj).getValue() << endl;
cout << "Prefix for prefix -- is " << (obj--).getValue() << endl;
cout << "Actual value by conversion operator is " << obj << endl;
cout << "Prefix -- value is << " << --obj << endl;
Templ<long, 20> templ(15);
cout << "Template created: " << string(templ) << endl;
// Use all template defaults
Templ<> def(5);
cout << "Template with default types: " << string(def) << endl;
Templ<char> chTempl('a');
cout << "Template for char created: " << string(chTempl) << endl;
Cpp a(10);
Cpp b = a;
cout << "b value is " << b << endl;
Cpp c;
c = a;
cout << "c value is " << c << endl;
return 0;
}