C++ Class Destructor Constructs and destructs a set of data members

Description

C++ Class Destructor Constructs and destructs a set of data members

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Course/* w w w . j  a  va2s.c  om*/
{
  public:
    Course()  { cout << "constructing course" << endl; }
    ~Course() { cout << "destructing course" << endl;  }
};

class Employee
{
  public:
    Employee() { cout << "constructing employee" << endl;}
    ~Employee() { cout << "destructing employee" << endl; }
};
class Manager
{
  public:
    Manager()
    {
        cout << "constructing teacher" << endl;
        pC = new Course;
    }
    ~Manager()
    {
        cout << "destructing teacher" << endl;
        delete pC;
    }
  protected:
    Course* pC;
};
class JobTeam
{
  public:
    JobTeam(){cout << "constructing jobTeam" << endl;}
   ~JobTeam(){cout << "destructing jobTeam" << endl; }
  protected:
    Employee employee;
    Manager teacher;
};

JobTeam* fn()
{
    cout << "Creating JobTeam object in function fn()" << endl;
    JobTeam tp;

    cout << "Allocating JobTeam off the heap" << endl;
    JobTeam*  pTP = new JobTeam;

    cout << "Returning from fn()" << endl;
    return pTP;
}


int main(int nNumberofArgs, char* pszArgs[])
{
    JobTeam* pTPReturned = fn();
    cout << "Return heap object to the heap" << endl;
    delete pTPReturned;

    return 0;
}



PreviousNext

Related