473,287 Members | 1,659 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,287 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 2250
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.