6.5 CONSTRUCTOR OVERLOADING

It may be noted that a constructor is a function. C++ allows function overloading. Naturally, it allows constructor overloading. When a class has more than one constructor defined, it is said that constructors are overloaded. The constructors are then recognized depending on the parameters passed.

The Program 6.5 will define two objects. One will be initialized by a parameterized constructor; other will be initialized with a constructor without parameters.

Problem: Write a program to demonstrate constructor overloading.

Solution: See Program 6.5.

Program 6.5 Constructor overloading I

// const2.cpp#include<iostream.h>class Date{ private :    int day, month, year;  public:    Date();    Date(int d, int m, int y); ...

Get Object Oriented Programming with C++, Second 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.