
// Company.h
#ifndef __COMPANY_H
#define __COMPANY_H
#include <string>
namespace Multi {
class Employee {
protected:
static const double baseSalary;
std::string name;
public:
Employee(const std::string &nm) {
name = nm;
}
std::string getName() const {
return name;
}
virtual double getSalary() const {
return baseSalary;
}
};
class DivisionManager: public Employee {
static const double divisionManagerSalaryFactor;
public:
DivisionManager(const std::string &nm): Employee(nm) {
}
double getSalary() const {
return baseSalary * divisionManagerSalaryFactor;
}
};
class VicePresident: public Employee {
static const double vicePresidentSalaryFactor;
public:
VicePresident(const std::string &nm): Employee(nm) {
}
double getSalary() const {
return baseSalary * vicePresidentSalaryFactor;
}
};
class President: public Employee {
static const double presidentSalaryFactor;
public:
President(const std::string &nm): Employee(nm) {
}
double getSalary() const {
return baseSalary * presidentSalaryFactor;
}
};
}
#endif // __COMPANY_H
// Company.cpp
#include "Company.h"
namespace Multi {
const double Employee::baseSalary = 100000;
const double DivisionManager::divisionManagerSalaryFactor = 1.2;
const double VicePresident::vicePresidentSalaryFactor = 2;
const double President::presidentSalaryFactor = 10;
}
// Main.cpp
#include <iostream>
#include "Company.h"
using namespace std;
using namespace Multi;
int main() {
Employee *P[4];
P[0] = new Employee("Joe");
P[1] = new DivisionManager("Jack");
P[2] = new VicePresident("Jane");
P[3] = new President("Jim");
for (int i = 0; i < 4; i++) {
cout << "The salary of " << P[i]->getName() << " is " << P[i]->getSalary() << endl;
delete P[i];
}
return 0;
}