I am learning about polymorphism in C++ and decided to implement an example. Is there anything I need to fix in my code? This compiles and runs.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
class Employee
{
public:
Employee():salary(1000){}
virtual ~Employee(){}
virtual void Talk() const {cout << "Employee says: ";}
protected:
int salary;
};
class Programmer : public Employee
{
public:
void Talk() const {cout << "I am a Programmer.\n";}
void Compiling() const {cout << "My code is compiling...\n";}
};
class SalesPerson : public Employee
{
public:
void Talk() const {cout << "I am a SalesRep.\n";}
void Selling() const {};
};
int main()
{
const int numEmployees = 3;
Employee* Company[numEmployees];
Employee* pEmployee;
int choice,i;
for(i=0; i<numEmployees; i++)
{
std::cout << "(1) Programmer (2) SalesRep: ";
cin >> choice;
if(choice == 1)
pEmployee = new Programmer;
else
pEmployee = new SalesPerson;
Company[i] = pEmployee;
}
cout << endl;
for(i = 0; i<numEmployees;i++)
{
Company[i]->Talk();
Programmer *pCoder = dynamic_cast<Programmer *> (Company[i]);
if(pCoder)
pCoder->Compiling();
cout << endl;
}
return 0;
}
Selling()
supposed to be empty? What is it doing? – Quaxton Hale Oct 6 '14 at 6:24