O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Constructors

A constructor is just a public member function that is called to create an instance of a class, known as an object. It is responsible for initializing private member variables and can receive parameters to enable it do so. The parameters work exactly as they do for any other function.

It is also possible to construct the object without a value being passed at all or to allow the calling code to optionally provide parameters. You can offer several constructors for the same class, each with different parameters, or none at all.

Be sure to set defaults for private member variables not explicitly initialized from the constructor call; otherwise the instance might not be properly instantiated. Just as variables don’t have properly set ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required