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

An important aspect of types is that you can initialize them. It’s also reasonable to ask to do this with objects as well to be able to initialize them upon construction. In fact, that makes object-oriented syntax much more convenient and programmer-friendly.

A constructor is essentially an initialization function. Welcome to the craft of C++ construction.

Introducing Constructors

A constructor tells the compiler how to interpret declarations like this:

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

Given what you’ve seen of ...

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