Objects with Constructors

To help with understanding some of the complexities in the behaviour of constructors, see if you can work out what this program would do when run. The answer appears at the end. You will almost certainly not get every detail right. Try to work out the true explanation for the parts you missed.

There are actually three different versions of main for this program; each is followed by the output that would be produced.


 

#include <stdio.h>

 

int number = 0;

 

class Object

{ public:

    int a, b, c;

    Object(void);

    Object(const Object & x);

    Object(int x, int y);

    Object & operator= (Object & x);

    void update(void); };

 

Object::Object(void)

{ printf("calling parameterless constructor for Object (0,0,0)\n");

  a=0;

  b=0;

  c=0;

  number+=1;  }

 

Object::Object(const Object & x)

{ printf("calling 'copy' constructor for Object (%d,%d,%d)\n", x.a, x.b, x.c);

  a=x.a;

  b=x.b;

  c=x.c;

  number+=1; }

 

Object::Object(int x, int y)

{ printf("calling normal constructor for Object (%d,%d)\n", x, y);

  a=x;

  b=y;

  c=x+y;

  number+=1; }

 

Object & Object::operator=(const Object & x)

{ printf("calling assignment method for Object (%d,%d,%d)\n", x.a, x.b, x.c);

  a=x.a;

  b=x.b;

  c=x.c;

  return x; }

 

void Object::update(void)

{ printf("calling update method for Object (%d,%d,%d)\n", a, b, c);

  a-=1;

  b+=1; }

 

void main(void)

{ printf("point A\n");

  Object one(5,7);

  printf("point B\n");

  Object two = one;

  printf("point C\n");

  Object three(one);

  printf("point D\n");

  Object four = Object(5,7);

  printf("point E\n");

  Object five;

  five = one;

  printf("point F, total of %d objects created\n", number); }


point A

calling normal constructor for Object (5,7)

point B

calling 'copy' constructor for Object (5,7,12)

point C

calling 'copy' constructor for Object (5,7,12)

point D

calling normal constructor for Object (5,7)

point E

calling parameterless constructor for Object (0,0,0)

calling assignment method for Object (5,7,12)

point F, total of 5 objects created

 

 

void main(void)

{ printf("point A1\n");

  Object *ptr;   

  printf("point B1\n");

  ptr = new Object(5,7);

  printf("point C1\n");

  Object *p2 = new Object(6,8);

  printf("point D1, total of %d objects created\n", number); }

 

 

point A1

point B1

calling normal constructor for Object (5,7)

point C1

calling normal constructor for Object (6,8)

point D1, total of 2 objects created

 

 

void main(void)

{ printf("point A2\n");

  Object array[10];

  printf("point B2\n");

  Object *ptrarray[10];

  printf("point C2, total of %d objects created\n", number); }

 

 

point A2

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

calling parameterless constructor for Object (0,0,0)

point B2

point C2, total of 10 objects created