I had written the program for inheritance. However the programme cannot be run and reflected more than one instance of constructors from the derived class. Pls help to see what's wrong:
StaffMember::StaffMember(string n, string ph) // constructor
{
name = n;
phone = ph;
}
StaffMember::StaffMember(string n, string ph, double Pay) // constructor
{
name = n;
phone = ph;
pay = Pay;
}
string StaffMember::getName()
{
return name;
}
string StaffMember::getPhone()
{
return phone;
}
double StaffMember::getPay()
{
return pay;
}
void StaffMember::display()
{
cout << "Name is " << name << endl;
cout << "Phone number is " << phone << endl;
cout << "Pay is $" << pay << endl;
}
class Volunteer : public StaffMember // Volunteer is derived from StaffMember
{
public: // two additional function members
Volunteer(string n = " ", string ph = " ") : StaffMember(n, ph) {}
void display();
};
void Volunteer::display()
{
cout << "Name is " << name << endl;
cout << "Phone number is " << phone << endl;
}
// the Employee class is derived from StaffMember
class Employee : public StaffMember
{
//data declaration section
protected:
double payRate; // add two additional data member
int hoursWorked;
// methods declaration section
public: // two additional member methods
Employee(string n = " ", string ph = " ", double PR = 0.0, int HW= 0) : StaffMember(n, ph), payRate(PR), hoursWorked (HW) {}
double getPay();
void display();
};
You put all the values in default for both of them. So, calling an empty constructor(StaffMember()) is ambigous, becouse both constructor can accept zero inputs! delete the default values, and then it will work.
If you pass two or less arguments to the StaffMember constructor it doesn't know which constructor to pick because there are two matching constructors.
You can solve this in at least two ways.
1. Remove the StaffMember constructor with 2 arguments.
2. Remove the default arguments from the StaffMember that takes 3 arguments.
Thanks viliml & Peter. Managed to deconflict and complie the program successfully. However, the pay gives rubbish values when displayed. Needd help again. Thanks.
{
StaffMember staffmember ("Ali", "1234567", 1200); // create one Staffmember object
Volunteer volunteer("Sister", "456779"); // create one Volunteer object
Employee employee("Brother", "123456", 12.5, 5); // create one Employee object