Trouble with constructor in abstract class

I am working with virtual functions and run-time polymorphism. The issue I am having, which its preventing me from doing anything else with this program, is my compiler is throwing an error when I try and make the constructor for SalariedEmployee claiming that there is no base constructor for employee. I created the constructor at the bottom for employee and it still throws an error. I am looking for help with constructor creations for each class. Not sure at this point, if its my mistake or my compiler is not set up properly?

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
  #include <iostream>
#include <string>

using namespace std;

class Date
{
public:
	static const unsigned int monthsPerYear = 12;
	explicit Date(int = 1, int = 1, int = 1900);
	void print() const;
	~Date();
private:
	unsigned int month;
	unsigned int day;
	unsigned int year;
};

Date::Date(int mn, int dy, int yr)
{
	if (mn > 0 && mn <= monthsPerYear)
		month = mn;
	else
		throw invalid_argument("month must be 1-12");

	year = yr;
	day = dy;

	print();
	cout << endl;
}

void Date::print() const
{
	cout << month << '/' << day << '/' << year;
}

Date::~Date()
{
	print();
	cout << endl;
}

class Employee
{
private:
	int ID;		
public:	
	char fullName[80];
	char homeAddress[80];
	char homePhone[80];
	Date birthDate; //composition: member object
	Date hireDate; //compositon: member object
	Employee(int id);
	int GetID();
	virtual double getMonthsPay() = 0;
};

class SalariedEmployee : public Employee
{
public:
	double salary;
	double getMonthsPay();
	SalariedEmployee(int id);
};

class HourlyEmployee : public Employee
{
public:
	double hours;
	double hourlyRate;
	double getMonthsPay();
	HourlyEmployee(int id);
};

class OutsourcedEmployee : public SalariedEmployee
{
public:
	double getMonthsPay();
	OutsourcedEmployee();
};

int main()
{

	system("pause");

}

Employee::Employee(int id)
{
	ID = id;
}

SalariedEmployee::SalariedEmployee(int id)
{//the error is here, wont let me create this constructor
}
Last edited on
What you want is this:
95
96
SalariedEmployee::SalariedEmployee(int id) : Employee(id)
{}

The problem is that you didn't specify the initializer for the base class and the base class has no default constructor.


Last edited on
Always something relatively simple, thank you. Since all the employee class member functions are public I should be able to use those to initialize the SalariedEmployee objects?

If I declare object SalariedEmployee emp1();

how do i go about assigning values to each member function from here?
Shouldn't it be emp1.fullName ="Insert Name"?

Since all the employee class member functions are public I should be able to use those to initialize the SalariedEmployee objects?

Yes, although best practice is not to make the member variables public. By having the member variables public, you open yourself up to allowing them to be changed anywhere in your program rather than controlling access through member functions of your class.

If I declare object SalariedEmployee emp1();
Your declaration of emp1 should not have the ().
Your declaration of emp1 should pass a single argument (ID) since SalariedEmployee has only a single constructor that requires an int.

Shouldn't it be emp1.fullName ="Insert Name"?

That works since your member variables are public.

edit: Corrected comment about emp1 constructor.
Last edited on
When i remove the (); from my SalariedEmployee object is get another warning about how I don't have a constructor for this class. I thought employee(id) was assumed through Salaried Employee since we initialized it to the base class.
No. SalariedEmployee has a constructor that takes an int, and passes it through to the Employee constructor.

If you try and instantiate a SalariedEmployee object without passing an int into the constructor, how could the compiler possibly know what value to pass to the Employee constructor?

EDIT:

And:

SalariedEmployee emp1();

does not define an object. It actually declares a function called emp1(), that returns a SalariedEmployee. That catches a lot of people out :)
Last edited on
Topic archived. No new replies allowed.