Você está na página 1de 14

 Constructor is a special member function which has the same name as its

class name.
 It can be defined either inside the class definition or outside the class
definition.
 The symbol used to declare an constructor is ( ).

Declaration and definition of a class


Eg :consider a constructor define and declare for a object of class box
class box
{ private : int length;
int width;
int height;
public: box(void) //constructor
{ length=0;
width=0;
height=0;
}
}
A constructor has same name as that of its class
name.
 It is declared in the public section of the class
definition.
 It is invoked automatically as soon as the class object
is created.
 It does not return any value so return type not
associated with its definition
 They can be overloaded.
 The constructor are not inherited
Default constructor: A constructor that accepts
no parameter is called the default constructor.

 Constructor for class XYZ is XYZ::XYZ().


 If the constructor is not defined for this then
the compiler will provide a default
constructor, so the statement is XYZ::X().
Eg: This program creates an object and initialize its
data members using default constructor.
class box
{
int length;
int width;
int height;
Public:box(void)
{ length=0;
width=0;
height=0;
}
void print(void)
{cout<<“length=“<<length<<endl;
cout<<“width=“<<width<<endl;
cout<<“height=“<<height<<endl;
};
void main()
{ box.bob;\\default constructor
bob.print();
}
A class may contain multiple constructors in a class that is called
as overloaded constructors
Eg:#include<iostream.h>
class point()
{
private: int x;
int y;
Public:point(void)//constructor 1
{ x=0;
y=0;
}
point(int xpos,int ypos)//constructor 2
{ x=xpos;
y=ypos;
void display(void)
{
cout<<“X=“<<x<<“\t”;
cout<<“Y=“<<y<<“\t”;
}
void main()
{
Point p1,p2(10,20);
cout<<“point p1 is “<<endl;
p1.display();
cout<<“point p2 is “<<endl;
p2.dispaly();
}
A copy constructor is one which constructs
another object by copying the member of the
given object
or
The process of copying one object from another
object is called copy constructor.
#include<iostream.h>
class point
{private:int x;
int y;
public: point()//default constructor
{
}
point(int xp,int yp)
{ x=xp;
y=yp;
}
point(point &p)//copy constructor
{ x=px;
y= py;
}
void display(void)
{cout<<“x”<<x<<“\t”;
cout<<“y<<“<<y<<“\t;
}
void main()
{point p1(20,30);
Point p2(p1);
Point p3;
P1.display();
P2.display();
}
It is also a member function which has the same
name as that of its class name. It destructs an object. It
does not take any return value or does not take any
return argument.

Characterstics of a destructor
 Its functions are invoked automatically when objects are
destroyed.
 We can have only one destructor in a class
 They cannot be overloaded or inherited
 It does not return a value
class sample
{ int i, int j;
~sample( )
{ cout<<“Destructing”;
}
public : sample( ){i=0;j=0;}
void memb1(void);
void memb2(void);
};
Q
void sample::memb1(void)
{ sample s1;
}
void sample::memb2(void)
{sample s2;
}
void doc(void)
{sample s3;
}
void main()
{ sample s4;
}

Você também pode gostar