Write a C++ class to represent an Employee. Each employee is given a name, an age, a NIC number, an address, and a salary. Furthermore:
The Employee class should keep track of how many employees (objects) have been created in the system.
Your Employee class should initialize all of its fields via a constructor.
Create function prototypes to set and get an employees zip code.
Declare a print_employee() function that prints all of an employee object's details.
(Whenever possible, use constant functions.)
#include <iostream>
#include <string>
using namespace std;
class Employee{
private:
string name;
int age;
string NICNumber;
string address;
double salary;
string zipCode;
public:
Employee(string name,int age,string NICNumber,string address,double salary){
this->name=name;
this->age=age;
this->NICNumber=NICNumber;
this->address=address;
this->salary=salary;
}
void setZipCode(string z){
this->zipCode=z;
}
string getZipCode(){
return this->zipCode;
}
void print_employee()const{
cout<<"Name: "<<this->name<<"\n";
cout<<"Age: "<<this->age<<"\n";
cout<<"NIC number: "<<this->NICNumber<<"\n";
cout<<"Address: "<<this->address<<"\n";
cout<<"Salary: "<<this->salary<<"\n";
cout<<"Zip code: "<<this->zipCode<<"\n";
}
};
int main()
{
string name;
int age;
string NICNumber;
string address;
double salary;
string zipCode;
cout<<"Enter name: ";
getline(cin,name);
cout<<"Enter age: ";
cin>>age;
cin.ignore();
cout<<"Enter NIC number: ";
getline(cin,NICNumber);
cout<<"Enter address: ";
getline(cin,address);
cout<<"Enter salary: ";
cin>>salary;
cin.ignore();
cout<<"Enter zip code: ";
getline(cin,zipCode);
Employee employee(name,age,NICNumber,address,salary);
employee.setZipCode(zipCode);
employee.print_employee();
system("pause");
return 0;
}
Comments
Leave a comment