Private data members

Z

zfareed

Is it possible to print an array that is declared from a class type
that also contains private data members?
I have declared an array in the main function and then set the values
for the array thru member functions. Now I need to print the array
from main but the error states that those elements are private. Here
is some of my code:

class Employee
{
private:
int age;
int id;
float salary;
public:
void setAge(int x);
void setId(int x);
void setSalary(float x);

};

//the main is in the driver file
int main()
{
Employee arr[2];


arr[0].setAge(30);
arr[0].setId(30042554);
arr[0].setSalary(50000.00);

arr[1].setAge(45);
arr[1].setId(40041002);
arr[1].setSalary(70000.00);

PrintEmployee(arr,2); //-------------------states that
Employee::id is private

system("pause");
return 0;
}
//this is one member function included in the
void Employee::setAge() int x
{
arr.age = x;
return x;
}
 
J

John Harrison

Is it possible to print an array that is declared from a class type
that also contains private data members?

Only with a little bit of work.
I have declared an array in the main function and then set the values
for the array thru member functions. Now I need to print the array
from main but the error states that those elements are private. Here
is some of my code:

class Employee
{
private:
int age;
int id;
float salary;
public:
void setAge(int x);
void setId(int x);
void setSalary(float x);

};

//the main is in the driver file
int main()
{
Employee arr[2];


arr[0].setAge(30);
arr[0].setId(30042554);
arr[0].setSalary(50000.00);

arr[1].setAge(45);
arr[1].setId(40041002);
arr[1].setSalary(70000.00);

PrintEmployee(arr,2); //-------------------states that
Employee::id is private

system("pause");
return 0;
}
//this is one member function included in the
void Employee::setAge() int x
{
arr.age = x;
return x;
}


You need to write some accessor functions.

class Employee
{
private:
int age;
int id;
float salary;
public:
int getAge() const;
int getId() const;
float getSalary() const;
void setAge(int x);
void setId(int x);
void setSalary(float x);

getAge, getId and getSalary are acccesor functions. Because they are
class members they can access the private parts of the class.

Now you use the accessor funcitons in your PrintEmployee function,
somthing like this

cout << "Employee age " << employee.getAge() << '\n';

Something like that, since I can;t see your PrintEmployee function I
can't say exactly what you need, but I'm sure you can figure it out.

john
 
J

Jerry Coffin

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 = 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.
 
Z

zfareed

Thanks for the help, guys. I should check my code properly before
posting it. My member functions were all wrong. I have opted to use
accessor functions but my coding shows that these member functions are
still wrong. Would declaring the print function as a friend solve the
problem? The array which I'm trying to set is undeclared.

// member functions
void Employee:setId(int x)
{
arr.id = x;

}
int Employee::getId()
{
return arr.id;
}
// main function
int main()
{
Employee arr[2];
arr[0].setAge(30);
arr[0].setId(30042554);
....

// portion of loop in print function
for(int i=0;i< length;i++)
{
cout << arr.getId() << " " ;
cout << arr.getAge()<< " " ;
 
J

Jerry Coffin

Thanks for the help, guys. I should check my code properly before
posting it. My member functions were all wrong. I have opted to use
accessor functions but my coding shows that these member functions are
still wrong.

Yes -- but then, the best you can hope for in accessor functions is that
they're a bit less wrong...but they're never really right (IMO, of
course).
Would declaring the print function as a friend solve the
problem?

Without knowing exactly what the problem is, that's impossible to
answer.
The array which I'm trying to set is undeclared.

Trying to use something without declaring it rarely works.
// member functions
void Employee:setId(int x)
{
arr.id = x;

}


I've pointed out already: this member function works with a single
instance of the Employee class -- it should not be doing any array
access or anything like it:

void Employee::setId(int x) { id = x; }

int Employee::getId() { return id; }

That's it. All the array access happens in main. There you have an array
OF Employee objects, and you access items in that array. Each item in
that array is just an Employee object -- that fact that it happens to be
one of N in an array makes no difference whatsoever to the Employee
object itself.
 
P

Piyo

Consider this also:
Note: may have some bugs but hopefully you get the idea :)

class Employee
{
private:
int age;
int id;
float salary;
public:
// whatever else you need
// the new addition
void print( ostream & os ) const
{
// print out the way you want it
os << age << " " << id << " " << salary;
}

// alternatively, you can throw away print and
// do this:
//friend ostream &operator<<( ostream &os, const Employee &emp );
};

ostream &
operator<<( ostream &os, const Employee &emp )
{
emp.print( os );
return os;

// if you opted for a friend, then you can do this
// return (os << age << " " << id << " " << salary);
}

void
PrintEmployee( Employee *emp, unsigned int size )
{
for( unsigned int i=0; i < size; ++i )
{
std::cout << emp << std::endl;
}
}

int
main()
{
Employee emp[2];
// init with stuff
PrintEmpolyee( emp, 2 );
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top