diff options
Diffstat (limited to '5.00.ReihenfolgeUndWirkungVonMethoden.cc')
| -rw-r--r-- | 5.00.ReihenfolgeUndWirkungVonMethoden.cc | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/5.00.ReihenfolgeUndWirkungVonMethoden.cc b/5.00.ReihenfolgeUndWirkungVonMethoden.cc new file mode 100644 index 0000000..c6f02d8 --- /dev/null +++ b/5.00.ReihenfolgeUndWirkungVonMethoden.cc @@ -0,0 +1,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); +} |
