inheritance question

so I have just started learning about inheritance and wanted to know if I can create a factory that checks 3 classes that all extend 1 class and returns one of them into an array

I am still trying to fix my code so excuse some errors but I have 1 class called employees and 3 classes that extend employee called faculty, classifiedstaff, and studentemployee. I am trying to create an employee factory that that takes these 3 classes and returns pointer to the select class depending on the text in the file
here is my code
employee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Employee
{ 
private:
	string _employee_name;
	int _employee_Id;
	bool _is_working;
public:
	Employee::Employee()
	{
		_employee_name = "";
		_employee_Id = 0;
		_is_working =false;
		
	}

	Employee :: Employee(string name,int id,bool is_notworking)
	{
		_employee_name = name;
		_employee_Id = id;
		_is_working = is_notworking;
	}

student employee
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
class StudentEmployee :public Employee
{
private:
	int _hours_worked;
	bool _is_work_study;
	double _payrate;
public:
StudentEmployee()
		: Employee("",0,false)
	{

		_hours_worked =0;
		_is_work_study = false;
		_payrate = 0;
	}

StudentEmployee( string name , int id, bool is_working,bool is_work_study,
					 int hours_worked, double pay_rate)
					: Employee(name,id,is_working)
	{

		_hours_worked =hours_worked;
		_is_work_study = is_work_study;
		_payrate = pay_rate;
	}


faculty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class faculty : public Employee
{
private:
	double _annual_salary;
	string _department;
	int _weeks_per_year;
public:
	 faculty(string name, int Id, bool is_working, double salary ,
		    int weeks_per_year, string department)
			: Employee(name,Id,is_working)
	{
		
		_annual_salary = salary;
		_department = department;
		_weeks_per_year = weeks_per_year;
	}


classified staff
1
2
3
4
5
6
7
8
9
10
11
12
13
class ClassifiedStaff : public Employee
{
private:
	double _weekly_salary;
	string _division;
public:
	ClassifiedStaff(string name, int id, bool isworking, double salary, string division)
					: Employee(name,id,isworking)
		
	{
		_weekly_salary = salary;
		_division = division;
	}

employee factory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Employee* fromString(string text)
{
	string employee[6];
		int number_items;
		StringSplitter files;
		string* pointer = &employee[0];
		Employee * someemployee = nullptr;
		
	pointer =	files.split(text,",",number_items);

	if(number_items == 5)
	{
	   return  staffFromString(text);// creates a pointer to a new         //                                          classifiedstaff and returns it
	}else if(is_number(employee[0]) == true)
	{
		return studentFromString(text);//create pointer to                                //                                               studentemployee
	}else
	{
		return facultyFromString(text);//creates pointer to faculty
	}
}


now my question is can a pointer employee point to all these functions, is there any way I can create a function like this
closed account (o3hC5Di1)
now my question is can a pointer employee point to all these functions, is there any way I can create a function like this


It seems your question was clipped, I'm not sure exactly what you mean.

If you are wondering about sharing functionality between base and derived classes perhaps you will find this helpful: http://www.cplusplus.com/doc/tutorial/polymorphism/

Please do get back to us with a specific question if you require any further help.

All the best,
NwN
Topic archived. No new replies allowed.