O'Reilly logo

C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart, Second Edition by Brian Overland

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

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 = ...

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