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

C++ examples for Class:Constructor

Description

Constructs and destructs a set of data members

Demo Code

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

class Course//from   w w w.j a  v  a  2  s  . com
{
  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;
}

Result


Related Tutorials