Question #31956

Derive a class Programmer from Employee. Supply a constructor Programmer(string name, double salary) that calls the base-class constructor. Supply a function get_name that returns the name in the format “Hacker, Harry (Programmer)”.

Expert's answer

#ifndef CLASSES_H
#define CLASSES_H

#include <iostream>
#include <string>

using namespace std;

class Employee
{
protected:
string name;
double salary;

public:
Employee(const string& name, double salary)
{
this->name = name;
this->salary = salary;
}
};

class Programmer : Employee
{
public:
Programmer(string name, double salary) : Employee(name, salary) {}
string get_name()
{
return name + string(" (Programmer)");
}
};

#endif

int main()
{
Programmer p("Hacker, Harry", 9400);
cout << p.get_name();
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS