Constructors and Destructor in C++

Constructors & Destructor in C++

Constructors


A constructor is a special member function whose task is to initialize the object of its class. It is special because its name is the same as the class name. The constructor is call whenever an object of its associated class is created. It is called constructor because it construct the value of data member of class. Constructor must follow following properties.

Properties


  1. Name of constructor is same as the class name.
  2. Constructor will call implicitly at the time of declaration.
  3. Constructor does not have any return type.
  4. Constructor are used to define object that mean to initialize member data.
  5. Constructor must be a public member function of a class. If constructor is private then user is unable to create object of such class.
  6. Constructor can be overload that means more than one constructor is possible in a class.
  7. We can not refer their address.

Type of Constructors


  1. Parameterized Constructor
  2. Default Argument Constructor such as
  3. Copy Constructor

Parameterized Constructor


The constructor that can take argument is called parameterized constructor.
Example:
Copy Code
#include <iostream>

using namespace std;
class add
{
    int a,b;
    public:
    add(); 
    add(int);
    add(int,int);
    void print();
};
add::add()
{
    a=2;
    b=5;
}
add::add(int x)
{
    a=x;
    b=x;
}
add::add(int x,int y)
{
    a=x;
    b=y;
}
void add::print()
{
    cout<<"\n"<<"a="<<a<<"\n"<<"b="<<b<<"\n"<<"Sum="<<a+b<<"\n";
}
int main()
{
    add a1;
    a1.print();
    add a2(10);
    a2.print();
    add a3(10,20);
    a3.print();

    return 0;
}

In the above example show how to use zero argument ,one argument,two argument constructor in any program.

Output

a=2
b=5
Sum=7

a=10
b=10
Sum=20

a=10
b=20
Sum=30

Default Argument Constructor


It is possible to define constructor with default arguments.

Example:


Add(int x,int y=0);
Here the default value of y is 0.

If the statement is Add A1(10);
That means assign the value 10 for x and 0 for y by default.

If the statement is Add A1(10,20);
That means assign 10 for x and 20 fro y. The actual parameter when specified overrides the default value.

Note: It is most important to distinguish between the default constructor Add::Add() and the default argument constructor Add::Add(int=0).The default argument can be called with either by one argument or no argument.When called with no argument it become default constructor.

Example:


Copy Code
#include <iostream>

using namespace std;
class add
{
    int a,b;
    public:
    add(); 
    //add(int);
    add(int,int=0); //Default Argument Constructor
    void print();
};
add::add()
{
    a=2;
    b=5;
}

add::add(int x,int y)
{
    a=x;
    b=y;
}
void add::print()
{
    cout<<"\n"<<"a="<<a<<"\n"<<"b="<<b<<"\n"<<"Sum="<<a+b<<"\n";
}
int main()
{
    add a1;
    a1.print();
    add a2(10);
    a2.print();
    add a3(10,20);
    a3.print();

    return 0;
}

Output

a=2
b=5
Sum=7

a=10
b=0
Sum=10

a=10
b=20
Sum=30

Copy Constructor


It is used to define constructor function which is used to initialize a new object with the help of object of same class in other words copy constructor is a constructor which take a object of same class as an argument.Copy constructor override the process of copy initilization.

Example:


Copy Code
#include <iostream>
using namespace std;
class add
{
    int a,b;
    public:
    add();
    add(int);
    add(int,int);
    add(add &);  // Declare Copy Constructor
    void print()
    {
        cout<<"\n"<<"a="<<a<<"b="<<b<<"\n";
    }
};
add::add(add &x)  // Definition of Copy Constructor
    {
        a=x.a;
        b=x.b;
    }
add::add()
{
    a=0;
    b=0;
}
add::add(int s)
{
    a=s;
    b=s;
}
add::add(int p,int q)
{
    a=p;
    b=q;
}

int main()
{
   add A1;
   add A2(10);
   add A4(10,20);
   add A5=A4; //Call copy constructor implicitly 
   A5.print();
   add A6=A1;  //Call copy constructor implicitly 
   A6.print();
   add A7=A2;  //Call copy constructor implicitly 
   A7.print();
   
    return 0;
}

Output

a=10 b=20
a=0 b=0
a=10 b=10

Destructor


A destructor is used to destroy the object that have been created by the constructor Like a constructor the destructor is a member function whose name is the same as the class name but is preceded by a tilde (~)
A destructor never takes any argument nor does it return any value.It will be called by compiler to cleanup strong that are no longer accessible. It is good practice to declare destructor in a program since it release memory space for future use.

Example:

~Add()
{
cout<<"Iam Destructor";
}

No comments:

Post a Comment