6.1 INTRODUCTION

Normally, we initialize local or global variables at the time of declaration. However, data members cannot be initialized in this way. For initialization of data members of the objects, C++ has introduced a special method called constructor. The primary purpose of the constructor is to initialize the object at the point of creation.

If we do not initialize the object, it will contain absurd values. Let us check it by a program.

Problem: Write a program to demonstrate that un-initialized objects contain arbitrary values.

Solution: We will define a class Date and check the result. See Program 6.1.

Program 6.1 Un-initialized objects

// object1.cpp#include<IOSTREAM.H>class Date  { private:    int day, month, year;    public:

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.