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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* =====================================================================================
*
* Filename: 5.00.ReihenfolgeUndWirkungVonMethoden.cc
*
* Description: Implements a class for testing methods
*
* Version: 1.0
* Created: 12.05.2014 09:47:57
* Revision: none
* Compiler: gcc
*
* Author: Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de
* Organization: FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
// =====================================================================================
// Class: Class
// Description: Simple Class for testing methods
// =====================================================================================
class Class{
public:
// ==================== LIFECYCLE =======================================
Class ( int _value = 0, string name = "" ); // constructor
Class ( const Class &other ); // copy constructor
~Class (); // destructor
// ==================== ACCESSORS =======================================
void print();
// ==================== MUTATORS =======================================
Class& set_name ( string _name );
Class& set_value( int _value);
// ==================== OPERATORS =======================================
Class& operator = ( const Class &other ); // assignment operator
Class operator + ( const Class &other ); // addition
private:
// ==================== DATA MEMBERS =======================================
int value;
string name;
}; // ----- end of class Class -----
Class f ( Class arg ); // Prototyp function f
Class g ( Class *arg ); // Prototyp function g
// === FUNCTION ======================================================================
// Name: main
// Description:
// =====================================================================================
int main ( int argc, char *argv[] ){
cout << "Class x1(11, \"x1\");\n";
Class x1(11, "x1");
cout << "\nClass x2 = x1;\n";
Class x2 = x1;
cout << "\nClass x3(33, \"x3\");\n";
Class x3(33, "x3");
cout << "\nx2.set_name(\"x2\");\n";
x2.set_name("x2");
cout << "\nx2 = x3;\n";
x2 = x3;
cout << "\nx2 = x1 + 11;\n";
x2 = x1 + 11;
cout << "\nx2 = x1 + x3;\n";
x2 = x1 + x3;
cout << "\nx3 = f( x2 );\n";
x3 = f( x2 );
cout << "\nx3 = x1 + f( x2 );\n";
x3 = x1 + f( x2 );
cout << "\nx3 = x1 + g( &x2 );\n";
x3 = x1 + g( &x2 );
cout << "\nx3 = 234;\n";
x3 = 234;
cout << "\nx3.set_value(234);\n";
x3.set_value(333);
cout << "\nEOF\n";
return EXIT_SUCCESS;
} // ---------- end of function main ----------
// === FUNCTION ======================================================================
// Name: f
// Description: Function for returning Class arg
// =====================================================================================
Class f ( Class arg ){
return arg;
} // ----- end of function f -----
// === FUNCTION ======================================================================
// Name: g
// Description: Function for returning Class *arg
// =====================================================================================
Class g ( Class *arg ){
return *arg;
} // ----- end of function g -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: Class
// Description: constructor
//--------------------------------------------------------------------------------------
Class::Class ( int _value, string _name){
// Print a few stats for testing
cout << " ==== constructor ( this = " << this << " )\n";
// Assign values
value = _value;
name = _name;
} // ----- end of method Class::Class (constructor) -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: Class
// Description: copy constructor
//--------------------------------------------------------------------------------------
Class::Class ( const Class &other ){
// Print a few stats for testing
cout << " ==== copy constructor ( this = " << this << ", other = " << &other << " )\n";
value = other.value;
} // ----- end of method Class::Class (copy constructor) -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: ~Class
// Description: destructor
//--------------------------------------------------------------------------------------
Class::~Class (){
// Print a few stats for testing
cout << " ==== destructor ( this = " << this << " )\n";
} // ----- end of method Class::~Class (destructor) -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: print
// Description: Prints value and object name
//--------------------------------------------------------------------------------------
void Class::print (){
// Print a few stats for testing
cout << " ==== print ( this = " << this << " )\n";
cout << name << ": " << value << "\n";
} // ----- end of method Class::print -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: set_name
//--------------------------------------------------------------------------------------
Class& Class::set_name ( string _name ){
// Print a few stats for testing
cout << " ==== set_name ( this = " << this << " )\n";
name = _name;
return *this;
} // ----- end of method Class::set_name -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: set_value
//--------------------------------------------------------------------------------------
Class& Class::set_value ( int _value ){
// Print a few stats for testing
cout << " ==== set_value ( this = " << this << " )\n";
value = _value;
return *this;
}
//--------------------------------------------------------------------------------------
// Class: Class
// Method: operator =
// Description: assignment operator
//--------------------------------------------------------------------------------------
Class& Class::operator = ( const Class &other ){
// Print a few stats for testing
cout << " ==== operator= ( this = " << this << ", other = " << &other << " )\n";
if ( this != &other ) {
value = other.value;
}
return *this;
} // ----- end of method Class::operator = (assignment operator) -----
//--------------------------------------------------------------------------------------
// Class: Class
// Method: operator +
// Description: addition operator
//--------------------------------------------------------------------------------------
Class Class::operator + ( const Class &other ){
// Print a few stats for testing
cout << " ==== operator+ ( this = " << this << ", other = " << &other << " )\n";
return Class(value + other.value);
}
|