constructor and destructor
Constructor
and destructor
Constructor
Constructor is a special member function of a class that is
executed whenever we crfeate new objects of that class. The type of constructor
are
1) default constructor
2) Parameterized constructor
3) overloaded constructor
4) copy constructor (
importent for CBSE )
1) default constructor
#include<conio.h>
#include<iostream.h>
Class
line
{
Int l;
Public :
Line();
// This is default constructor
};
Line::line() // Defining default constructor
{
cout<<”value
is not defined “;
}
Void main()
{
Line a;
getch();
}
2) Parameterized constructor
#include<conio.h>
#include<iostreamh>
Class
line
{
Int l;
Public :
stra(int len); // This is parameterized constructor
};
Void Line::stra(int
len) // Defining parameterized
constructor
{
cout<<”value
is not defined “;
}
Void main()
{
Line a;
getch();
}
3) overloaded constructor
#include<conio.h>
#include<iostreamh>
Class
line
{
Int l;
Public :
stra(int len); // the 1st constructor
stra(int len, int br) // the 2nd constructor
};
Void
Line::stra(int len) // Defining
1st constructor
{
cout<<”value
is defined “;
}
Void main()
{
Line a;
getch();
}
4) copy constructor
#include<conio.h>
#include<iostreamh>
Class
line
{
Int l;
Public :
stra(int len) // Defining 1st constructor
{
cout<<”value is not defined “;
}
stra(stra &s)
{
Cout<<”constructor
is copy constructor “; //copy
constructor
}
Void main()
{
Line
a;
getch();
}
Destructor
#include<conio.h>
#include<iostreamh>
Class
line
{
Int l;
Public :
Line()
{
Cout<<”constructor is defined “;
}
~line() //destructor is defined
{
cout<<”constructor is destructed “;
}
};
void main()
{
Line a;
getch();
}
Comments
Post a Comment