You are on page 1of 15

STATIC MEMBERS

Static Data Members Static Member Function

#include<iostream.h> #include<conio.h> class example { int a; int b; public: void getdata(int x,int y) { a=x; b=y; }

void print () { cout<<A<<a; cout<<B"<<b; } }; void main() { example o1,o2; o1.getdata(5,5); o1.print(); o2.getdata(10,10); o2.print(); o1.print(); getch(); }

A:5 B:5 A:10 B:10 A:5 B:5

Static Data Members They are used to maintain values common to the entire class. Characteristics:
It is initialized to zero when the first object of the

class is created .No other initialization is done. Only one copy of that member is created for that entire class and is shared by object of that class,no matter how many objects are created. It is visible only within the program but its life time is entire program.

#include<iostream.h> #include<conio.h> class example { static int a; int b; public: void getdata(int x,int y) { a=x; b=y; }

void print () { cout<<STATIC:"<<a; cout<<"NONSTATIC:"<<b; } }; int example :: a; void main() { example o1,o2; o1.getdata(5,5); o1.print(); o2.getdata(10,10); o2.print(); o1.print(); getch(); }

STATIC:5 NON STATIC:5 STATIC:10 NON STATTIC:10 STATIC:10 NON STATIC:5

Static Member Function


A static function can have access to only other static members declared in the same class. A static member function can be called using the class name.
Syntax:
class name :: function-name;

#include<iostream.h> #include<conio.h> class example { static int a; int b; public: void getdata(int x,int y) { a=x; b=y; }

static void print () { cout<<STATIC:"<<a; cout<<"NONSTATIC:"<<b; } }; int example :: a; void main() { example o1,o2; o1.getdata(5,5); o1.print(); o2.getdata(10,10); o2.print(); o1.print(); getch(); }

#include<iostream.h> #include<conio.h> class example { static int a; static int b; public: void getdata(int x,int y) { a=x; b=y; }

static void print () { cout<<STATIC:"<<a; cout<<"STATIC:"<<b; } }; int example :: a; int example::b; void main() { example o1,o2; o1.getdata(5,5); o1.print(); o2.getdata(10,10); o2.print(); o1.print(); getch(); }

STATIC:5 NON STATIC:5 STATIC:10 NON STATTIC:10 STATIC:10 NON STATIC:10

NESTED CLASSES

#include<iostream.h> #include<conio.h> class outside { class inside { public: void display() { cout<<"welcome"; } };

public: void show() { inside in; in.display(); } }; void main() { clrscr(); outside out; out.show(); getch(); }

LOCAL CLASSES

#include<iostream.h> #include<conio.h> void test() { class local { public: void disp(){ cout<<"Hai";} }; local l; l.disp(); }

void main() { clrscr(); test(); getch(); }

You might also like