Newbie needing assistance on object construction, array of an object as function argument... - c++
This is a discussion on Newbie needing assistance on object construction, array of an object as function argument... - c++ ; 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, ...
-
Newbie needing assistance on object construction, array of an object as function argument...
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.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
-
Re: Newbie needing assistance on object construction, array of an object as function argument...
cantide5ga wrote:
[original post re-arranged]
> 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:
>
<inserted remarks of OP>
>
> 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.
Actually, you don't need a unique name for each EmployeeData object.
Unless, that is, you consider things like 'employee[42]' to be a name as
well.
> 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.
This requirement means effectively that GetEmployee can not be a regular
member of the EmployeeData class, because it is supposed to create such
objects.
That means you essentially have two, equally good, options: make it a
static member function, or make it a free-standing non-member 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.
The function that displays a list of employees should also not be a
member of EmployeeData. In a good OO design, members of a class should
primarily operate on the current object, not on a bunch of objects of
unspecified size.
You can, however, use a member function to display a single employee,
and call that function in a loop for displaying all of them.
>
> 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!
That is right, you either need an array of EmployeeData objects, or
preferably you would used a std::vector<EmployeeData>.
> Oh man... any advice would be appreciated.
>
>
</insertion>
> //
>
**********************************************************************************************************************************
> #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;
Unlike some other languages, in C++ it is very common to have
functions/constants/etc that are not members of a class.
Lest move that declaration out of the class for now.
>
<snip>
> 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];
What is so different about writing arrays as members and writing
class EmployeeData {
string staffName;
string staffTitle;
double staffWage;
double staffHours;
} employees[number];
> int arrayData;
> int staffNumber;
These two really should not be members of EmployeeData.
<snip>
> };
>
> int main()
> {
> EmployeeData id0;
>
> id0.getEmployee();
> id0.putEmployee();
>
> return 0;
> }
>
> //
>
**********************************************************************************************************************************
As a reference, here is an alternative way to write the program.
#include <iostream>
#include <ostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
class Employee {
public:
Employee(string aName, string aTitle, double aHours, double aWage)
: name(aName), title(aTitle), hours(aHours), wage(aWage) {}
void Print(ostream& os) const
{
string printName = name;
if (wage > 20)
printName += '*';
if ((wage * hours) > 800)
printName += '*';
os << "\nNAME\t: " << printName;
os << "\nTITLE\t: " << title;
os << "\nPAY\t: $" << wage * hours;
}
private:
string name;
string title;
double hours;
double wage;
};
Employee GetEmployee()
{
string name;
string title;
double hours;
double wage;
cout << "Employee NAME\t: ";
getline(cin, name);
cout << "Employee TITLE\t: ";
getline(cin, title);
cout << "Employee WAGE\t: ";
cin >> wage;
cout << "Employee HOURS\t: ";
cin >> hours;
return Employee(name, title, hours, wage);
}
ostream& operator<<(ostream& os, const Employee& emp)
{
emp.Print(os);
return os;
}
void PrintEmployees(const vector<Employee>& employees)
{
copy(employees.begin(),
employees.end(),
ostream_iterator<Employee>(cout, ""));
}
int main()
{
vector<Employee> employees;
int staffSize;
cout << "Please enter number of employees (max. 100):";
cin >> staffSize;
cin.ignore( 1, '\n' );
for (int i=0; i<staffSize; i++)
{
employees.push_back(GetEmployee());
}
PrintEmployees(employees);
}
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Similar Threads
-
By Application Development in forum Javascript
Replies: 2
Last Post: 11-12-2007, 12:34 PM
-
By Application Development in forum c++
Replies: 7
Last Post: 10-23-2007, 05:06 PM
-
By Application Development in forum c++
Replies: 9
Last Post: 10-01-2007, 09:15 AM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 10-03-2006, 09:48 AM
-
By Application Development in forum XML SOAP
Replies: 0
Last Post: 05-18-2005, 05:49 PM