Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Constructors

When we declare a Point object, the instance variables dX and dY remain uninitialized until we specially assign values to them. It would probably make sense to set them to zero by default to specify an origin point of your cartoon. You can do this using a constructor . Constructor is a special member function (unique to OOP languages) which has the same name as its class and has no return type:

class Point

{


Point ()

{

dX = 0 ;

dY = 0 ;

}


};

Constructor is called automatically when you declare an object like this:

void Scene1 ()

{

Point MyPoint;

}

Constructors can also accept parameters. For example it is pretty common to assign dX and dY immediately after an object declaration. It is possible to do this using a constructor also:

class Point

{


Point ()

{

dX = 0 ;

dY = 0 ;

}


Point ( double a_dX, double a_dY )

{

dX = a_dX;

dY = a_dY;

}



};

void Scene1 ()

{

Point MyPoint; // initialized with zero values

Point AnotherPoint ( 400, 300 ); // initialized with 300 and 400

}

You can see two more things from the example above. First of all there could be several constructors in a class if they accept different parameters. You can also note that we used strange parameter names like a_dX and a_dY. This was done on purpose to distinguish between a method (constructor) parameters and instance variables dX and dY.

But what if we do not want a default constructor initializing our object with zeros? You can just delete it. But in this case you will no longer be able to declare an object without parameters:

class Point

{


Point ( double a_dX, double a_dY )

{

dX = a_dX;

dY = a_dY;

}



};

void Scene1 ()

{

Point MyPoint; // WRONG!! There is no default constructor!

Point AnotherPoint ( 400, 300 ); // OK

}

It is a good practice to specify a default constructor with no parameters for most of the classes as well as constructors with parameters. If constructors are not specified, the C++ compiler (not the WCM compiler) will specify one, which can lead to problems beyond the scope of this book. Also beyond the scope of this book is the concept of a destructor , which is essentially the opposite of a constructor and is used in special circumstances to free up previously allocated memory space (garbage collection).


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll