Program To Illustrate The Use Of Constructor Member Function To Initialise An Object During Its Creation Using C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code


To copy code: Click View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

In this program, whenever we create an object of class rectangle with the statement  rectangle r1; the data members length and breadth of object r1 will be initialized with the values 6 and 8 respectively by invoking the zero parameter constructor automatically.This method of initializing data members of objects using constructors is very simple and concise as there is no need to explicitly call the member function for each object separately.


Area of rectangle = 48