Sunday 3 February 2013

Accessibility in c++


//check the accessability of static and non static variables/functions
#include<iostream.h>
#include<conio.h>

class test
{
private:
static int x;       //static variable
public:
static void show()  //static function
{
cout<<"value of x   "<<x;
}
};

int test::x=10;     //way of accessing the static variable outside the class
void main()
{
test::show();       //way of accessing the static function outside the class
getch();            //here we access the function with out creating any object
}                   //this proved that static variable or static function is
                    //independent of object creation
/*if we use non static variable and then use static function then compiler will
give error b/c for variable object must be created*/

0 comments:

Post a Comment