//////////////////////////////////////////////////////////////
// File: StandardFunctions.cpp
// Author: Scott Badman
// Location: Parkland College, CSC 125
//
// Purpose: Summary of formats for common class functions
//////////////////////////////////////////////////////////////
#include "StandardFunctions.h"
// Typical definition of Default Constructor
MyClass::MyClass() {
// appropriate
default initialization of all data members, such as: mValue = 0;
// no return
value allowed
} // MyClass::MyClass
// Typical definition of Copy Constructor
MyClass::MyClass(const MyClass& original) {
// approtriate
copy code, such as: mValue = original.mValue;
// no return
value allowed
} // MyClass::MyClass
// Typical Destructor
MyClass::~MyClass() {
// usually
does nothing unless pointers, files, or system resources are used
}
// Typical overloaded assignment function.
// Similar definitions for +=, -=, *=, /=, %=.
MyClass&
MyClass::operator=(const MyClass& rightside) {
// appropriate
assignment code, such as mValue = rightside.mValue;
return *this;
} // MyClass::operator=
// Typical definition of Conversion Constructors
MyClass::MyClass(int number) {
// appropriate
conversion code, such as: mValue = number;
// no return
value allowed
} // MyClass::MyClass
// Conversion Constructor from some other object
MyClass::MyClass(const OtherClass& other) {
// appropriate
conversion code, such as: mValue = other.mValue;
// no return
value allowed
} // MyClass::MyClass
// Typical insertion operator definition.
// Note that this is a global function. Normally this
function is declared
// a friend
function inside MyClass using the following declaration:
// friend
ostream& operator<<(ostream&, const MyClass&);
ostream&
operator<<(ostream& streamout, const
MyClass& myObject) {
// appropriate
code such as: streamout << setw(n) << myObject.mValue;
return
streamout;
} // MyClass::operator<<
// Typical extraction operator definition. Notice right parameter is not const
// Note that this is a global function. Normally this
function is declared
// a friend
function inside MyClass using the following declaration:
// friend
istream& operator>>(istream&, const MyClass&);
istream&
operator>>(istream& streamin, MyClass&
myObject) {
// appropriate
code such as: streamin >> myObject.mValue;
return
streamin;
} // MyClass::operator>>
// Typical overloading of a prefix unary member function,
except unary minus
// Prefix -- is similar
MyClass
MyClass::operator++() {
// appropriate
code that makes this object "increment by one"
return *this;
} // MyClass::operator++
// Special overloading of the postfix ++ operator
// Postfix -- is similar
MyClass
MyClass::operator++(int ignore) {
MyClass
temp(*this); // use Copy Constructor to
make a copy of this object
++(*this); // call prefix version to
"increment" this object
return
temp; // use Copy Constructor
to return the object as it
// was before incrementing.
} // MyClass::operator++(int)
// Typical overloading of the unary minus operator
// Note that this function does not change the original
object
MyClass
MyClass::operator-() {
MyClass
temp(*this);
// appropriate code to negate the temporary such
as: temp.mValue = -temp.mValue;
return
temp; // return the
temp, using the Copy Constructor.
} // Integer::operator-
// Typical overloading of the unary plus operator
// Note that this function does not change the original
object
MyClass
MyClass::operator+() {
MyClass
temp(*this);
// usually this operator does nothing, so no
code goes here
return
temp; // return the
temp, using the Copy Constructor.
} // Integer::operator-
// Typical overloading of a global binary friend
function.
// Normally these functions are defined in terms of the
combination
// assignment
operator, such as +=
// Note that this is a global function. Normally this
function is declared
// a friend
function inside MyClass using the following declaration:
// friend MyClass
operator+(const MyClass&, const MyClass&);
// Similar definitions for -, *, /, %
MyClass
operator+(const MyClass& left, const MyClass&
right) {
MyClass
temp(left); // use Copy Constructor to
create a local copy of left
temp +=
right; // use += operator below to
perform the "addition"
return
temp; // use Copy Constructor to
copy temp to return location
} // operator+
// Typical overloading of a combination assignment member
function
// Similar definitions for -=, *=, /=, %=
MyClass&
MyClass::operator+=(const MyClass& rightside) {
// appropriate
code goes here to "add" the rightside of += to this object,
// such as:
mValue += rightside.mValue;
return *this;
} // MyClass::operator+=
// Typical overloading of a binary friend logical
function
// Does not have a corresponding combination assignment
function.
// Similar definitions for >, >=, <=, ==, !=
// Also more rarely, similar definitions for &&
and !!
bool
operator<(const MyClass& left, const MyClass&
right) {
bool result;
// appropriate
code goes here, such as: result =
left.mValue < right.mValue;
return result;
} // operator<
// Typical overloading of the unary not operator
bool
MyClass::operator!() {
bool result;
// appropriate
code goes here that returns a logical result for this object,
// often depending on whether this object has
been properly initialized.
return result;
} // MyClass::operator<
// Typical type cast operator to intrinsic type. No parameters or return type.
// Notice that
even though there is no return type, the function must
// return a
value whose type matches the operator.
operator int() must
// return an
int, operator float() must return a float, etc.
MyClass::operator int() {
return
mValue; // where mValue is the correct
type, int for this example.
}
int main() {
// test code
would go here
return 0;
} // main