construction.cpp
/** @file construction.cpp
 *  @author Martin Vlach
 *  @version 1.1
 *  @date    28.11.2010
 */
#include <iostream>
#include <string>
 
/**
  * @class C1
  * @brief Base class that informs about launching it's
  *   internal mechanisms by writing to cout stream object.
  */
class C1{
protected:
    std::string _description;
 
public:
    /**
      Implicit constructor, which inicialiazes the _description attribute
      to an empty string.
    */
    C1();
 
    /**
      Constructor with additional description string.
      @param variableDescription
        value to be assigned to the object for it's easier identification.
    */
    C1(const std::string variableDescription);
 
    /**
      Copy constructor with some logging to an internal attribute.
      @param copyFrom
        Descriptive string of this argument is copied and there is
        an information added about the fact, that the desctiption
        didn't originally belong to this object.
    */
    C1(C1& copyFrom);
 
    /**
      If a descriptive string was provided, it is printed along the class name.
    */
    virtual ~C1();
};
 
 
/**
  * @class C2
  * @brief This class is capable of the same operations as C1 class,
  *   it only demonstrates the way derived classes are constructed and destroyed.
  */
class C2: public C1{
public:
    /**
      Implicit constructor, which inicialiazes the _description attribute
      to an empty string.
    */
    C2();
 
    /**
      Constructor with additional description string.
      @param variableDescription
        value to be assigned to the object for it's easier identification.
    */
    C2(const std::string variableDescription);
 
    /**
      Copy constructor with some logging to an internal attribute.
      @param copyFrom
        Descriptive string of this argument is copied and there is
        an information added about the fact, that the desctiption
        didn't originally belong to this object.
    */
    C2(C2& copyFrom);
 
    /**
      If a descriptive string was provided, it is printed along the class name.
    */
    virtual ~C2();
};
 
 
// Definitions of methods of the class C1:
C1::C1(): _description(){
    std::cout << "Implicit constructor of C1" << std::endl;
}
 
C1::C1(const std::string variableDescription): _description(variableDescription){
    std::cout << "Constructor of C1 with parameter \"" << _description
              << "\"" << std::endl;
}
 
C1::C1(C1& copyFrom): _description("copied " + copyFrom._description){
    std::cout << "Copy constructor of C1 - \"" << _description << "\"" << std::endl;
}
 
C1::~C1(){
    std::cout << "Destructor of C1 object labeled as \""
              << _description << "\"" << std::endl;
}
 
// Definitions of methods of the class C2:
C2::C2(){
    std::cout << "Implicit constructor of C2" << std::endl;
}
 
C2::C2(const std::string variableDescription): C1(variableDescription){
    std::cout << "Constructor of C2 with parameter \"" << _description
            << "\"" << std::endl;
}
 
C2::C2(C2& copyFrom): C1("copied " + copyFrom._description){
    std::cout << "Copy constructor of C2 - \"" << _description << "\"" << std::endl;
}
 
C2::~C2(){
    std::cout << "Destructor of C2 object labeled as \""
              << _description << "\"" << std::endl;
}
 
 
void someFunction(void);
 
 
using namespace std;
 
C1 globalVar("global instance" );
 
int main(void){
    cout << endl;
    C1 loC1;
    cout <<endl;
    C2 loC2;
    cout << endl;
    C2 loc3("Local instance 3");
    cout << endl;
    C2 loc4=loc3;
    cout << endl;
    {
        cout << "A block starts here on line " << __LINE__ << endl;
        C1 loc5("Local 5 (inside the block)");
        cout << "The block ends here on line " << __LINE__ << endl;
    }
    cout << endl;
    cout << "'someFunction' called:" << endl;
    someFunction();
    cout << endl << "From the 'someFunction' we're back in main." << endl;
    cout << "Strictly speaking, we just reached it's end." << endl << endl;
 
    // Here should all the objects get destroyed.
    return 0;
}
 
 
void someFunction(void){
    std::cout << "Function starts here on line " << __LINE__ << std::endl;
    C1 loc6("Local 6 (inside function)");
    std::cout << "Function ends here on line " << __LINE__ << std::endl;
}  // In fact, function forms a block too.
QR Code
QR Code public:constructionloggingexample (generated for current page)