Topics : Constructor Destructor Use Of objects as function arguments
Constructor: What is the constructor?
A constructor is a "special" member function which initializes the objects Of class.
The constructor is like a function used to initialize value.
The difference between function and constructor is function have the return type and constructor not have the return type.
And the function is invoked when we call but the constructor invoked we object is created.
Properties Of constructor:
The constructor has invoked automatically whenever an object Of class is created.
Constructor name must be same as class name.
Constructors should be declared in the public section because the private constructor cannot be invoked outside the class so they are useless.
Constructors do not have return types and they cannot return values, not even void.
Constructors cannot be inherited, even though a derived class can call the base class constructor.
Constructors cannot be virtual.
An object with a constructor cannot be used as a member Of a union.
They make implicit calls to the operators new and delete when memory allocation is required.
Types of constructor :
There are mainly three types Of constructors as follows:
1. Default constructor:
The default constructor is the one which invokes by default when object Of the class is created.
default constructor not have any parameter .
If they have then it is not Default constructor.
It is generally used to initialize the value Of the data members.
It is also called no argument constructor.
Example:
class integar
{
int m, n;
public :
integer ( ) / / Default constructor
{
m=n=0;
}
};
2. Parameterized constructor
Constructors that can take arguments are called parameterized constructors.
Sometimes it is necessary to initialize the various data elements Of different objects with different values when they are created.
We can achieve this objective by passing arguments to the constructor function when the objects are created.
Example:
class integar
{
int m, n;
public :
integer (int a,int b ) / /Parameterizedconstructor
{
m=a;
n=b;
}
};
3. Copy constructor
A copy constructor is used to declare and initialize an object from another object.
For example, integer (integer &i); OR integer 12 (11) ;
Constructor which accepts a reference to its own class as a parameter is called copy constructor.
Example:
class Integer
{
int m,n;
public:
integer(rectangle &x)// Copy constructor
{
m=x.m;
n=x.n;
}
};
Default constructor , Parameterized constructor and Copy constructor example :
#include<iostream.h>
#include<conio.h>
class integar
{
int m, n;
public :
integer ( ) / / Default constructor
{
m=n=0;
}
integer (int a,int b ) / / Parameterizedconstructor
{
m=a;
n=b;
}
integer(rectangle &x)// Copy constructor
{
m=x.m;
n=x.n;
}
};
void main()
{
integar r1;// Default constructor
integar r2(5,10);// Parameterizedconstructor
integar r3(r2);// Copy constructor
getch();
}
Destructor :
• The destructor is used to destroy the objects that have been created by a constructor.
• The destructor is a member function whose name must be same as class name but is preceded by a tilde
• A destructor will have an exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. The destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.
• Destructor never takes any argument nor it returns any value nor it has the return type.
• Destructor removes the space which is allocated to the constructor.
• If the class have any constructor then only we can use the destructor.
• When constructor executed then destructor runs. its means first constructor runs then destructor runs.
• The destructor is invoked automatically by the compiler upon exit from the program.
• The destructor should be declared in the public section
Example :
#include<iostream.h>
#include<conio.h>
class integar
{
int m, n;
public :
integer ( ) / / Default constructor
{
m=n=5;
}
~integer()// ~disconstructor
{
}
};
void main()
{
integar r1;// constructor and disconstructor
getch();
}
Use Of objects as function arguments:
Like any other data type function may be used as a function argument. It can be done in following two ways:
1. A copy Of the entire object is passed to the function
This method is call pass by value.
Since copy Of the object is passed to the function, any changes made to the object inside the function do not affect the object used to call the function.
Only the address Of the object is transferred to the function.
2. This method is call pass by value.
This method is called pass-by-reference.
When an address Of the object is passed, the called function works directly on the actual object used in the call.
This means that any changes made to the object inside the function will reflect in the actual object.
This method is more efficient because it requires passing only addresses Of the object, not an entire object.