public and private

This is a discussion on public and private within the C++ Programming forums, part of the General Programming Boards category; hi, i'm a newbie in C++. after going through a reference, i still can't understand how to get and set ...

  1. #1

    public and private


    i'm a newbie in C++. after going through a reference, i still can't understand how to get and set the variables on private. can someone please explain to me.(with an example will be good.)

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    To have a private variable in a class, declare it after the "private:" part. To access it inside your class, just handle it as a normal variable. You cannot access it from outside your class, that's the whole purpose behind the private keyword.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    here's an example:

    class returnnumber
          float pi = 3.1428;
          int century = 100;
    and you can put a lot of code in the middle if you want..

  4. #4
    You should always make data within the class private, never protected or public.

  5. #5
    Registered User
    Join Date
    Jan 2002
    PHP Code:
    class returnnumber
    float pi 3.1428;
    int x;
    int century 100;
    returnnumber() {}   // default constructor
    returnnumber(z) {setX(z)}   // overidden constructor
    void setX(int y) {y;} 
    int getX() {return x;};

    int main()
    returnnumber a;   // declares object named a of class
                            // returnnumber, but value of x in a is undefined
    returnnumber(10b;   // declares object named b of class 
                          // returnnumber with value of 10 in x
    a.setX(5);   // x now = 5 
    Last edited by salvelinus; 07-10-2002 at 06:44 AM.
    Truth is a malleable commodity - Dick Cheney

  6. #6
    Registered User
    Join Date
    Jun 2002
    class myclass{
    int i,j; /*these are the private variables or functions */
    public: //after here goes the public variables

    int age(); //these are public variables or functions
    int speed();
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. "public" public objects
    By VOX in forum C# Programming
    Replies: 6
    Last Post: 06-22-2005, 11:32 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 02:29 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21