Professional Documents
Culture Documents
#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(); }
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(); }
#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(); }
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(); }