What is Constructor and write their types with example.
or
Explain Constructor and their types with example.
Definition:- A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same name as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.
A constructor is declared and defined as follows:
class integer:
{
int m,n;
integer();
..............
..............
};
integer::integer()
{
m=0;
n=0;
}
int main()
{
integer obj1;
.................
.................
}
integer obj1;=> not only creates obj1 but also initializes its data members m and n to zero. There is no need to write any statement to invoke the constructor function.
Characteristics of Constructor :-
i)They should be declared in the public section.
ii)They are invoked automatically when the object are created.
iii)They do not have return type, not even void.
iv)They cannot be inherited, though a derived class can call the base class constructor.
v)Like other c++ functions, they can have default arguments.
vi)Constructor cannot be virtual.
vii)We cannot refer to their addresses.
viii)They make "implicit calls" to the operators new and delete when memory allocations is required.
TYPES OF CONSTRUCTORS:-
1.Default Constructor.
2.Parameterized Constructor.
3.Copy Constructor.
1.Default Constructor:- A constructor that accepts no parameters is called the default constructor.
Program:-(Example)
#include<iostream.h>
#include<conio.h>
class item
{
int m,n;
public:
item()
{
m=10;
n=20;
}
void put();
};
void item::put()
{
cout<<m<<n;
}
void main()
{
item t;
t.put();
getch();
}
2.Parameterized Constructors:-The constructor that take parameter are called parameterized constructor.
#include<iostream.h>
class item
{
int m,n;
public:
item(int x, int y)
{
m=x;
n=y;
}
};
When a constructor has been parameterized, the object declaration statement such as item t; may not work. We must pass the initial values as arguments to the constructor function when an object is declared. This can be done in 2 ways: item t=item(10, 20);//explicit calls item t(10,20); //implicit calls
Eg:-
#inlcude<iostream.h>
#include<conio.h>
class item
{
int m,n;
public:
item(int x,int y)
{
m=x;
n=y;
}
void put;
};
void item::put()
{
cout<<m<<n;
}
void main()
{
item t1(10,20);
item t2=item(20,30);
t1.put();
t2.put();
getch();
}
3.Copy constructor:-A copy constructor is used to declare and initialize an object from another object.
Eg:-
item t2(t1);
or
item t2=t1;
1. The process of initializing through a copy constructor is known as copy initialization.
2. t2=t1 will not invoke copy constructor t1 and t2 are objects, assigns the values of t1 to t2.
3.A copy constructor takes a references to an object of the same class as itself as an arguments.
Program:-
#include<iostream.h>
#include<conio.h>
class sample
{
int n;
public:
sample()
{
n=0;
}
sample(int a )
{
n=a;
}
sample(sample &x)
{
n=x.n;
}
void display()
{
cout<<n;
}
};
void main()
{
smaple A(100);
sample B(A);
sample C=A;
sample D;
D=A;
A.display();
B.display();
C.display();
D.display();
}
Output:
100 100
100 100
Multiple Constructor in a Class: Multiple constructor can be declared in a class. There can be any number of constructor in a class.
Program:
#inlcude<iostream.h>
#include<conio.h>
class complex
{
float real,img;
public:
complex()//default constructor
{
real=img=0;
}
complex(float r)//single parameterized constructor
{
real=img=r;
}
complex(float r,float i)//two parameter parameterized constructor
{
real=r,img=i;
}
complex(complex &c)//copy constructor
{
real=c.real;
img=c.img;
}
complex sum(complex c)
{
complex t;
t.real =real+c.real;
t.img=real+c.img;
return t;
}
void show()
{
if(img>0)
cout<<real<<"+i"<<img<<endl;
else
}
};
{
img=-img;
cout<<real<<"-i"<<img<<endl;
}
void main()
{
complex c1(1,2);
complex c2(2,2);
complex c3;
c3=c1.sum(c3);
c3.show();
}
What is Destructor or Explain Destructor with program?
Destructor:- A destructor is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor is a member function whose name is the same as the class name but it is preceded by a tilde. Eg: ~item() {}
1.A destructor never takes any arguments nor does it return any value.
2.It will be invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible.
3. It is a good practice to declare destructors in a program since it release memory space for future use.
Program:-
#include<iostream.h>
#include<conio.h>
class Marks
{
public:
int maths;
int science;
//constructor
Marks()
{
cout<<"Inside Constructor"<<endl;
cout<<"C++ Object created"<<endl;
}
//Destructor
~Marks()
{
cout<<"Inside Destructor"<<endl;
cout<<"C++ Object destructed"<<endl;
}
};
int main()
{
Marks m1;
Marks m2;
return 0;
}
Output:
Inside Constructor
C++ Object created
Inside Constructor
C++ Object created
Inside Destructor
C++ Object destructed
Inside Destructor
C++ Object destructed.