Download this source file (Boxer.cpp)
download Boxer.exe


// Demonstrates constructor and destructor calls in C++.  -Wayne Pollock, 1999

#include <iostream>
#include <cstdlib>   // Formally known as <stdlib.h>
#include <string>

using namespace std;

class Boxer
{
  public:
    Boxer ( const string name = "[Unknown]" );
    Boxer ( const Boxer& );
    Boxer& operator= ( const Boxer& );
    ~Boxer ()  { cout << name << " ****You're out!" << endl; }

    const string get_name () const  { return name; }
    void set_name ( const string& n )  { name = n; }

  private:
    string name;
};

Boxer::Boxer ( const string n )
{   name = n;
    cout << name << " has entered the ring via an ordinary constructor.\n";
}

Boxer::Boxer ( const Boxer& b )
{   name = b.name;
    cout << b.name << " has entered the ring via a copy constructor.\n";
}

Boxer& Boxer::operator= ( const Boxer& b )
{   cout << "*** assignment op called: " << name << " <-- " << b.name << endl;
    if ( &b == this )   return *this;
    name = b.name;
    return *this;
}

Boxer foo( "George Forman" );  // A global object whose constructor uses iostream is risky!
int main ()
{   cout << "*** At the top of main ***\n";

    Boxer ChampionOfTheSixties("Cassius Clay");
    Boxer TheBestEver = ChampionOfTheSixties;
    ChampionOfTheSixties.set_name("Mohamed Ali");

    cout << "The Champion of the sixties: " << ChampionOfTheSixties.get_name() << endl;
    cout << "The best ever: " << TheBestEver.get_name() << endl;

    Boxer* pboxer = new Boxer("Wayne \"One-Punch\" Pollock");

    Boxer LotsOfBoxers[4];
    LotsOfBoxers[1] = TheBestEver;

    delete pboxer;

    for ( int i=0; i<4; ++i )
    {   cout << "top of for loop\n";
        Boxer b( "Mike Tyson" );    // When is destructor called?
        cout << "i = " << i << endl;
    }
    return 0;
}
#ifdef COMMENTED_OUT

George Forman has entered the ring via an ordinary constructor.
*** At the top of main ***
Cassius Clay has entered the ring via an ordinary constructor.
Cassius Clay has entered the ring via a copy constructor.
The Champion of the sixties: Mohamed Ali
The best ever: Cassius Clay
Wayne "One-Punch" Pollock has entered the ring via an ordinary constructor.
[Unknown] has entered the ring via an ordinary constructor.
[Unknown] has entered the ring via an ordinary constructor.
[Unknown] has entered the ring via an ordinary constructor.
[Unknown] has entered the ring via an ordinary constructor.
*** assignment op called: [Unknown] <-- Cassius Clay
Wayne "One-Punch" Pollock ****You're out!
top of for loop
Mike Tyson has entered the ring via an ordinary constructor.
i = 0
Mike Tyson ****You're out!
top of for loop
Mike Tyson has entered the ring via an ordinary constructor.
i = 1
Mike Tyson ****You're out!
top of for loop
Mike Tyson has entered the ring via an ordinary constructor.
i = 2
Mike Tyson ****You're out!
top of for loop
Mike Tyson has entered the ring via an ordinary constructor.
i = 3
Mike Tyson ****You're out!
[Unknown] ****You're out!
[Unknown] ****You're out!
Cassius Clay ****You're out!
[Unknown] ****You're out!
Cassius Clay ****You're out!
Mohamed Ali ****You're out!
George Forman ****You're out!

#endif