Example:3
Give output of the following program
#include <iostream> #include<conio.h> using namespace std; class sample { private: static int a; public: void disp(); }; //int sample::a; //1 int sample::a=100; //2 void sample::disp() { for(int i=0;i<=10;i++) a+=i; //a=a+i; cout<<"a= "<<a<<endl; } int main() { /* clrscr(); */ sample e; for(int i=0;i<5;i++) e.disp(); getch(); return(0); }
Output
//if statement 1 is active
a= 55
a= 110
a= 165
a= 220
a= 275
//if statement 2 is active
a= 155
a= 210
a= 265
a= 320
a= 375
Example:4
C++ program to count total number of objects created in the program
Give output of the following program
#include <iostream> #include<conio.h> using namespace std; class sample { private: static int a; public: sample();//default constructor void disp(); }; int sample::a; sample::sample() { a=a+1; } void sample::disp() { cout<<"a= " << a<<endl; } int main() { /* clrscr(); */ sample e1,e2,e3,e4,hello; e4.disp();//1 e1.disp();//2 e2.disp();//3 sample e5,e8,e7,e9; e5.disp();//4 e1.disp();//5 e8.disp();//6 getch(); return(0); }
Output
a= 5
a= 5
a= 5
a= 9
a= 9
a= 9