C++ help. I just need to know what the array would look like from this code: Giv
ID: 3580057 • Letter: C
Question
C++ help. I just need to know what the array would look like from this code:
Given a class “Employee” with a method called “getBasePay()”, and 2 subclasses, “SalaryEmployee” and “ContractEmployee” that each define their own “getBasePay(), provide the code that creates an array called “payroll” of 3 Employees. The first 2 are SalaryEmployees and the third is a ContractEmployee. Then, provide the code that goes through the array and prints each Employees base pay that uses the appropriate “getBasePay()” method based on array element types
I need help with the array part. i am pretty bad at designing arrays. Thanks
Explanation / Answer
/**
C ++ program that demonstrates the creation of the pointer array for
Employee class and override getBasePay in child classes
SalaryEmployee and ContractEmployee .
*/
//test.cpp
//include header file
#include<iostream>
using namespace std;
//Base class Employee
class Employee
{
public:
//declare a virtual ,method getBasePay
virtual int getBasePay()
{
return 0;
}
};
class SalaryEmployee : public Employee
{
public:
//Overrite the method getBasePay
//that return 10000 for salary employee
int getBasePay()
{
return 10000;
}
};
class ContractEmployee : public Employee
{
public:
//Overrite the method getBasePay
//that return 8000 for salary employee
int getBasePay()
{
return 8000;
}
};
int main()
{
//Cretae an array of pointer type Employee class
Employee*emp[3];
//Set salryEmployee object
emp[0]=new SalaryEmployee();
//Set salryEmployee object
emp[1]=new SalaryEmployee();
//Set ContractEmployee object
emp[2]=new ContractEmployee();
//Print getBasePay through emp pointer array
for(int index=0;index<3;index++)
cout<<emp[index]->getBasePay()<<endl;
system("pause");
return 0;
}
sample output:
10000
10000
8000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.