hi,
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.)
thanks.
hi,
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.)
thanks.
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.
hth
-nv
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.
here's an example:
and you can put a lot of code in the middle if you want..Code:class returnnumber { private: float pi = 3.1428; public: int century = 100; };
www.akilla.tk
You should always make data within the class private, never protected or public.
PHP Code:
class returnnumber
{
private:
float pi = 3.1428;
int x;
public:
int century = 100;
returnnumber() {} // default constructor
returnnumber(z) {setX(z)} // overidden constructor
void setX(int y) {x = 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(10) b; // 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
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