PDA

View Full Version : Initialize private members


Jim Roberts
Oct11-04, 03:30 PM
I have a class and i'd like to initialize
or set the values of its private members
in the client code, how can I do that ?
thanku,

jim

Hurkyl
Oct11-04, 03:43 PM
Through setter methods.

For example, in Java style:


public class Example {
private int var;

public int getVar() { return var; }
public void setVar(int i) { var = i; }
}

Jim Roberts
Oct11-04, 04:01 PM
:wink:, but can you tell me how to do that in c++?
and initialize/change the values of class's private
member variables in main function ?
thanks hurkyl

Guybrush Threepwood
Oct12-04, 01:40 AM
int main(int argc, char *argv[]) {
Example exmpl;

exmpl.setVar(2004);
printf("private var = %d\n", exmpl.getVar());

return 0;
}