Default Constructors

Many classes do not need constructors because class objects do not need default initialization. When the class designer does not add any constructors to the class, the system provides a default constructor (which does nothing) for the class.

class Cylinder {                   // OK if no constructors/destructors
     double radius, height;     // data is protected from client access
public:
  void setCylinder(double r, double h);       // methods are accessible
  double getVolume();
  void scaleCylinder(double factor);
  void printCylinder();
} ;                                           // end of class scope

All of the versions of class Cylinder that I discussed in the previous section were using the system-supplied default constructor. I did not mention it to avoid unnecessary complication of the discussion. ...

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.