I have a very specific problem, which leads to the more fundamental understanding of the polymorphism - At least I think so.
I am making a employee-handling program from an assignment in a book. I am to make a superclass or should I say interface called Employee. This superclass has three child-classes which has some additional member variables.
In my main file, I keep the employees in a vector (vector<Employee>) variable. At some point I want to make it possible to delete employees from the vector.
The way I wanted to do that is to print out the containment of my vector, and then let the user pick the employee to be deleted.
Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
#include <iostream>
#include <fstream>
class Employee
{
public:
virtual ~Employee();
protected:
Employee();
void informMeNow();
void printToFile(std::ostream& file);
std::string eFirstname;
std::string eLastname;
int eSalary;
private:
};
#endif // EMPLOYEE_H
This header has no interesting implementation.
One of the subclasses header file:
Engineer.h:
#pragma once
#include "employee.h"
enum engineerType {mechanical, electric, software};
class Engineer : public Employee
{
public:
Engineer(void);
~Engineer(void);
void informMeNow();
void printToFile(std::ostream& file);
private:
engineerType engType;
bool knowsCpp;
int yearsOfExperience;
};
The Engineers implementation:
Engineer.cpp:
#include "Engineer.h"
using namespace std;
Engineer::Engineer(void)
{
int choice = 0;
while(choice < 1 || choice > 3)
{
cout << "Engineer is either mechanical, electric or software. Enter 1, 2 or 3: "; cin >> choice;
switch(choice)
{
case 1:
engType= mechanical;
break;
case 2:
engType= electric;
break;
case 3:
engType = software;
break;
default:
cout << "You dumbfuck!" << endl;
cout << "You need to enter one of three numbers! don't bug me again!" << endl;
cout << "Try again!" << endl;
break;
}
}
cout << "How many years of experience does this engineer have?: "; cin >> yearsOfExperience;
cout << "Does this engineer know c++? 'true' or 'false': "; cin >> knowsCpp;
}
Engineer::~Engineer(void)
{
//No dynamic memory to free
}
void Engineer::printToFile(ostream& file)
{
file << "------------------------------------------------" << endl;
file << "Name: " << eFirstname + " " + eLastname << endl;
file << "Salary: " << eSalary << endl;
file << "This engineer handles the " << engType << " area" << endl;
file << "This enginner knows C++: " << knowsCpp << endl;
file << "This engineer has " << yearsOfExperience << " years of experience." << endl;
file << "------------------------------------------------" << endl << endl;
}
void Engineer::informMeNow()
{
cout << "------------------------------------------------" << endl;
cout << "Name: " << eFirstname + " " + eLastname << endl;
cout << "Salary: " << eSalary << endl;
cout << "This engineer handles the " << engType << " area" << endl;
cout << "This enginner knows C++: " << knowsCpp << endl;
cout << "This engineer has " << yearsOfExperience << " years of experience." << endl;
cout << "------------------------------------------------" << endl << endl;
}
The main file:
main.cpp:
#include <vector>
//include custom classes
#include "Manager.h"
#include "Engineer.h"
#include "Reseacher.h"
#include "Employee.h"
using namespace std;
int main()
{
vector<Employee> employees;
bool isRunning = true;
int menuChoice = 0;
cout << "*** EMPLOYEE MANAGING SYSTEM ***" << endl << endl;
while(isRunning)
{
cout << endl;
cout << "/// *** MENU *** ///" << endl;
cout << "1) Add an employee, 2) Delete an employee, 3) Save to Database" << endl; cin >> menuChoice;
switch(menuChoice)
{
case 1:
int employeeChoice;
cout << "1) Manager, 2) Engineer, 3) Reseacher: "; cin >> employeeChoice;
switch(employeeChoice)
{
case 1:
employees.push_back(Manager());
break;
case 2:
employees.push_back(Engineer());
break;
case 3:
employees.push_back(Reseacher());
break;
default:
cout << "choice not possible!" << endl << endl;
break;
}
break;
case 2:
// enter delete algorithm.
int deleteChoice;
cout << "Press enter to get the list of employees!";
system("pause");
for(unsigned int i= 0; i < employees.size(); i++)
{
employees[i].informMeNow();
}
cout << endl;
cout << "Enter integer of employee to delete: ";
cin >> deleteChoice;
employees.erase( employees.begin() + deleteChoice );
break;
default:
return 1;
break;
}
}
system("pause");
return 1;
}
This will not compile, because of the " employees[i].informMeNow() " line. I get an error whether the informMeNow() function is protected or public - which confuses me. I though that the polymorphic system would search down the hierarchy of inheritance for an equevalent public function?
I tried several other things, which I can't really remember now.
How do I access the informMeNow() function from a vector containing supers. ? I must have got the theory behind polymorphism all wrong, because this has given me a headache.
Could You please tell me what I am missing.
(Be aware that the main file is unfinished!)
Thank you in advance
Beweren

New Topic/Question
Reply



MultiQuote





|