O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

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 and Destructors

When you create a struct variable, the value of the member variables is not defined until you initialize them from your program. But the class allows you to declare and define a special member function, the constructor, which can initialize member variables, public or private, when an object of the class is instantiated.

The constructor is called automatically by code that the compiler generates as part of the process of instantiating the object.

The constructor has the same name as the class. It has no return type, not even void, and the most basic constructor has no arguments. For a class called aSAMSAccumulator, it would be specified in the class declaration as shown in Listing 17.5.

Listing 17.5. A Constructor ...

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