Constructors:
*What is the use of Constructor?
Answer :: 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:
{ 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 XYZ
{
private:
int a,b;
public:
XYZ ();
...
};
XYZ:: XYZ ()
{
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:
XYZ e3(e2);
or
XYZ e3=e2;
Both the above formats can be sued to invoke a copy constructor.
For Example:
#include
class XYZ()
{
private:
int a;
public:
XYZ()
{ }
XYZ(int w)
{
a=w;
}
XYZ(XYZ& e)
{
a=e.a;
cout<<” Example of Copy Constructor”; } void result() { cout<< ne3="”;e3.result();" e3="50" style="text-align: center;">Destructors:
Answer :: 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:
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 XYZ
{
private:
int a,b;
public:
XYZ ();
...
};
XYZ:: XYZ ()
{
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:
XYZ e3(e2);
or
XYZ e3=e2;
Both the above formats can be sued to invoke a copy constructor.
For Example:
#include
class XYZ()
{
private:
int a;
public:
XYZ()
{ }
XYZ(int w)
{
a=w;
}
XYZ(XYZ& e)
{
a=e.a;
cout<<” Example of Copy Constructor”; } void result() { cout<< ne3="”;e3.result();" e3="50" style="text-align: center;">Destructors:
*What is the use of Destructors?
Answer:: 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.
0 comments:
Post a Comment