1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Initialize private members

  1. Oct 11, 2004 #1
    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
     
  2. jcsd
  3. Oct 11, 2004 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Through setter methods.

    For example, in Java style:

    Code (Text):

    public class Example {
      private int var;

      public int getVar() { return var; }
      public void setVar(int i) { var = i; }
    }
     
     
  4. Oct 11, 2004 #3
    :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
     
  5. Oct 12, 2004 #4
    Code (Text):

    int main(int argc, char *argv[]) {
    Example exmpl;

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

    return 0;
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?