The member objects of a class are each constructed before the container class constructor gets a shot at it - C++ Class

C++ examples for Class:Constructor

Description

The member objects of a class are each constructed before the container class constructor gets a shot at it

Demo Code

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

class Course//from   w  w w .  ja  va  2s. c o  m
{
  public:
    Course(){ cout << "constructing course" << endl;}
};

class Employee
{
  public:
    Employee()
    {
        cout << "constructing employee" << endl;
        workHour = 0;
        salary = 0.0;
    }
  protected:
    int  workHour;
    double salary;
};
class Manager
{
  public:
    Manager(){cout << "constructing teacher" << endl;}
  protected:
    Course c;
};
class JobTeam
{
  public:
    JobTeam()
    {
        cout << "constructing jobTeam" << endl;
        noMeetings = 0;
    }
  protected:
    Employee employee;
    Manager teacher;
    int   noMeetings;
};

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "Creating JobTeam object" << endl;
    JobTeam tp;

    return 0;
}

Result


Related Tutorials