Constructors in C++ Programming - Part 1

C++ Constructor
Constructors are the special type of member function that initializes the object automatically when it is created. Compiler identifies that the given member function is a constructor by its name and return type. Constructor has same name as that of class and it does not have any return type.
..... ... .....
class temporary
{
    private:
        int x;
        float y;
    public:
        temporary( ): x(5), y(5.5)     /* Constructor */
    {
        /* Body of constructor */
    }
    .... ... ....
}
int main()
{
    temporary t1;
    .... ... ....

Working of Constructor
In the above pseudo code, temporary() is a constructor. When the object of class temporary is created, constructor is called and x is initialized to 5 and y is initialized to 5.5 automatically.

You can also initialize data member inside the constructor's function body as below. But, this method is not preferred.

temporary( )
{
    x=5;
    y=5.5;
}
/* This method is not preferred. */

Use of Constructor in C++
Suppose you are working on 100's of objects and the default value of a data member is 0. Initializing all objects manually will be very tedious. Instead, you can define a constructor which initializes that data member to 0. Then all you have to do is define object and constructor will initialize object automatically. These types of situation arise frequently while handling array of objects. Also, if you want to execute some codes immediately after object is created, you can place that code inside the body of constructor.

Constructor Example
/*Source Code to demonstrate the working of constructor in C++ Programming */
/* This program calculates the area of a rectangle and displays it. */ 
#include <iostream>
using namespace std;
class Area
{
private:
    int length;
    int breadth;
public:
    Area( ): length(5), breadth(2){ } /* Constructor */
    void GetLength( )
    {
    cout<<"Enter length and breadth respectively: ";
    cin>>length>>breadth;
    }
    int AreaCalculation( ) 
    {
    return (length*breadth); 
    }
    void DisplayArea(int temp)
    {
    cout<<"Area: "<<temp;
    }
};
int main( )
{
    Area A1,A2;
    int temp;
    A1.GetLength( );
    temp=A1.AreaCalculation( );
    A1.DisplayArea(temp);
    cout<<endl<<"Default Area when value is not taken from user"<<endl;
    temp=A2.AreaCalculation( );
    A2.DisplayArea(temp);
    return 0;
}

Explanation: In this program, a class of name Area is created to calculate the area of a rectangle. There are two data members length and breadth. A constructor is defined which initializes length to 5 and breadth to 2. And, we have three additional member functions GetLength( ), AreaCalculation( ) and DisplayArea( ) to get length from user, calculate the area and display the area respectively.

When, objects A1 and A2 are created then, the length and breadth of both objects are initialized to 5 and 2 respectively because of the constructor. Then the member function GetLength( ) is invoked which takes the value of length and breadth from user for object A1. Then, the area for the object A1 is calculated and stored in variable temp by calling AreaCalculation( ) function. And finally, the area of object A1 is displayed. For object A2, no data is asked from the user. So, the value of length will be 5 and breadth will be 2. Then, the area for A2 is calculated and displayed which is 10.

Output
Enter length and breadth respectively: 6
7
Area: 42
Default Area when value is not taken from user

No comments:

Post a Comment