How can I initialize private members in a class using client code?

  • Thread starter Thread starter Jim Roberts
  • Start date Start date
  • Tags Tags
    Members
AI Thread Summary
To initialize or set the values of private members in a class from client code, setter methods are commonly used. In Java, this is demonstrated with a class that includes private variables and public getter and setter methods. For C++, the same concept applies. A class can be defined with private member variables, and public methods can be created to modify and access these variables. In the provided C++ example, an instance of the class is created in the main function, and the setter method is used to assign a value to the private member, which can then be retrieved using the getter method. This approach maintains encapsulation while allowing controlled access to private data.
Jim Roberts
Messages
3
Reaction score
0
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
 
Computer science news on Phys.org
Through setter methods.

For example, in Java style:

Code:
public class Example {
  private int var;

  public int getVar() { return var; }
  public void setVar(int i) { var = i; }
}
 
  • Like
Likes WWGD
: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
 
Code:
int main(int argc, char *argv[]) {
Example exmpl;

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

return 0;
}
 
I have been idly browsing what Apple have to offer with their new iPhone17. There is mention of 'Vapour cooling' to deal with the heat generated. Would that be the same sort of idea that was used in 'Heat Pipes' where water evaporated at the processor end and liquid water was returned from the cool end and back along a wick. At the extreme high power end, Vapour Phase Cooling has been used in multi-kW RF transmitters where (pure) water was pumped to the Anode / or alternative Collector and...
Back
Top