Professional Documents
Culture Documents
Constructors and Destructors
Constructors and Destructors
class student
class student
{
{
private:
private:
int rollno;
int rollno;
float marks;
float marks;
public:
public:
student( ) //Constructor
student( ) //Constructor
{ rollno=0; marks=0.0; }
{ rollno=0; marks=0.0; }
};
};
void main()
void main()
{
{
student s1 = student(); //explicit
student s1; //implicit
call
call
}
}
IMPLICIT CALL OF CONSTRUCTOR EXPLICIT CALL OF CONSTRUCTOR
Constructor does not have any return type not even void.
1. Constructor helps to initialize the object of a class. Whereas destructor is used to destroy the instances.
3. Constructor can either accept arguments or not. While it can’t have any arguments.
Constructor is used to allocate the memory to an While it is used to deallocate the memory of an object of a
5.
instance or object. class.
Here, its name is also same as the class name preceded by the
7. The constructor’s name is same as the class name.
tiled (~) operator.
8. In a class, there can be multiple constructors. While in a class, there is always a single destructor.
10. They are often called in successive order. They are often called in reverse order of constructor.