Default constructors
The default constructor is a particular member function that is invoked through the C++ compiler without any argument for initializing the object class. C++ compiler automatically produces default constructors if it is not defined.
The common form is class user_name
{
private:
-------------
-------------
protected:
-------------
-------------
public:
user_name ( ); //default constructor
-------------
-------------
}
user_name :: user_name ( ) //without any parameters
Example:
// program to elaborate the default constructor
# include<iostream.h>
class student
{
private:
char name[20];
int rollno;
char sex;
float height;
float weight;
public:
};
student( );
void display( );
student :: student( )
{
name[0] = '\0';
rollno = 0; sex = '\0';
height = 0;
weight = 0;
}
void student:: display ( )
{
cout <<"name ="<< name<<endl;
cout <<"rollnumber ="<< rollnoname<<endl;
cout <<"sex ="<< sex<<endl;
cout <<"height ="<< height<<endl;
cout <<"weight ="<< weight<<endl;
}
void main( )
{
student a;
cout <<"description of default constructor\n";
a.display( );
}