O'Reilly logo

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

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

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