151k views
2 votes
Create a class that represents an employee. This class will have three constructors to initialize variables. If the constructor doesn't provide a parameter for a field, make it either "(not set)" or "0" as appropriate.

1 Answer

3 votes

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;

}

};

Step-by-step 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

User Hristo Deshev
by
6.4k points