Polymorphism exercise

The exercise involves making a database of employees, using a single vector, and saving the database on a file.

All employees have a first and last name, and a salary. There are three kinds of employees, each with some distinct attributes:

1) Manager
- number of meetings per week
- number of vacations per year

2) Engineer
- a value specifying whether they know C++
- years of experience
- string denoting their type (i.e "mechanical", "electric", "software"

3) Researcher
- string specifying the school they got their PhD from
- string specifying topic of PhD Thesis

The database is, like I said, supposed to be saved on a single vector variable, and it should be able to add or delete an employee, and save itself on a file.

I wrote a little of the class declarations, but I'm lost on the methods themselves. Here's the code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Header.h

#ifndef HEADER_H
#define HEADER_H

#include <string>
#include <vector>

class Employee
{
public:


protected:
	std::string mFirstName;
	std::string mLastName;
	float mSalary;
};

class Manager : public Employee
{
public:


protected:
	int mNumMeetings;
	int mNumDays;
};

class Engineer : public Employee
{
public:


protected:
	bool mC_Knowledge;
	int mNumYears;
	std::string mType;
};

class Researcher : public Employee
{
public:


protected:
	std::string mSchool;
	std::string mThesis;
};


#endif // HEADER_H 


I'm supposed to use polymorphism and all it entails, like virtual functions, pure virtual functions and maybe abstract classes.

I don't know what methods I should even have for this thing, let alone if they should be virtual or pure virtual, or even if I need constructors.

So far, I've thought of making a single pure virtual function, that will be implemented in the derived classes as a function that simply assigns the necessary parameters to the attributes of each specialized employee. But then, I'm not sure if you can use upcasting with an abstract class.

I've never written large exercises like this before, so I lack the ability to properly make a good structure...I would like some tips on what methods I should use. Mind you, I'm asking for descriptions of what they should do, not code :P

Thanks.
Last edited on
Me personally..

I would start by breaking my problem down to smaller problems.
and if it was not clear at that point to me.

Then I would just start to code the problem the hard way (brute force)
e.g. separate classes and methods the start to note what can overlap in terms of functions then that would tell me what should and should not be polymorphic. Also the process might lend to what classes should be my base and what classes I will derive.

Edit: what is jumping out at me right away is your

print
load file
save file
add entry

functions are going to be the things that are going to need to be polymorphic.

Last edited on
From what youve said, you'll need to make the Employee class an abstract one, create getters and setters, probably as friends. For the vector, once the base class is abstract, you can create a vector of pointers to Employee, and access all the subclasses from there. I'll recommend a separate mechanism for the file saving process.

Aceix.
Topic archived. No new replies allowed.