Parameterized Constructor in C++ with example



By passing argument to the constructor function when the object is created. The constructor that can take arguments are called parameterized constructor. Using parameterized constructor simplifies the object’s declaration. 

Example:
#include<iostream.h>
#include<conio.h>
Class employee
               {
               int id;
               char name[20];
               public:
               employee (int t) //constructor
                              {
                              id = t;
                              }
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(101), em(102);
               emp.get();
               emp.put();
               em.get();
               em.put();
               getch();
}

No comments:

Post a Comment