473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Private data members

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[i] = x;
return x;
}

Feb 18 '07 #1
6 2253
zf*****@umd.umich.edu wrote:
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[i] = 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[i].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
Feb 18 '07 #2
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.
Feb 18 '07 #3
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[i].id = x;

}
int Employee::getId()
{
return arr[i].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[i].getId() << " " ;
cout << arr[i].getAge()<< " " ;

Feb 18 '07 #4
In article <11**********************@v45g2000cwv.googlegroups .com>,
zf*****@umd.umich.edu says...
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[i].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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 18 '07 #5
Thanks. I got it to work. Just removed the array instance from the
functions.

Feb 18 '07 #6

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[i] << std::endl;
}
}

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

zf*****@umd.umich.edu wrote:
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[i] = x;
return x;
}
Feb 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
5
by: pmatos | last post by:
Hi all, I have a style question. I've been for long programming in Lisp-like languages and C when I need a low-level language. Now, I'm programming for professional reasons in C++ (which I had...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" <<...
12
by: tobias.sturn | last post by:
Hi! My prof told me always to make my members private or protected cause its standard to write setter and getter methodes.. Is that in your opinion correct? Cause I dont see any adventages to...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
4
by: ChrisW | last post by:
Hi everyone, Great Forum. I would like to create a database hosted on the net for my sporting club so that members can record their match scores and be able to review this data from their own...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.