Hint
A vector of unique_ptr<Employee>; mark the salary() methods with override. Do not forget the virtual destructor.
Solution (C++)
#include <bits/stdc++.h>
using namespace std;
class Employee {
protected:
string name;
public:
Employee(string n) : name(n) {}
virtual long long salary() const = 0;
string getName() const { return name; }
virtual ~Employee() = default;
};
class Permanent : public Employee {
long long amount;
public:
Permanent(string n, long long a) : Employee(n), amount(a) {}
long long salary() const override { return amount; }
};
class Hourly : public Employee {
long long hours, price;
public:
Hourly(string n, long long h, long long p) : Employee(n), hours(h), price(p) {}
long long salary() const override { return hours * price; }
};
int main() {
int n;
cin >> n;
vector<unique_ptr<Employee>> staff;
for (int i = 0; i < n; i++) {
char type;
string name;
cin >> type >> name;
if (type == 'P') { long long a; cin >> a; staff.push_back(make_unique<Permanent>(name, a)); }
else { long long h, p; cin >> h >> p; staff.push_back(make_unique<Hourly>(name, h, p)); }
}
long long total = 0;
for (const auto& e : staff) {
cout << e->getName() << " " << e->salary() << "\n";
total += e->salary();
}
cout << "total " << total << "\n";
return 0;
}