Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

Class Constructors and destructors in C++

 

Class Constructors and destructors in C++

In this C++ tutorial you will learn about Class Constructors and destructors in C++ viz., Constructors, What is the use of Constructor, General Syntax of Constructor, Destructors, What is the use of Destructors and General Syntax of Destructors.



Constructors:

What is the use of Constructor

The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.


General Syntax of Constructor

Constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:


<class name> { arguments};


The default constructor for a class X has the form


X::X()


In the above example the arguments is optional.

The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator.

There are several forms in which a constructor can take its shape namely:


Default Constructor:

This constructor has no arguments in it.  Default Constructor is also called as no argument constructor.


For example:


class Exforsys
{
    private:
        int a,b;
    public:
        Exforsys();
        ...
};

Exforsys :: Exforsys()
{
    a=0;
    b=0;
}


Copy constructor:

This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

For example to invoke a copy constructor the programmer writes:


Exforsys e3(e2);
or
Exforsys e3=e2;


Both the above formats can be sued to invoke a copy constructor.

For Example:


#include <iostream.h>
class Exforsys()
{
    private:
        int a;
    public:
        Exforsys()
        { }
        Exforsys(int w)
    {
        a=w;
    }
    Exforsys(Exforsys& e)
    {
        a=e.a;
        cout<<” Example of Copy Constructor”;
    }
    void result()
    {
        cout<< a;
    }
};


void main()
{
    Exforsys e1(50);
    Exforsys e3(e1);
    cout<< “\ne3=”;e3.result();
}



In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is


Example of Copy Constructor
e3=50


Some important points about constructors:


  • A constructor takes the same name as the class name.
  • The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
  • No return type is specified for a constructor.
  • The constructor must be defined in the public. The constructor must be a public member.
  • Overloading of constructors is possible. This will be explained in later sections of this tutorial.

Destructors

What is the use of Destructors

Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.


General Syntax of Destructors

~ classname();


The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.




Some important points about destructors:


  • Destructors take the same name as the class name.
  • Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
  • The Destructor does not take any argument which means that destructors cannot be overloaded.
  • No return type is specified for destructors. 

For example:


class Exforsys
{
    private:
        ……………
    public:
        Exforsys()
        { }
        ~ Exforsys()
        { }
}




Read Next: Concepts of Arrays in C++



 

 

Comments


soumyapallai said:

  I really liked the detailed information on this topic....
October 25, 2008, 7:19 am

james mburu said:

  I really liked the detailed information on this topic....
October 27, 2008, 2:32 am

babbu said:

  this information is very useful...thanx
November 27, 2008, 2:23 pm

wondwoosen tadesse said:

  I RELLY LIKE THE INFORMATION GIVEN. It describe detailed information about the topic
December 3, 2008, 8:23 am

Manoraj said:

  why the name of Constructor is same has class name ?
January 4, 2009, 5:33 am

ashish said:

  #include<iostream.h>
class calc
{
private:
int num1,num2,res;
public:
calc(int,int);
void sum();
};
calc::calc(intx,inty)
{
num1=x;
num2=y;
res=0;
}
void calc::sum()
{
res=num1+num2;
cout<<"the total of two numbers is:"<<res;
}
int main()
{
int var1,var2;
cout<<"val1";
cin>>var1;
cout<<"val2";
cin>>var2;
calc cl(var1,var2);
cl.sum();
return 0;
}
January 19, 2009, 12:24 am

Amar said:

  It's a good explaination...
January 20, 2009, 3:51 am

subash said:

  i wanna receive the content .
February 2, 2009, 1:25 pm

chandana said:

  It is a good explanation but destructor is mentioned very little.It is not explained detailed.If it could have detailed more.we will be able to learn more.But constructor is good.
February 19, 2009, 6:09 am

lakuma said:

  What is significance of the destructors in the case of pure virtual function?
March 4, 2009, 7:37 am

MAHINDER KATHOR said:

  #include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
abc(int x,int y)
{
a=x;
b=y;
}
abc(abc&s)
{
a=s.a;
b=s.b;
}
void disp()
{
cout<<a<<b;
}
};
void main()
{
clrscr();
abc a1(4,5);
abc a2=a1;
a1.disp();
getch();
}
March 10, 2009, 4:35 am

SORABH said:

  When we pass object as argument to a function, then whether constructor or destructor are called or not.
March 25, 2009, 11:50 am

jaikumar said:

  what is the differnce between privat and public access specifiers........

#include
class calc
{
private:
int num1,num2,res;
public:
calc(int,int);
void sum();
};
calc::calc(intx,inty)
{
num1=x;
num2=y;
res=0;
}
void calc::sum()
{
res=num1+num2;
cout<<"the total of two numbers is:"<
}
int main()
{
int var1,var2;
cout<<"val1";
cin>>var1;
cout<<"val2";
cin>>var2;
calc cl(var1,var2);
cl.sum();
return 0;
}
[in this prog if we not use private wat will be the output]what is the main use of private and publis....plz help me am in great confusion
July 15, 2009, 8:43 am

preethy said:

  What about recursive constructors? Please explain about it!
August 11, 2009, 9:22 am

amritlal behera said:

  what is parameterized constructor? explain with example.
October 15, 2009, 5:44 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape