Default Constructor in C++ with example



The Default Constructor is usually a special member function which is invoked by the C++ compiler without any argument for initializing the objects of a class.
The purpose of the Default Constructor is to construct a default object of the class type.
Example:
#include<iostream.h>
#include<conio.h>
Class employee
               {
               int id;
               char name[20];
               public:
               employee()          //constructor
                              {
                              id = 110;
                              }
void get();
void put();           
};
void employee :: get()
{
cout<<”Enter the employee name =”;
cin>>name;    
}
void employee :: put()
{
               cout<<” Employee Id = ”<<id;
               cout<<” Employee Name = ”<<name;
}
void main()
{
clrscr();
               employee emp;
               emp.get();
               emp.put();
               getch();
}

No comments:

Post a Comment