Constructors and Destructors

There are two ways to define an integer variable. You can define the variable and then assign a value to it later in the program. For example:

int Weight;            // define a variable
...                    // other code here
Weight = 7;            // assign it a value

Or you can define the integer and immediately initialize it. For example:

int Weight = 7;        // define and initialize to 7

Initialization combines the definition of the variable with its initial assignment. Nothing stops you from changing that value later. Initialization ensures that your variable is never without a meaningful value.

How do you initialize the member data of a class? Classes have a special member function called a constructor that is called when an object of the class is ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.