Você está na página 1de 2

Constructor

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. A constructor is declared without a return value, Therefore, when implemented, do not return a value: that also excludes void.

There are some important qualities for a constructor to be noted: 1) 2) 3) 4) 5) Constructors have the same name as that of a class. Constructors do not return values not even void Constructors can be parameterized. Constructors cannot be static , as static member functions belong to the class only. Constructor cannot be virtual function.

Types of Constructor

1. 2. 3. 4.

Default Constructor Parameterized Constructor Copy Constructor Dynamic Constructor

Default Constructor:

This constructor has no arguments in it. The default Constructor is also called as the no argument constructor.

Parametrized Constructor

We can also send arguments to the constructor. By sending arguments we can initialize the object with desired values at the time of object creation.

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.

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example:
class X { public: // Constructor for class X X(); // Destructor for class X ~X(); };

A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static. A destructor can be declared virtual or pure virtual. If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.

Você também pode gostar