12. Constructors: If You Build It...

One of the themes in this book is that object orientation is a way to create fundamental new data types—types that, if useful enough, can be reused in multiple programs.

The promise that you can make classes as convenient as standard data types has yet to be fulfilled. One of the most important features of types such as int, float, double, and so on, is that you can initialize them as you declare them.

But you’re about to see how to do that with classes. Welcome to the craft of C++ construction.

Introducing Constructors

The term constructor is C++-speak for an initialization function, specifically, I mean functions that tell the compiler how to interpret declarations like this:

Fraction a(1, 2);    // a = ...

Get C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart, Second 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.