Math, asked by deepakkuma6858, 1 year ago

area of a rectangle using overloaded constructors c++

Answers

Answered by rmb3029
1
A constructor is a special type of member function that initialises an objectautomatically when it is created.

Compiler identifies a given member function is a constructor by its name and the return type.

Constructor has the same name as that of the class and it does not have any return type. Also, the constructor is always public.


How constructor works?

In the above pseudo code, temporary() is a constructor.

When an object of class temporary is created, the constructor is called automatically, and x is initialized to 5 and y is initialized to 5.5.

You can also initialise the data members inside the constructor's body as below. However, this method is not preferred.

Use of Constructor in C++

Suppose you are working on 100's of Person objects and the default value of a data member age is 0. Initialising all objects manually will be a very tedious task.

Instead, you can define a constructor that initialises age to 0. Then, all you have to do is create a Person object and the constructor will automatically initialise the age.

These situations arise frequently while handling array of objects.

Also, if you want to execute some code immediately after an object is created, you can place the code inside the body of the constructor

PLZ mark me as a BRAINLIST
Similar questions