Introduction to Programming and C++

Contents Previous Topic Next Topic

In this sequence of lessons, we will cover the numerous properties of classes and objects in C++. In order to demonstrate these properties, we will look at a class Verbose (below) which has many of the properties which we are looking for.

You will note that almost each component of the class is linked to one of the topics associated with classes. You can either select the appropriate topic or, click on the part of the class which you do not understand.

The Verbose class is so named because it prints information about every event which occurs, be it a copy constructor or a member function. We will use this to examine how C++ updates and copies instances.

#ifndef VERBOSE
#define VERBOSE

#include <iostream>
#include <string>
using namespace std;

/********************* 
 * Class Declaration * 
 *********************/

template <typename T>
class Verbose;

/********************************** 
 * Global Operator << Declaration * 
 **********************************/

template <typename T>
ostream & operator << ( ostream &, const Verbose<T> & );

/**************************** 
 * Class Member Declaration * 
 ****************************/

template <typename T>
class Verbose {
	private:
		T value;
		int id;

		static int id_count;

	public:
		static const string PREFIX = "       - ";

		Verbose( const T & = T() );    // Constructor
		Verbose( const Verbose & );    // Copy Constructor
		~Verbose();                    // Destructor

		Verbose & operator = ( const Verbose & );

		// Accessors

		T get() const;

		// Mutators

		void set( const T & );

	friend ostream & operator << <> ( ostream &, const Verbose<T> & );
};

/**********************************
 * Static Variable Initialization *
 **********************************/

template <typename T>
int Verbose<T>::id_count = 0;

/*************************************
 * Class Member Function Definitions *
 *************************************/

template <typename T>
Verbose<T>::Verbose<T>( const T & v ):value( v ), id( ++id_count ) {
	cout << PREFIX << *this
	     << ": Calling the constructor" << endl;
}

template <typename T>
Verbose<T>::Verbose<T>( const Verbose<T> & vcc ):value( vcc.value ), id( ++id_count ) {
	cout << PREFIX << *this
	     << ": Calling the copy constructor" << endl;
}

template <typename T>
Verbose<T>::~Verbose<T>() {
	cout << PREFIX << *this
	     << ": Calling the destructor" << endl;
}

template <typename T>
Verbose<T> & Verbose<T>::operator = ( const Verbose<T> & rhs ) {
	value = rhs.value;

	cout << PREFIX << "Assigning to " << *this
	     << " the object " << rhs << endl;
}

template <typename T>
T Verbose<T>::get() const {
	cout << PREFIX << *this << ": Getting the value "
	     << value << endl;

	return value;
}

template <typename T>
void Verbose<T>::set( const T & v ) {
	cout << PREFIX << *this << ": Setting the value from "
	     << value << " to "
	     << v << endl;

	value = v;
}

/*********************************
 * Global Operator << Definition *
 *********************************/

template <typename T>
ostream & operator << ( ostream & out, const Verbose<T> & v ) {
	out << "#" << v.id << " (= " << (&v) << ")";

	return out;
}

#endif

Contents Previous Topic Top Next Topic