Quote
Create a class person with data members for a person's name, age, and payrate such that the below definition is possible. Note that you may need to define multiple overloaded constructors. Also have a display member function. An unititialized person object should have the string "blank" for their name and 0's for age and payrate.
void main(void)
{
[INDENT]person employees[5] = {"Ann Annson", 20, 20.20
[INDENT]"Bill Billson",
30,
"Carl Carlson"};[/INDENT]
for (int x=0; x < 5; x++)
[INDENT] employees[x].display();[/INDENT][/INDENT]
}
void main(void)
{
[INDENT]person employees[5] = {"Ann Annson", 20, 20.20
[INDENT]"Bill Billson",
30,
"Carl Carlson"};[/INDENT]
for (int x=0; x < 5; x++)
[INDENT] employees[x].display();[/INDENT][/INDENT]
}
This is all well and good, but I can't get the definition to work. Here is my current source code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Person //this class holds and displays a person's name, age, and payrate
{ //note: assignment says call the class "person" but I'm calling it "Person"
//because I like capitalizing that way to denote a class or struct
private:
string name; //name
int age; //age
double payrate; //hourly wage
public:
Person(): name("blank"), age(0), payrate(0)
{ } //default constructor
Person(string na, int ag, double pay):
name(na), age(ag), payrate(pay)
{ } //full 3-argument constructor
Person(string na): name(na), age(0), payrate(0)
{ }//consturctor for name only
Person(int ag): name("blank"), age(ag), payrate(0)
{ }//consturctor for age only
Person(double pay): name("blank"), age(0), payrate(pay)
{ }//constructor for payrate only
void display(); //this function prints out the Person data
};
void Person::display() //this function prints out the Person data
{
cout << name << " is " << age << " years old and is paid " << payrate << " an hour." << endl;
}
int main ()
{
Person employees[5] = {
Person("Ann Annson", 20, 20.20),
"Bill Billson",
30,
"Carl Carlson"
};
for (int x=0; x<5; x++)
employees[x].display();
cout << "Press Enter to exit....";
cin.get(); //wait for enter to be pressed before exiting
return 0;
}
What am I doing wrong?
It returns
In function `int main()': 46: invalid conversion from `const char*' to `int' 46: initializing argument 1 of `Person::Person(int)' 46: invalid conversion from `const char*' to `int' 46: initializing argument 1 of `Person::Person(int)'











