Respuesta :
Answer:
Following are the class employee in which we create a three constructor to initialize variables.
class employee// class
{
public:
int ID,age1 ; // variables
string name1;
employee() // constructor initialize the member with 0 when no constructor is called
{
name1="0";
age1=0;
ID=0;
cout<<ID<<name1<<age1;
}
employee(int id ,string name ,int age ) // constructor 2
{
ID=id;
name1=name;
age1=age;
cout<<ID<<name1<<age1<<endl;
}
// Copy constructor
employee(const employee(&p2) ) // constructor3
{
ID= p2.ID;
name1 = p2.name1;
age1=p2.age1;
cout<<ID<<name1<<age1<<endl;
}
};
Explanation:
Here we create a class employee and create a three variable ID,age1 and name1 initially we create a default constructor in this we set all variable to 0 this constructor is call when doesn't provide any parameter to constructor after that we create a parametrized and copy constructor.In the main function we call these constructor .
Following are the program in c++ language
#include <iostream> // header file
#include <string>
using namespace std;
class employee// class
{
public:
int ID,age1 ; // variables
string name1;
employee() // constructor initialize the member with 0 when no constructor is called
{
name1="0";
age1=0;
ID=0;
cout<<ID<<name1<<age1;
}
employee(int id ,string name ,int age ) // constructor 2
{
ID=id;
name1=name;
age1=age;
cout<<ID<<name1<<age1<<endl;
}
// Copy constructor
employee(const employee(&p2) ) // constructor3
{
ID= p2.ID;
name1 = p2.name1;
age1=p2.age1;
cout<<ID<<name1<<age1<<endl;
}
};
int main() // main function
{
cout<<"first constructor"<<endl;
employee e; // call constructor doesn't provide a parameter
cout<<endl<<" second constructor"<<endl;
employee e1(13,"ss",67);// call the second constructor
cout<<" third constructor"<<endl;
employee p2 =e1;// call third constructor
return 0;
}
Output:
first constructor
000
second constructor
3ss67
third constructor
3ss67