C++ Constructor

October 27, 2015 Posted by WithU Technologies
They are known as Class constructors, as the constructor is
called when the new class object is created.

What is constructor?
·
Constructor name is identical to that class and
it has no return type.
·
Used for setting initial values for certain
member variables.
·
By default, constructor has no Parameters. But
with requirement parameters can be added to a constructor.

·
When a new object is created, it assigns an
initial value to it.

#include <iostream>
using namespace std;

class Area     //New Class created

{

    private:

       int l;    //length

       int b;    //breadth



    public:

       Area(): l(8), b(3){ //Constructor
declared with two parameters
       }

       void GetArea(){

           cout<<"Enter
length of the rectangle: "
<<endl;

           cin>>l;  //Length value taken from user

           cout<<"Enter
breadth of the rectangle: "
<<endl;

           cin>>b;  //Breadth value taken from user

       }

       int CalculateArea() {

            return (l*b);  //It will return the product of
length and breadth

       }

       void Output(int r)  //Method is
declared

       {

           cout<<"Area:
"
<<r<<endl;  //It outputs the Area of
rectangle

       }

};

int main(){

    char answer;

            cout<<"Do you want to enter new length
and breadth for the rectangle? (Enter Y or N) "
<<endl;

            cin>>answer;  //User decision
in character y or n

            if(answer =='y'){ // if user input Y then

                      Area A1;     //new object named A1 created under
Area class

                      int CustomResult;  //Integer
variable initialised



                      A1.GetArea();  //A1 objects access the GetArea method to get the values of length and breadth
provided by user

                      CustomResult=A1.CalculateArea();   //CalculateArea method will be called where it gets a return
of length and breadth multiplication



                      A1.Output(CustomResult);  //It carry
forward the product value to output function to display it




            }

            if(answer == 'n'){   //If user
input N then

                  Area A2;       //New object named A2 created under
Area class

                  int DefaultResult;   //integer variable initialised

                  cout<<endl<<"Area of the rectangle by
default: "
<<endl;  //Here we did not access the GetArea method to use the length and breadth value provided
by the user

                  DefaultResult=A2.CalculateArea();  //Thus it
access the default values of length and breadth initialised by the constructor
and multiply them to provide a return

                  A2.Output(DefaultResult);   //That return
value then used to display the default area of rectangle

            }

    return 0;

}

·
The complete program is explained with comments
within the program code. Here we use constructor to initialise the length and
breadth with a default value of 8 and 3 respectively.

·
Then we declare two access specifiers. One as
private where we initialise the l and b integer variables and the next one as
public, where we use the GetArea() function to take
length and breadth input from user.

·
Then the CalculateArea()
function is used to provide return value after multiplying the length and
breadth.
·
And after that an Output function is used to
display the product of length and breadth.
·
Now in the main function of the program, we
first prompt user to provide an input of Y or N which will make it clear that
the user wants to provide new length and breadth to calculate area or he wants
to go with the default values.
·
According to that we nested the if statement
twice. In the first if statement we created A1 object and then use that object
to access GetArea() function to fetch length and
breadth provided by user and It also fetch CalculateArea()
function to calculate that length and breadth.
·
After calculating user given length and breadth,
it displays the output by passing the value to the Output function.
·
The second if statement is used to display the
product of default length and values and thus it did not call the GetArea() function and that is the only difference between two.