Return to the C++ Demos Home Page
SAMS Teach Yourself C++ in 21 Days
Chapter Ten
Prog 1 : Member Function Overloading
// Chapter 10 STYC++ in 21 prog 1
//
// Program to show overloading member funcs
//
//
#include <iostream.h>
// Rect class def
class Rect
{
public:
// constructors
Rect (int width, int height);
~Rect() {}
// overloaded class functions
void Draw( int aWidth, int aHeight) const;
void Draw() const;
private:
int itsWidth;
int itsHeight;
}; // end of class Rect
// Constructor Implementation
Rect::Rect(int width, int height)
{
itsWidth = width; itsHeight = height;
}
// The Draw Functions
void Rect::Draw() const
{
Draw( itsWidth, itsHeight );
}
void Rect::Draw(int aWidth, int aHeight) const
{
for ( int i = 0; i<aHeight; i++)
{
for ( int j = 0; j< aWidth; j++ ) cout << "*";
cout << "\n";
}
} // end of Draw defs
// Driver to demonstrate the overloaded functions
int main()
{
// int a Rect to 30,5
Rect myRect(30,5);
cout << "Draw(): \n" ;
myRect.Draw();
cout << "Draw(40,2) : \n";
myRect.Draw(40,2);
cout << "There you have it! \n";
return 0;
}// end of prog
Prog 2 : Overloading Constructors
// Chapter 10 STYC++ in 21 prog 2
//
// Program to show overloading constructors
//
//
#include <iostream.h>
// Rect class def
class Rect
{
public:
// constructors
Rect();
Rect (int width, int height);
~Rect() {}
// overloaded class functions
void Draw( int aWidth, int aHeight) const;
void Draw() const;
private:
int itsWidth;
int itsHeight;
}; // end of class Rect
// Constructor Implementation
Rect::Rect()
{
itsWidth = 5; itsHeight=5;
}
Rect::Rect(int width, int height)
{
itsWidth = width; itsHeight = height;
}
// The Draw Functions
void Rect::Draw() const
{
Draw( itsWidth, itsHeight );
}
void Rect::Draw(int aWidth, int aHeight) const
{
for ( int i = 0; i<aHeight; i++)
{
for ( int j = 0; j< aWidth; j++ ) cout << "*";
cout << "\n";
}
} // end of Draw defs
// Driver to demonstrate the overloaded functions
int main()
{
// int a Rect to 30,5
Rect myRect;
Rect myOtherRect(30,5);
cout << "The Rect with no params \n";
myRect.Draw();
cout << "Now my other Rect \n";
myOtherRect.Draw();
cout << "There you have it! \n";
return 0;
}// end of prog
Prog 3 : A copy constructor for deep copies
// STYC++in21 Chapter 10 prog 3
//
// Example of a proper copy constructor
//
#include <iostream.h>
// class Cat
class Cat
{
public:
Cat();
Cat (const Cat & ); // copy constructor
~Cat();
int getAge() const { return *itsAge; }
int getWeight() const { return *itsWeight; }
void setAge(int age) { *itsAge = age; }
private:
int *itsAge;
int *itsWeight;
}; // end of class Cat def
Cat::Cat()
{
itsAge = new int;
itsWeight = new int;
*itsAge = 5;
*itsWeight = 9;
}
Cat::Cat( const Cat & rhs) // the deep copy constructor
{
itsAge = new int;
itsWeight = new int;
*itsAge = rhs.getAge(); // public access
*itsWeight = *(rhs.itsWeight); // private access
}
Cat::~Cat()
{
delete itsAge;
itsAge = 0;
delete itsWeight;
itsWeight = 0;
}
// The driver to test it..
int main()
{
Cat frisky;
cout << "Frisky's Age is : " << frisky.getAge() << "\n";
cout << "Setting Frisky's age to 6... \n";
frisky.setAge(6);
cout << "Now I'm creating Boots from Frisky!!\n";
Cat boots(frisky);
cout << "Frisky's Age is : " << frisky.getAge() << "\n";
cout << "Boot's Age is : " << boots.getAge() << "\n";
cout << "Setting Frisky's age to 7... \n";
frisky.setAge(7);
cout << "Frisky's Age is : " << frisky.getAge() << "\n";
cout << "Boot's Age is : " << boots.getAge() << "\n";
return 0;
} //
Prog 4 : Operator Overloading
// STYC++ in 21 days Chapter 10 prog 4
//
// Simple operator overloading
//
#include <iostream.h>
// class counter
class counter
{
public:
counter();
~counter() {}
int getItsVal() const { return itsVal; }
void setItsVal(int val) { itsVal = val; }
void Increment() { ++itsVal; }
void operator++ () { ++itsVal; }
private:
int itsVal;
}; // end of def for counter
counter::counter():
itsVal(0) // init val in the constructors init phase
{}
int main() // lets drive this thing...
{
counter i;
cout << "The value of i is : " << i.getItsVal() << "\n";
i.Increment();
cout << "The value of i is : " << i.getItsVal() << "\n";
++i;
cout << "The value of i is : " << i.getItsVal() << "\n";
return 0;
}
Prog 5 : Ready Overloaded Op for assignments
// STYC++ in 21 days Chapter 10 prog 5
//
// Using the this pointer to return a copy of the
// class if used with an assignment on the lhs
//
#include <iostream.h>
// class counter
class counter
{
public:
counter();
~counter() {}
int getItsVal() const { return itsVal; }
void setItsVal(int val) { itsVal = val; }
void Increment() { ++itsVal; }
const counter & operator++ ();
private:
int itsVal;
}; // end of def for counter
counter::counter():
itsVal(0) // init val in the constructors init phase
{}
const counter& counter::operator++()
{
++itsVal;
return *this;
}
int main() // lets drive this thing...
{
counter i;
cout << "The value of i is : " << i.getItsVal() << "\n";
i.Increment();
cout << "The value of i is : " << i.getItsVal() << "\n";
++i;
cout << "The value of i is : " << i.getItsVal() << "\n";
// now try out the operator w/ an assignment
counter a = ++i;
cout << "The value of a is : " << a.getItsVal() << " ";
cout << "and i is : " << i.getItsVal() << "\n";
// How's a doin.....
++a; cout << "The value of a is : " << a.getItsVal() << "\n";
return 0;
}
Prog 6 : Handle all cases when overloading an operator
// STYC++ in 21 days Chapter 10 prog 6
//
// Using the this pointer to return a copy of the
// class if used with an assignment on the lhs
//
// Now we add the postfix operator....The Unary op ++
// works differently when pre or post (you better know
// that if you're reading this) so we handle both cases
//
#include <iostream.h>
// class counter
class counter
{
public:
counter();
~counter() {}
int getItsVal() const { return itsVal; }
void setItsVal(int val) { itsVal = val; }
void Increment() { ++itsVal; }
const counter & operator++ (); // prefix
const counter operator++ (int); // postfix
private:
int itsVal;
}; // end of def for counter
counter::counter():
itsVal(0) // init val in the constructors init phase
{}
const counter& counter::operator++() // the prefix
{
++itsVal;
return *this;
}
const counter counter::operator ++(int x) // the postfix
{
counter temp(*this);
++itsVal;
return temp;
}
int main() // lets drive this thing...
{
counter i;
cout << "The value of i is : " << i.getItsVal() << "\n";
i.Increment();
cout << "The value of i is : " << i.getItsVal() << "\n";
++i;
cout << "The value of i is : " << i.getItsVal() << "\n";
// now try out the operator w/ an assignment (conveinently a prefix op)
counter a = ++i;
cout << "The value of a is : " << a.getItsVal() << " ";
cout << "and i is : " << i.getItsVal() << "\n";
// Now lets test our postfix operator...
a = i++;
cout << "The value of a is : " << a.getItsVal() << " ";
cout << "and i is : " << i.getItsVal() << "\n";
return 0;
}
Prog 7 : Overloading a regular binary operator
// STYC++ in 21 days Chapter 10 prog 7
//
// Now we ovwerload a binary operator...
// in this case the addition operator +...
//
#include <iostream.h>
// class counter
class counter
{
public:
counter();
counter(int myVal);
~counter() {}
int getItsVal() const { return itsVal; }
void setItsVal(int val) { itsVal = val; }
counter operator+ (const counter &);
private:
int itsVal;
}; // end of def for counter
counter::counter():
itsVal(0) // init val in the constructors init phase
{}
counter::counter(int myVal):
itsVal(myVal) // take a param
{}
counter counter::operator+ (const counter & rhs)
{
return counter(itsVal + rhs.getItsVal());
}
int main() // lets drive this thing...
{
counter varOne(2), varTwo(4), varThree;
varThree = varOne + varTwo;
cout << "varOne : " << varOne.getItsVal() << "\n";
cout << "varTwo : " << varTwo.getItsVal() << "\n";
cout << "varThree : " << varThree.getItsVal() << "\n";
return 0;
}