Class structure (Newbie)

Discussion in 'C++' started by sam, Jan 22, 2007.

  1. sam

    sam Guest

    Hi,
    See this example of class:-
    class a{
    private:
    int o,t;
    public:
    a( ) { };
    a(int s,int g)
    {o=s;
    t=g;
    }
    };
    My question is see o=s; and t=g;
    What we are doing this we are inserting the public value to private
    value or converting public to private.I new to c++ programming but I
    want to understand the logic behind this or the technique behind
    this(Simply I curious about how this work? )
    Thanks for advance.
     
    sam, Jan 22, 2007
    #1
    1. Advertising

  2. sam wrote:
    > Hi,
    > See this example of class:-
    > class a{
    > private:
    > int o,t;
    > public:
    > a( ) { };
    > a(int s,int g)
    > {o=s;
    > t=g;
    > }
    > };
    > My question is see o=s; and t=g;
    > What we are doing this we are inserting the public value to private
    > value or converting public to private.I new to c++ programming but I
    > want to understand the logic behind this or the technique behind
    > this(Simply I curious about how this work? )
    > Thanks for advance.
    >


    'public' and 'private' refer to class members. s and g are not class
    members, they are input parameters from the world outside the class.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Jan 22, 2007
    #2
    1. Advertising

  3. sam

    Noah Roberts Guest

    sam wrote:
    > Hi,
    > See this example of class:-
    > class a{
    > private:
    > int o,t;
    > public:
    > a( ) { };
    > a(int s,int g)
    > {o=s;
    > t=g;
    > }
    > };
    > My question is see o=s; and t=g;
    > What we are doing this we are inserting the public value to private
    > value or converting public to private.I new to c++ programming but I
    > want to understand the logic behind this or the technique behind
    > this(Simply I curious about how this work? )


    The compiler simply refuses to compile anything that accesses those
    variables directly.
     
    Noah Roberts, Jan 22, 2007
    #3
  4. sam

    amit Guest

    sam wrote:
    > Hi,
    > See this example of class:-
    > class a{
    > private:
    > int o,t;
    > public:
    > a( ) { };
    > a(int s,int g)
    > {o=s;
    > t=g;
    > }
    > };
    > My question is see o=s; and t=g;
    > What we are doing this we are inserting the public value to private
    > value or converting public to private.I new to c++ programming but I
    > want to understand the logic behind this or the technique behind
    > this(Simply I curious about how this work? )
    > Thanks for advance.


    You are not 'inserting' but copying value of s and g into o and t
    respectively. o and t will continue to remain private.
     
    amit, Jan 22, 2007
    #4
  5. sam

    Jim Langston Guest

    "sam" <> wrote in message
    news:...
    > Hi,
    > See this example of class:-
    > class a{
    > private:
    > int o,t;
    > public:
    > a( ) { };
    > a(int s,int g)
    > {o=s;
    > t=g;
    > }
    > };
    > My question is see o=s; and t=g;
    > What we are doing this we are inserting the public value to private
    > value or converting public to private.I new to c++ programming but I
    > want to understand the logic behind this or the technique behind
    > this(Simply I curious about how this work? )
    > Thanks for advance.


    a(int s, intg) is a constructor for your class a. It takes two interger
    paramters passed by value, then assigns these values to the class's private
    variables o and t. The class itself can use it's own private variables in
    it's own methods (including constructor).

    Incidently, the same thing can be achieved by:

    a( const int s, const int g): o(s), t(g) {};

    which basically does the same thing except it initializes o and t to the
    values instead of assigns them.
     
    Jim Langston, Jan 23, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,850
    Thomas Weidenfeller
    Oct 12, 2005
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    778
    Martin Ambuhl
    Oct 26, 2004
  3. Leo Nunez
    Replies:
    3
    Views:
    1,253
    Neil Kurzman
    Feb 9, 2005
  4. Replies:
    2
    Views:
    625
  5. Replies:
    9
    Views:
    25,421
    Lal Bahadur Singh
    Nov 11, 2011
Loading...

Share This Page