Declare the class Employee, consisting of data members are emp_number and emp_age. Invoke a null constructor Employee() when an object is created, the parameterized constructor Employee(en, eg) to assign values for the data members, show() member function is used to display the information of Employee. Finally free the resources of data objects using destructor member function.
#include <iostream>
using namespace std;
class Employee
{
public:
// default constructor
Employee() {
emp_number = new int;
*emp_number = 1;
emp_age = new float;
*emp_age = 18;
}
// parameterized constructor
Employee (int n, float a){
emp_number = new int;
*emp_number = n;
emp_age = new float;
*emp_age = a;
}
// destructor
~Employee() {
delete emp_number;
delete emp_age;
}
void show() {
cout << "Employee number:" << *emp_number << endl;
cout << "Employee age:" << *emp_age << endl;
cout<<"*********************************"<<endl<<endl;
};
private:
int *emp_number;
float *emp_age;
};
int main()
{
// default constructor
Employee firstEmp;
firstEmp.show();
// parameterized constructor
Employee secondEmp(77, 20.5);
secondEmp.show();
return 0;
};
Comments
Leave a comment