Conversion Constructors

A constructor with one parameter of some other type, not the same type as the class, is called a conversion constructor. Often, it is the type of one of the data members of the class. The conversion constructor is useful when the client code wants to specify only one individual value for creation of each object and use the same default values for other fields of each object.

For example, in a modeling program you might want to create Cylinder objects using different values of their radius. Initially, all objects should have height zero, and then they will grow to reflect the process being modeled (growth or arteries, connecting electronic components, heat exchange through the pipe walls, etc.).

 Cylinder c1(50.0); // ...

Get Core C++ A Software Engineering Approach 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.