2 contructors. Help

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:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class StaffMember
{
// data declaration section
protected:
string name;
string phone;
double pay;
// methods declaration section
public:
StaffMember(string n = " ", string ph = " ");
StaffMember(string n = " ", string ph = " ", double=0.0); // constructor
string getName();
string getPhone();
double getPay();
void display();
};

// methods implementation section for Circle

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();
};

// methods implementation section for Employee

double Employee::getPay()
{
return payRate * hoursWorked;
}

void Employee::display()
{
cout << "Name is " << name << endl;
cout << "Phone number is " << phone << endl;
cout << "Pay is $" << pay << endl;
}


int main()
{
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

staffmember.display();
volunteer.display();
employee.display();

return 0;
}
look at you constructors for the base class:
1
2
StaffMember(string n = " ", string ph = " ");
StaffMember(string n = " ", string ph = " ", double=0.0);

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

staffmember.display();
volunteer.display();
employee.display();
You forgot to initialize pay in one of the constructors.
Hi Peter, thks for the prompt reply. After getting rid of the default arg as reccommended, the pay generated rubbish. The revised code as follows:

class StaffMember
{
// data declaration section
protected:
string name;
string phone;
double pay;
// methods declaration section
public:
StaffMember(string n, string ph);
StaffMember(string n, string ph, double Pay); // constructor
string getName();
string getPhone();
double getPay();
void display();
};
Topic archived. No new replies allowed.