//////////////////////////////////////////////////////////////

// File: StandardFunctions.h

// Author: Scott Badman

// Location: Parkland College, CSC 125

//

// Purpose: Summary of "Standard" formats for common class functions

//////////////////////////////////////////////////////////////

#include <iostream>

 

using namespace std;

 

class OtherClass;      // Forward declaration for conversion constructor below

                                            //   Needed only as an example.

 

class MyClass {

public:

 

      // Default Constructor - initializes arrays.  If you have any data members

      //    that might be left uninitialized by a programmer or if you create

      //    another constructor with parameters, you should write a default

      //    constructor.

      MyClass();

 

      // "The Big Three" -- if you create one of them, you should create all them

      //    Normally needed if you have a pointer as a data member, or you control

      //    some system resource such as a locked file or a semaphore.

      MyClass(const MyClass&);    // Copy Constructor

      ~MyClass();                          // Destructor

      MyClass& operator=(const MyClass&);  // Assignment operator, notice reference

                                                                                //   return value

 

      // Conversion Constructors - be careful of promotion conflicts.

      MyClass(int);                // also MyClass(long), MyClass(float), etc.

      MyClass(const OtherClass&);       // From a different class

 

      // casts - be careful, can also cause promotion conflicts.

      // Notice there is no return type, although the function must have a return that matches

    //    the datatype in the function name.

      operator int();               // also operator long(); operator float(); etc.

 

      // Input and Output.

      friend ostream& operator<< (ostream&, const MyClass&);  // this one

                                                                   // function works for both cout and ofstream

      friend istream& operator>> (istream&, MyClass&);  // works for cin and files

 

      // Most common unary operator overloads, defined as member functions

      MyClass operator-();         // should NOT change any value in this object

      MyClass operator++();        // pre-increment  -- SHOULD change this object

      MyClass operator++(int);     // post-increment -- see special case code in .CPP

      MyClass operator--();        // pre-increment

      MyClass operator--(int);     // post-increment -- see special case for ++ below

      MyClass operator+();         // for completeness, usually does nothing

 

      // Most common binary operator overloads, defined as friend functions.

      // These friend functions have complete access to all of this object's data

      friend MyClass operator+(const MyClass&, const MyClass&);

      friend MyClass operator-(const MyClass&, const MyClass&);

      friend MyClass operator*(const MyClass&, const MyClass&);

      friend MyClass operator/(const MyClass&, const MyClass&);

      friend MyClass operator%(const MyClass&, const MyClass&);

 

 

      // If you overload the binary operators above, and you overload =, then

      //    you should overload the following to have similar meaning.

      // Notice these return a reference to your object, not a copy.

      MyClass& operator+=(const MyClass&);

      MyClass& operator-=(const MyClass&);

      MyClass& operator*=(const MyClass&);

      MyClass& operator/=(const MyClass&);

      MyClass& operator%=(const MyClass&);

 

      // Logical overloads - notice return type of bool.

      friend bool operator<(const MyClass&, const MyClass&);

      friend bool operator<=(const MyClass&, const MyClass&);

      friend bool operator>(const MyClass&, const MyClass&);

      friend bool operator>=(const MyClass&, const MyClass&);

      friend bool operator==(const MyClass&, const MyClass&);

      friend bool operator!=(const MyClass&, const MyClass&);

   

    // The following logical operators usually take only bools on both sides, and

    //    usually will not have logical meaning for objects

      friend bool operator&&(const MyClass&, const MyClass&);

      friend bool operator||(const MyClass&, const MyClass&);

 

    // The not operator, !, is often used return whether the object is properly

    //   initialized or not.

    bool operator!();  // unary member function. Should NOT change the object.

 

      // LESS COMMONLY OVERLOADED OPERATORS

 

      // Overloading the subscript operator, pointer dereference, etc.

      //   Note: pointers and arrays will work well with your new class without any

      //         special overloaded definitions for [], *, ->, etc.

      //         Overload these operators only if you want to SIGNIFICANTLY CHANGE

      //         their normal meaning and use.

      //   Notice that they return a reference to your class, not a copy of it.

      MyClass& operator[](int);    // the int parameter becomes the index inside []

      MyClass& operator*();

      MyClass* operator->();       // returns a pointer

      MyClass* operator&();        // unary address of operator, returns pointer

 

      // Memory allocation - REALLY know what you are doing if you redefine these

      void* operator new(size_t);

      void operator delete(void*, size_t);

 

      // Very rarely overloaded operators, mostly for bit manipulation

      MyClass operator~();

      friend MyClass operator&(const MyClass& left, const MyClass& right);

      friend MyClass operator|(const MyClass& left, const MyClass& right);

      friend MyClass operator^(const MyClass& left, const MyClass& right);

      friend MyClass operator->*(const MyClass& left, const MyClass& right);

      MyClass& operator>>=(const MyClass&);

      MyClass& operator<<=(const MyClass&);

      MyClass& operator&=(const MyClass&);

      MyClass& operator|=(const MyClass&);

      MyClass& operator^=(const MyClass&);

 

       // CANNOT BE OVERLOADED

       // ::     scope resolution operator

       //  .     class or struct membership operator

       // .*     class or stuuct member pointer dereference operator

       // ?:     ternary if - then operator

       // sizeof operator

 

private:

    int mValue; // typical private value, included only for the comments in the C file

 

}; // class MyClass