Newbie needing assistance on object construction, array of an object as function argument...

Discussion in 'C++' started by cantide5ga, Oct 23, 2007.

  1. cantide5ga

    cantide5ga Guest

    I am a student beginning C++ (also new to Newsgroups as well) and am
    having a hell of a time understanding everything. My experience in OO
    and UML from a prior class provide an understanding of what C++ COULD
    do, but I can't seem to get past the coding aspect.

    The program below is one of my assignments that is the beginning of an
    Employee DB. It does what is asked, but not how it was asked:

    //
    **********************************************************************************************************************************
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    #include <string>
    using std::string;
    using std::getline;

    class EmployeeData
    {
    public:
    const static int number = 100;

    void getEmployee()
    {
    getStaffNumber();

    for (int n = 1; n <= staffNumber; n++)
    {
    cout << "Input data for EMPLOYEE #" << n << endl;

    arrayData = n;

    getStaffName();
    getStaffTitle();
    getStaffWage();
    getStaffHours();

    cin.ignore( 1, '\n' );
    }
    }

    void putEmployee()
    {
    for (int n = 1; n <= staffNumber; n++)
    {
    cout << "Data for EMPLOYEE #" << n << endl;

    if (staffWage[n] > 20)
    staffName[n] = staffName[n] + "*";

    if (staffWage[n] * staffHours[n] > 800)
    staffName[n] = staffName[n] + "*";

    cout << "\nNAME\t: " << staffName[n];
    cout << "\nTITLE\t: " << staffTitle[n];
    cout << "\nPAY\t: $" << staffWage[n] * staffHours[n];
    }
    }

    private:
    string staffName[number];
    string staffTitle[number];
    double staffWage[number];
    double staffHours[number];
    int arrayData;
    int staffNumber;

    int getStaffNumber()
    {
    int number;

    cout << "Please enter number of employees (max. 100): ";
    cin >> number;

    staffNumber = number;

    cin.ignore( 1, '\n' );

    return staffNumber;
    }

    string getStaffName()
    {
    string name;

    cout << "Employee NAME\t: ";
    getline(cin, name);

    staffName[arrayData] = name;

    return staffName[arrayData];
    }

    string getStaffTitle()
    {
    string title;

    cout << "Employee TITLE\t: ";
    getline(cin, title);

    staffTitle[arrayData] = title;

    return staffTitle[arrayData];
    }

    double getStaffWage()
    {
    double wage;

    cout << "Employee WAGE\t: ";
    cin >> wage;

    staffWage[arrayData] = wage;

    return staffWage[arrayData];
    }

    double getStaffHours()
    {
    double hours;

    cout << "Employee HOURS\t: ";
    cin >> hours;

    staffHours[arrayData] = hours;

    return staffHours[arrayData];
    }
    };

    int main()
    {
    EmployeeData id0;

    id0.getEmployee();
    id0.putEmployee();

    return 0;
    }
    //
    **********************************************************************************************************************************

    I *think* I should be able to somehow invoke a constructor to create
    an object for each employee (with relative name, title, wage, and
    hours data) but I can't wrap my head around the concept. The main
    obstacle here is having a unique object name created for each
    employee. After melting my brain for hours, I decided to just go with
    my default idea and have one object containing a user defined amount
    of arrays.

    According to the assignment:
    -Create and use a GetEmployee function that returns an employee
    object. This function will prompt the user to enter data, store that
    data in an object, and return the object to the calling function.
    -Create and use a function that displays a list of employees and their
    pay. This function will take an array of employee object as an
    argument.

    It sounds to me like I do indeed need to have an object for each
    employee, because the display function will take it as an argument!
    Oh man... any advice would be appreciated.
     
    cantide5ga, Oct 23, 2007
    #1
    1. Advertising

  2. cantide5ga

    osmium Guest

    "cantide5ga" wrote:

    >I am a student beginning C++ (also new to Newsgroups as well) and am
    > having a hell of a time understanding everything. My experience in OO
    > and UML from a prior class provide an understanding of what C++ COULD
    > do, but I can't seem to get past the coding aspect.
    >
    > The program below is one of my assignments that is the beginning of an
    > Employee DB. It does what is asked, but not how it was asked:

    <snippage>

    >
    > //
    > > I *think* I should be able to somehow invoke a constructor to create

    > an object for each employee (with relative name, title, wage, and
    > hours data) but I can't wrap my head around the concept. The main
    > obstacle here is having a unique object name created for each
    > employee. After melting my brain for hours, I decided to just go with
    > my default idea and have one object containing a user defined amount
    > of arrays.
    >
    > According to the assignment:
    > -Create and use a GetEmployee function that returns an employee
    > object. This function will prompt the user to enter data, store that
    > data in an object, and return the object to the calling function.
    > -Create and use a function that displays a list of employees and their
    > pay. This function will take an array of employee object as an
    > argument.
    >
    > It sounds to me like I do indeed need to have an object for each
    > employee, because the display function will take it as an argument!
    > Oh man... any advice would be appreciated.


    You misunderstand the assignment. GetEmployee is a "free standing" (for
    lack of a better word) function. It's purpose is to interact with the user
    and create an Emloyee object, it is not a part of the employee class as you
    have it.
     
    osmium, Oct 23, 2007
    #2
    1. Advertising

  3. cantide5ga

    Jim Langston Guest

    "cantide5ga" <> wrote in message
    news:...
    >I am a student beginning C++ (also new to Newsgroups as well) and am
    > having a hell of a time understanding everything. My experience in OO
    > and UML from a prior class provide an understanding of what C++ COULD
    > do, but I can't seem to get past the coding aspect.
    >
    > The program below is one of my assignments that is the beginning of an
    > Employee DB. It does what is asked, but not how it was asked:
    >
    > //
    > **********************************************************************************************************************************
    > #include <iostream>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > #include <string>
    > using std::string;
    > using std::getline;
    >
    > class EmployeeData
    > {
    > public:
    > const static int number = 100;
    >
    > void getEmployee()
    > {
    > getStaffNumber();
    >
    > for (int n = 1; n <= staffNumber; n++)
    > {
    > cout << "Input data for EMPLOYEE #" << n << endl;
    >
    > arrayData = n;
    >
    > getStaffName();
    > getStaffTitle();
    > getStaffWage();
    > getStaffHours();
    >
    > cin.ignore( 1, '\n' );
    > }
    > }
    >
    > void putEmployee()
    > {
    > for (int n = 1; n <= staffNumber; n++)
    > {
    > cout << "Data for EMPLOYEE #" << n << endl;
    >
    > if (staffWage[n] > 20)
    > staffName[n] = staffName[n] + "*";
    >
    > if (staffWage[n] * staffHours[n] > 800)
    > staffName[n] = staffName[n] + "*";
    >
    > cout << "\nNAME\t: " << staffName[n];
    > cout << "\nTITLE\t: " << staffTitle[n];
    > cout << "\nPAY\t: $" << staffWage[n] * staffHours[n];
    > }
    > }
    >
    > private:
    > string staffName[number];
    > string staffTitle[number];
    > double staffWage[number];
    > double staffHours[number];
    > int arrayData;
    > int staffNumber;
    >
    > int getStaffNumber()
    > {
    > int number;
    >
    > cout << "Please enter number of employees (max. 100): ";
    > cin >> number;
    >
    > staffNumber = number;
    >
    > cin.ignore( 1, '\n' );
    >
    > return staffNumber;
    > }
    >
    > string getStaffName()
    > {
    > string name;
    >
    > cout << "Employee NAME\t: ";
    > getline(cin, name);
    >
    > staffName[arrayData] = name;
    >
    > return staffName[arrayData];
    > }
    >
    > string getStaffTitle()
    > {
    > string title;
    >
    > cout << "Employee TITLE\t: ";
    > getline(cin, title);
    >
    > staffTitle[arrayData] = title;
    >
    > return staffTitle[arrayData];
    > }
    >
    > double getStaffWage()
    > {
    > double wage;
    >
    > cout << "Employee WAGE\t: ";
    > cin >> wage;
    >
    > staffWage[arrayData] = wage;
    >
    > return staffWage[arrayData];
    > }
    >
    > double getStaffHours()
    > {
    > double hours;
    >
    > cout << "Employee HOURS\t: ";
    > cin >> hours;
    >
    > staffHours[arrayData] = hours;
    >
    > return staffHours[arrayData];
    > }
    > };
    >
    > int main()
    > {
    > EmployeeData id0;
    >
    > id0.getEmployee();
    > id0.putEmployee();
    >
    > return 0;
    > }
    > //
    > **********************************************************************************************************************************
    >
    > I *think* I should be able to somehow invoke a constructor to create
    > an object for each employee (with relative name, title, wage, and
    > hours data) but I can't wrap my head around the concept. The main
    > obstacle here is having a unique object name created for each
    > employee. After melting my brain for hours, I decided to just go with
    > my default idea and have one object containing a user defined amount
    > of arrays.
    >
    > According to the assignment:
    > -Create and use a GetEmployee function that returns an employee
    > object. This function will prompt the user to enter data, store that
    > data in an object, and return the object to the calling function.
    > -Create and use a function that displays a list of employees and their
    > pay. This function will take an array of employee object as an
    > argument.
    >
    > It sounds to me like I do indeed need to have an object for each
    > employee, because the display function will take it as an argument!
    > Oh man... any advice would be appreciated.


    Since this is homework, I'll not give you complete code, but you have shown
    a tremendous amount of effort, so I'll pretty much show you how to do it.
    Untested code: Note that mainline is just showing a rudimentary way of
    input, etc... you should use proper error checking, etc...

    class EmployeeData
    {
    public:
    EmployeeData( const std::string& Name, const std::string& Title ):
    Name_( Name ), Title_( Title ) {}
    std::string Name() { return Name_; }
    std::string Title() { return Title_; }
    private:
    std::string Name_;
    std::string Title_;
    };

    int main()
    {
    std::vector<EmplyeeData> Data;

    std::string Name;
    std::string Title;
    std::cout >> "Enter Employee Name: ";
    while ( std::getline( std::cin, Name ) )
    {
    std::cout >> "Enter Emplee Title: ";
    if ( std::getline( std::cin, Title ) )
    {
    Data.push_back( EmployeeData( Name, Title ) );
    }
    }

    // At this point Data contains Employee's if they were input.
    for ( int i = 0; i < Data.size(); ++i )
    {
    std::cout << "Name: " << Data.Name() << "\n";
    std::cout << "Title: " << Data.Title() << "\n\n";
    }

    }

    I pushed instances of EmploeeData onto a vector. At this point three is one
    EmployeeData instance for each employee that was entered. There are a few
    ways to go through the employees, I'm showing one way, you can also use
    iterators. You may also perfer to use a std::map keyed by EmploeeName or
    such.
     
    Jim Langston, Oct 23, 2007
    #3
  4. cantide5ga

    cantide5ga Guest

    On Oct 23, 3:25 pm, "osmium" <> wrote:
    > "cantide5ga" wrote:
    > >I am a student beginning C++ (also new to Newsgroups as well) and am
    > > having a hell of a time understanding everything. My experience in OO
    > > and UML from a prior class provide an understanding of what C++ COULD
    > > do, but I can't seem to get past the coding aspect.

    >
    > > The program below is one of my assignments that is the beginning of an
    > > Employee DB. It does what is asked, but not how it was asked:

    >
    > <snippage>
    >
    >
    >
    >
    >
    > > //
    > > > I *think* I should be able to somehow invoke a constructor to create

    > > an object for each employee (with relative name, title, wage, and
    > > hours data) but I can't wrap my head around the concept. The main
    > > obstacle here is having a unique object name created for each
    > > employee. After melting my brain for hours, I decided to just go with
    > > my default idea and have one object containing a user defined amount
    > > of arrays.

    >
    > > According to the assignment:
    > > -Create and use a GetEmployee function that returns an employee
    > > object. This function will prompt the user to enter data, store that
    > > data in an object, and return the object to the calling function.
    > > -Create and use a function that displays a list of employees and their
    > > pay. This function will take an array of employee object as an
    > > argument.

    >
    > > It sounds to me like I do indeed need to have an object for each
    > > employee, because the display function will take it as an argument!
    > > Oh man... any advice would be appreciated.

    >
    > You misunderstand the assignment. GetEmployee is a "free standing" (for
    > lack of a better word) function. It's purpose is to interact with the user
    > and create an Emloyee object, it is not a part of the employee class as you
    > have it.


    So a separate class with its own functions?
     
    cantide5ga, Oct 23, 2007
    #4
  5. cantide5ga wrote:
    >> [..]
    >> You misunderstand the assignment. GetEmployee is a "free standing"
    >> (for lack of a better word) function. It's purpose is to interact
    >> with the user and create an Emloyee object, it is not a part of the
    >> employee class as you have it.

    >
    > So a separate class with its own functions?


    A "free standing" function is not a member of any class. Like 'main'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 23, 2007
    #5
  6. cantide5ga

    osmium Guest

    "cantide5ga" wrote:

    >> You misunderstand the assignment. GetEmployee is a "free standing" (for
    >> lack of a better word) function. It's purpose is to interact with the
    >> user
    >> and create an Emloyee object, it is not a part of the employee class as
    >> you
    >> have it.

    >
    > So a separate class with its own functions?


    No. just a function. perhaps like this

    Emp& get_emp()
    // interact with user. create an employee and return it.
     
    osmium, Oct 23, 2007
    #6
  7. osmium wrote:
    > "cantide5ga" wrote:
    >
    >>> You misunderstand the assignment. GetEmployee is a "free standing"
    >>> (for lack of a better word) function. It's purpose is to interact
    >>> with the user
    >>> and create an Emloyee object, it is not a part of the employee
    >>> class as you
    >>> have it.

    >>
    >> So a separate class with its own functions?

    >
    > No. just a function. perhaps like this
    >
    > Emp& get_emp()
    > // interact with user. create an employee and return it.


    Just a thought: if a function is to _create_ an object, it should
    most likely either return a pointer to a dynamically created one,
    or return the object by value. Returning a reference to non-const
    object often presumes the object existed before the function was
    called.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 23, 2007
    #7
  8. cantide5ga

    cantide5ga Guest

    On Oct 23, 3:34 pm, "Jim Langston" <> wrote:
    > "cantide5ga" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >I am a student beginning C++ (also new to Newsgroups as well) and am
    > > having a hell of a time understanding everything. My experience in OO
    > > and UML from a prior class provide an understanding of what C++ COULD
    > > do, but I can't seem to get past the coding aspect.

    >
    > > The program below is one of my assignments that is the beginning of an
    > > Employee DB. It does what is asked, but not how it was asked:

    >
    > > //
    > > **********************************************************************************************************************************
    > > #include <iostream>
    > > using std::cout;
    > > using std::cin;
    > > using std::endl;

    >
    > > #include <string>
    > > using std::string;
    > > using std::getline;

    >
    > > class EmployeeData
    > > {
    > > public:
    > > const static int number = 100;

    >
    > > void getEmployee()
    > > {
    > > getStaffNumber();

    >
    > > for (int n = 1; n <= staffNumber; n++)
    > > {
    > > cout << "Input data for EMPLOYEE #" << n << endl;

    >
    > > arrayData = n;

    >
    > > getStaffName();
    > > getStaffTitle();
    > > getStaffWage();
    > > getStaffHours();

    >
    > > cin.ignore( 1, '\n' );
    > > }
    > > }

    >
    > > void putEmployee()
    > > {
    > > for (int n = 1; n <= staffNumber; n++)
    > > {
    > > cout << "Data for EMPLOYEE #" << n << endl;

    >
    > > if (staffWage[n] > 20)
    > > staffName[n] = staffName[n] + "*";

    >
    > > if (staffWage[n] * staffHours[n] > 800)
    > > staffName[n] = staffName[n] + "*";

    >
    > > cout << "\nNAME\t: " << staffName[n];
    > > cout << "\nTITLE\t: " << staffTitle[n];
    > > cout << "\nPAY\t: $" << staffWage[n] * staffHours[n];
    > > }
    > > }

    >
    > > private:
    > > string staffName[number];
    > > string staffTitle[number];
    > > double staffWage[number];
    > > double staffHours[number];
    > > int arrayData;
    > > int staffNumber;

    >
    > > int getStaffNumber()
    > > {
    > > int number;

    >
    > > cout << "Please enter number of employees (max. 100): ";
    > > cin >> number;

    >
    > > staffNumber = number;

    >
    > > cin.ignore( 1, '\n' );

    >
    > > return staffNumber;
    > > }

    >
    > > string getStaffName()
    > > {
    > > string name;

    >
    > > cout << "Employee NAME\t: ";
    > > getline(cin, name);

    >
    > > staffName[arrayData] = name;

    >
    > > return staffName[arrayData];
    > > }

    >
    > > string getStaffTitle()
    > > {
    > > string title;

    >
    > > cout << "Employee TITLE\t: ";
    > > getline(cin, title);

    >
    > > staffTitle[arrayData] = title;

    >
    > > return staffTitle[arrayData];
    > > }

    >
    > > double getStaffWage()
    > > {
    > > double wage;

    >
    > > cout << "Employee WAGE\t: ";
    > > cin >> wage;

    >
    > > staffWage[arrayData] = wage;

    >
    > > return staffWage[arrayData];
    > > }

    >
    > > double getStaffHours()
    > > {
    > > double hours;

    >
    > > cout << "Employee HOURS\t: ";
    > > cin >> hours;

    >
    > > staffHours[arrayData] = hours;

    >
    > > return staffHours[arrayData];
    > > }
    > > };

    >
    > > int main()
    > > {
    > > EmployeeData id0;

    >
    > > id0.getEmployee();
    > > id0.putEmployee();

    >
    > > return 0;
    > > }
    > > //
    > > **********************************************************************************************************************************

    >
    > > I *think* I should be able to somehow invoke a constructor to create
    > > an object for each employee (with relative name, title, wage, and
    > > hours data) but I can't wrap my head around the concept. The main
    > > obstacle here is having a unique object name created for each
    > > employee. After melting my brain for hours, I decided to just go with
    > > my default idea and have one object containing a user defined amount
    > > of arrays.

    >
    > > According to the assignment:
    > > -Create and use a GetEmployee function that returns an employee
    > > object. This function will prompt the user to enter data, store that
    > > data in an object, and return the object to the calling function.
    > > -Create and use a function that displays a list of employees and their
    > > pay. This function will take an array of employee object as an
    > > argument.

    >
    > > It sounds to me like I do indeed need to have an object for each
    > > employee, because the display function will take it as an argument!
    > > Oh man... any advice would be appreciated.

    >
    > Since this is homework, I'll not give you complete code, but you have shown
    > a tremendous amount of effort, so I'll pretty much show you how to do it.
    > Untested code: Note that mainline is just showing a rudimentary way of
    > input, etc... you should use proper error checking, etc...
    >
    > class EmployeeData
    > {
    > public:
    > EmployeeData( const std::string& Name, const std::string& Title ):
    > Name_( Name ), Title_( Title ) {}
    > std::string Name() { return Name_; }
    > std::string Title() { return Title_; }
    > private:
    > std::string Name_;
    > std::string Title_;
    >
    > };
    >
    > int main()
    > {
    > std::vector<EmplyeeData> Data;
    >
    > std::string Name;
    > std::string Title;
    > std::cout >> "Enter Employee Name: ";
    > while ( std::getline( std::cin, Name ) )
    > {
    > std::cout >> "Enter Emplee Title: ";
    > if ( std::getline( std::cin, Title ) )
    > {
    > Data.push_back( EmployeeData( Name, Title ) );
    > }
    > }
    >
    > // At this point Data contains Employee's if they were input.
    > for ( int i = 0; i < Data.size(); ++i )
    > {
    > std::cout << "Name: " << Data.Name() << "\n";
    > std::cout << "Title: " << Data.Title() << "\n\n";
    > }
    >
    > }
    >
    > I pushed instances of EmploeeData onto a vector. At this point three is one
    > EmployeeData instance for each employee that was entered. There are a few
    > ways to go through the employees, I'm showing one way, you can also use
    > iterators. You may also perfer to use a std::map keyed by EmploeeName or
    > such.


    Thanks to all of you. Will spend the rest of the night playing with
    the suggestions. Jim, I'll be dissecting your code to be sure I
    understand a lot of it, special thanks to you.

    Only thing I am worried about is the scope here (not OO scope btw).
    I'd rather stay within the realm of our studies 'thus far' to achieve
    what I need to. The extra effort will be made to understand it all.
     
    cantide5ga, Oct 23, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. raecer

    newbie needing help

    raecer, Nov 19, 2004, in forum: C++
    Replies:
    3
    Views:
    327
    Phlip
    Nov 19, 2004
  2. rh0dium
    Replies:
    2
    Views:
    472
    Adriano Ferreira
    May 20, 2005
  3. Ook
    Replies:
    10
    Views:
    561
  4. Reckoner
    Replies:
    11
    Views:
    708
    Steven D'Aprano
    Jan 19, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    565
    Seebs
    Nov 21, 2011
Loading...

Share This Page