In article <11**********************@t69g2000cwt.googlegroups .com>,
zf*****@umd.umich.edu says...
Is it possible to print an array that is declared from a class type
that also contains private data members?
Yes.
[ ... ]
class Employee
{
private:
int age;
int id;
float salary;
public:
void setAge(int x);
void setId(int x);
void setSalary(float x);
};
First of all, storing an age is almost always a poor idea -- store a
birthdate, and compute the age as needed. The birthday stays constant
whereas the age needs to be updated regularly.
Second, it looks like your member functions aren't really accomplishing
much that just using a struct with public variables wouldn't do better.
I realize people routinely advise against public variables -- and for
some kinds of things, they're entirely correct. Nonetheless, if all the
class really does is store some data, and has no behavior beyond storing
and retreiving data, a struct may be a better way to go.
[ ... ]
PrintEmployee(arr,2); //-------------------states that
Employee::id is private
It's usually easier to comment on a function like PrintEmployee if you
actually show it to us. I'm going to guess that PrintEmployee looks
something like this:
void PrintEmployee(Employee *data, int index) {
std::cout << data[index].id << ": " <<
data[index].salary << ": " <<
data[index].age << "\n";
}
You've made the id, age and salary members of Employee private. That
means they're ONLY directly accesssible to code that's part of the
Employee class and functions/classes that Employee says are its friends
(which is none, right now).
You could change Employee to declare PrintEmployee as a friend, or you
could make PrintEmployee a part of the Employee class.
//this is one member function included in the
void Employee::setAge() int x
{
arr.age[i] = x;
return x;
}
This has enough problems that it shouldn't even compile. A class member
will normally work with one specific object of that class -- outside
code will handle things like selecting one object in an array. You also
have this function declared to return void (i.e. not return anything)
but then you have it attempting to return a value. As it is right now,
the 'int x' part is a syntax error. You probably want it between the
parentheses.
The usual way to support printing objects is via operator<<, which is
typically declared as a friend of the class. As I started out saying,
however, unless your member funtions are going to provide real behavior
beyond just storing data, you'd be better off just creating a struct
with the data public and be done with it.
Other than that, if this is intended for real use, and not just
something like homework, chances are that you shouldn't really be
storing the salary as a float. Calculations involving money are normally
expected to give results that are entirely repeatable and verifiable to
the smallest amount in that money system (e.g. the penny). To fit that
requirement, you normally need to use integers, not floating point
numbers.
--
Later,
Jerry.
The universe is a figment of its own imagination.