Syntax rules for writing constructor functions
- Its name must be similar as that of the class to which it belongs.
- It is declared with no return type (not even void). However, it will implicitly return a temporary copy of the instance itself that is being formed.
- It cannot be declared static (a function which does not belong to a particular example), cost( in which you cannot make changes).
- It should have public or protected access within the class. Only in very rare circumstances the programmers declare it in private section.
e.g.
class boxclass
{
public :
boxclass ( int x1, int y1, int x2, int y2);
void disp(void);
private :
int x1, y1;
int x2, y2 ;
};
boxclass::boxclass(int ax1,int ay1, int ax2, int ay2)
{
x1 = ax1 ;
y1 = ay1 ;
x2 = ax2 ;
y2 = ay2 ;
}