Parameterized Constructors in C++

The constructor integer(), defined in the previous section (Constructors in C++), initializes the data members of all the objects to zero. However, in practice it may be necessary to initialize the various data elements of different objects with different values when the are created. C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that can take the arguments are called parameterized constructors. The constructor integer() may be modified to take arguments as shown below: class integer { private: int m, n; public: integer(int x, int y); //parameterized constructor … … }; integer :: integer(int x, int y) { m = x; n = y; } When a constructor has been parameterized, the object declaration statement such as: integer in1; may not work. We must pass the initial values as arguments to the constructor function when an object is...