:
A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.
When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself. 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 :
A constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:
{ arguments};
The default constructor for a class X has the form
X::X()
In the above example, the arguments are 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. A constructor is declared without a return value, that also excludes void.
Therefore, when implemented, do not return a value:
Constructor Example :
class rectangle { // A simple class
int height;
int width;
public:
rectangle(void); // with a constuctor,
~rectangle(void); // and a destructor
};
rectangle::rectangle(void) // constuctor
{
height = 6;
width = 6;
}
The Constructor Initializer :
A constructor does not exist simply for cosmetic reasons. It can be used to initialize the member variables of an object. Therefore, a constructor provides a valuable alternative to a method initializer, the type of method we saw earlier.
To use a constructor to initialize the member variables of an object, provide as arguments the necessary variables that you intend to initialize. You do not have to initialize all member variables in the constructor, only those that need to be initialized. In fact, you should initialize only those members that you think the other objects or functions would need to provide when calling this object; this means that your object may have member variables that, either the external objects or functions do not need to modify (or access) or the member variable will be initialized later when called from the needed object or function.
There are several forms in which a constructor can take its shape namely :
Default Constructor :
A constructor is declared without a return value, that also excludes void. Therefore, when implemented, do not return a value:
//---
#include
using namespace std;
//---
struct TBook
{
public:
TBook(); // Constructor
};
//---
TBook::TBook()
{
cout << "I see a book...\n";
}
//---
int main()
{
TBook B;
return 0;
}
//---
This would produce:
I see a book…
This book constructor is a programmer created constructor and is empty. You might find it sometimes convenient to create your own constructor because, whether you create an empty constructor or not, this does not negatively impact your program but makes it more lively and allows other parts of the program to conveniently call the object using its constructor. A constructor is easily implemented once you have created one.
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 :
Sample Code-
#include <iostream>
using namespace std;
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.
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.
Constructor Overloading :
Like an ordinary method, a construction can be overloaded. This means that you can have different constructors following the rules of overloading a function. Since we saw that a constructor can be used to initialize the member variables of its object, you can use multiple constructors to apply different initializations.
Constructors in C++.
Regards,
Preeti Bagad [BE(CS)]
SW Engineer Cum Blogger
On Line Assistence :
Gtalk : PreetiB.A1Soft@gmail.com
Y! Messenger : PreetiB.A1Soft@yahoo.com
Rediff Bol : PreetiB.A1Soft@rediffmail.com