Jump to content


Help with Class Constructor Overloading


1 reply to this topic

#1 crazedfred

    New Member

  • Members
  • Pip
  • 2 posts

Posted 21 February 2007 - 10:02 PM

I am working on an assignment involving class constructors and am a tad confused. Here is the assignment:

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]
}


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)' 


#2 crazedfred

    New Member

  • Members
  • Pip
  • 2 posts

Posted 21 February 2007 - 10:11 PM

Okay, wow. All I had to do was change
      Person(string na): name(na), age(0), payrate(0)
      { }//consturctor for name only
to
      Person(char *na): name(na), age(0), payrate(0)
      { }//consturctor for name only
and it works.

Here I was thinking it was some huge syntax problem.
I feel real smart now.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users