Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class Array<?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:of

ID: 3531628 • Letter: C

Question

Class Array<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Consider a class Employee with data members: age(an integer), id (an integer) and salary (a float), and their corresponding member functions as follows:

class Employee

{

private:

int age;

int id;

float salary;

public:

void setAge(int x);

void setId(int x);

void setSalary(float x);

int getAge( );

int getId( );

float getSalary( );

};

You need to provide the definition of the above six member functions (setAge, setId, setSalary, getAge, getId, getSalary). Write a main routine in which you create an array of Employee with 3 elements. Set the first element with the content (age=30, id = 30042554, salary=50000.00), the second element with the content (age=45, id=40041002, salary=70000.00) and the third element with the content (age=25, id=50051003, salary=30000.00) by using the first three member functions. Then, pass the array to a function, PrintEmployee(arg1, arg2), in which arg1 should be the array of Employee and arg2 is the length of the array (=3 in this case). In PrinteEmployee(arg1, arg2), write a for loop to print out the information of each employee by using the fourth through sixth member functions and cout (in this case, we have three employees in total). At the end of this function, print out the average age and salary of all the employees.

Explanation / Answer

class Employee

{

private:

int age;

int id;

float salary;

public:

void setAge(int x)

{

age=x;

}


void setId(int x)

{

id=x;

}


void setSalary(float x)

{

salary=x;

}


int getAge( )

{

return age;

}


int getId( )

{

return id;

}


float getSalary( )

{

return salary;

}

};



void PrintEmployee(Employee emp[], int arg)

{

float tAge=0, tSalary=0;

for(int i=0; i<3; i++)

{

cout<<" The age of employee no. : "<<(i+1);

cout<<emp[i].getAge()<<" ";


cout<<" The ID of employee no. : "<<(i+1);

cout<<emp[i].getId()<<" ";


cout<<" The Salary of employee no. : "<<(i+1);

cout<<emp[i].getSalary()<<" ";


tAge = tAge + emp[i].getAge();

tSalary = tSalary + emp[i].getSalary();

}


cout<<" The average Age is : "<<(tAge/3);

cout<<" The average Salary is : "<<(tSalary/3);

}



void main( )

{

Employee obj[3];

int p;

float q;

for(int i=0; i<3; i++)

{

cout<<" Enter the age of employee no. : "<<(i+1);

cin>>p;

obj[i].setAge(p);


cout<<" Enter the ID of employee no. : "<<(i+1);

cin>>p;

obj[i].setId(p);


cout<<" Enter the salary of employee no. : "<<(i+1);

cin>>q;

obj[i].setSalary(q);

}


PrintEmplyee( obj, 3);

}