Abstraction and Polymorphism, question.

Hello everyone,

I am working on a project for school and had a general question. I researched the error I am getting upon compilation and found out the reason for the error.

I want this to be at the top of my post in case my instructor finds this post online! I am not asking for an answer or asking anyone to write my program for me. I am asking for clarification and giving an example of the code I am using and hoping for clarity on this topic. The rest of the project is easy "smiley face here".

I am however unsure on the solution. I am working a bit ahead of myself so please do not pay too much attention to my driver. I was just working on creating iterators and also my vector of employee objects.

I found this: "Any class with one or more pure virtual functions is an abstract class, and it is illegal to instantiate an object of it. Trying to do so will cause a compile-time error. Putting a virtual function in your class signals two things to clients of your class:
*Don't make an object of this class, derive from it.
*Make sure you override the pure virtual function."

source: http://www.cplusplus.com/forum/general/4153/

So I am still unsure on how to correct it to fix this issue.

I am asking for clarification on how to correctly work around Abstraction with Polymorphism.

Please keep in mind my code is not complete and I am still working on it.

Employee.h
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>

using std::string;

class Employee
{
protected:

	int intEmpId;
	double dblEmpPay;
	string strEmpId;
	string strEmpName;
	string strEmpStatus;


public:

	Employee();
	~Employee();

	virtual void setEmpId(int id) = 0;
	virtual void setEmpName(string name) = 0;
	virtual void setEmpPay(double pay) = 0;
	virtual void setEmpStatus(char a, char b) = 0;


};

class PartTime: public Employee
{
private:

public:

	PartTime()
	{

	}

	~PartTime();

	void setEmpId(int id)
	{

	}

	void setEmpName(string name)
	{

	}

	void setEmpPay(double pay)
	{

	}

	void setEmpStatus(char a, char b)
	{

	}

};

class FullTime: public Employee
{
private:

public:

	FullTime()
	{

	}

	~FullTime();

	void setEmpId(int id)
	{

	}

	void setEmpName(string name)
	{

	}

	void setEmpPay(double pay)
	{

	}

	void setEmpStatus(char a, char b)
	{

	}
};

class Contractor: public Employee
{
private:

public:

	Contractor()
	{

	}

	~Contractor();

	void setEmpId(int id)
	{

	}

	void setEmpName(string name)
	{

	}

	void setEmpPay(double pay)
	{

	}

	void setEmpStatus(char a, char b)
	{

	}
};

driver.cpp
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include "Employee.h"

using namespace std;

void main()
{
	vector<Employee> vEmp;

	vector<Employee>::const_iterator iBegin = vEmp.begin();
	vector<Employee>::const_iterator iBegin = vEmp.end();

	fstream fstObj;
	string temp = "";
	
	fstObj.open("employees.dat", fstream::in | fstream::out | fstream::app);

	while(fstObj.good())
	{

		getline(fstObj, temp);

		//(int)temp.find('|');
	}
	

}


I would say from this:
 
virtual void setEmpId(int id) = 0;


you have to decide if its
1
2
3
virtual void setEmpId() = 0;
// or 
virtual void setEmpId(int id) {}

cbeginplusplus,

Thank you for your response. After looking at your answer I looked into this subject even further. I read the entire document here: http://www.cplusplus.com/doc/tutorial/polymorphism/

I found out that I would have ran into an issue later on trying to instantiate an object from an Abstract class. I also found the workaround to that is to just create a pointer to it instead :P

Now I have a question which I can not answer by reading online.

I was under the impression that by including the signature of my functions like such
 
virtual void setEmpId(int id) = 0;


that this means "Ok compiler, I am creating the signature for this function in the parent class but LOOK AT MY Child class's for the actual function to be used."

I thought this was how we used Abstraction with Polymorphic behavior.

I also understand that by appending the = 0 to the end of the function I am making that virtual function abstract.

Now without the abstraction applied, is polymorphism still used?
Topic archived. No new replies allowed.