topical media & game development

talk show tell print

basic-program-solutions-07-Soln7-6.c

? / basic-program-solutions-07-Soln7-6.c


  // Soln7_6.cpp
  
  include <iostream>                   // For stream input/output
  include <cstring>
  
  using std::cout;
  using std::endl;
  
  class CTrace
  {
  public:
     CTrace(const char* str);
     ~CTrace();
  private:
     char* pstr;
     static int indentLevel;
  };
  
  int CTrace::indentLevel = 0;
  
  CTrace::CTrace(const char* str)
  {
     indentLevel += 2;
  
     size_t len = strlen(str)+1;
     pstr = new char[len];
     strcpy_s(pstr, len, str);
  
     for(int i = 0; i<indentLevel ; i++)
        cout << ' ';
     cout << "Entry: " << pstr << endl;
  }
  
  CTrace::~CTrace()
  {
    for(int i = 0 ; i<indentLevel ; i++)
      cout << ' ';
    cout << "Exit: " << pstr << endl;
  
     delete pstr;
     pstr = NULL;
     indentLevel -= 2;
  }
  
  int main()
  {
     CTrace trace("Main routine");
  
     if (3 > 5)
     {
        CTrace trace1("'if' block");
     }
     else
     {
        CTrace trace2("'else' block");
     }
  
     return 0;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.