Static Variables Lab
Static Variables Lab
{ {
int roll; int roll;
char name[20]; char name[20];
int i;
public: public:
void display() void display()
{ {
printf("%d ", roll); printf("%d ", roll);
puts(name); puts(name);
} }
}; };
void main()
{
student s1;
student s2;
cout << sizeof(s1)<<endl;
cout << sizeof(s2)<<endl;
cout << sizeof(student)<<endl;
cout << sizeof(student*)<<endl;
}
class student class student
{ {
int roll; int roll;
static int i; static int i;
public: public:
student()
{
i = 1;
roll = i;
i++;
cout << "const of " << i << endl;
void display() }
{ void display()
cout << roll << endl; {
cout << roll << endl;
}
}
void show()
void show()
{
{
cout << i << endl;
cout << i << endl;
}
}
};
};
s1.display(); s1.display();
s1.show(); s1.show();
} }
student() student()
{ {
roll = i; roll = i;
i++; i++;
cout << "const of " << roll << endl; cout << "const of " << roll << endl;
} }
void display() void display()
{ {
cout << "Value of Roll Number =" << roll << endl; cout << "Value of Roll Number =" << roll << endl;
} }
void show() void show()
{ {
cout <<"Value of i="<< i << endl; cout <<"Value of i="<< i << endl;
} }
}; };
int student::i = 100; int student::i = 1000;
s1.display(); s1.display();
s1.show(); s1.show();
s2.display(); s2.display();
s2.show(); s2.show();
} }
class student class student
{ {
int roll; int roll;
static int i; static int i;
public: public:
student() student()
{ {
i++;
roll = i; roll = i;
i++;
cout << "const of " << roll << endl; cout << "const of " << roll << endl;
} }
void display() void display()
{ {
cout << "Value of Roll Number =" << roll << endl; cout << "Value of Roll Number =" << roll << endl;
} }
void show() void show()
{ {
cout <<"Value of i="<< i << endl; cout <<"Value of i="<< i << endl;
} }
}; };
int student::i = 0; int student::i = 0;
s1.display(); s1.display();
s1.show(); s1.show();
s2.display(); s2.display();
s2.show(); s2.show();
} }
class student
{
int roll;
static int i;
public:
student()
{
i++;
roll = i;
cout << "const of " << roll << endl;
}
student(int num)
{
i = i + 10;
roll = i;
cout << "11111111 const of " << roll <<
endl;
}
void display()
{
cout << "Value of Roll Number =" << roll << endl;
}
void show()
{
cout <<"Value of i="<< i << endl;
}
};
int student::i = 0;
void main() void main()
{ {
student s1; student s1(1);
student s2(2); student s2;
s1.display(); s1.display();
s1.show(); s1.show();
s2.display(); s2.display();
s2.show(); s2.show();
} }
class student void main()
{ {
int roll; student s1;
static int i; student s2;
static int j;
public: s1.display();
student() s1.show();
{ s2.display();
i++; s2.show();
roll = i; }
cout << "const of " << roll << endl; void main()
} {
student(int num) student s1;
{ student s2(2);
i = i + 1;
roll = i; s1.display();
cout << "11111111 const of " << roll << s1.show();
endl; s2.display();
z } s2.show();
~student() }
{ void main()
j++; {
cout << "dstr of " << roll << endl; student s1;
cout << "i " << i << endl; s1.display();
cout << "j " << j << endl; s1.show();
} student s2;
void display() s2.display();
{ s2.show();
cout << "Value of Roll Number =" << roll << endl; }
} void main()
void show() { student s1;
{ s1.display();
cout << "Value of i=" << i << endl; s1.show();
cout << "Value of j=" << j << endl; student s2(2);
} s2.display();
}; s2.show();
int student::i = 0; }
int student::j = 0;
void main()
class student {
{ student s1;
int roll; s1.show();
static int i; student s2(2);
static int j; s2.show();
}
public: void main()
student() {
{ student s1;
i++; s1.show();
roll = i; {
cout << "const of " << roll << endl; student s2(2);
} s2.show();
student(int num) }
{ student s3;
i = i + 1; }
roll = i; void main()
cout << "11111111 const of " << roll << {
endl; student s1;
z } s1.show();
~student() {
{ student s2(2);
j++; s2.show();
cout << "dstr of " << roll << endl; }
cout << "i " << i << endl; student s3;
cout << "j " << j << endl; s3.show();
} }
void display() void main()
{ {
cout << "Value of Roll Number =" << roll << endl; student s1;
} s1.show();
void show() {
{ student s2(2);
cout << "Value of Roll Number =" << roll << endl; s2.show();
cout << "Value of i=" << i << endl; }
cout << "Value of j=" << j << endl; student s3;
} s2.show();
}; }
int student::i = 0;
int student::j = 0;
class student class student
{ {
int roll; int roll;
static int i; static int i;
static int j; static int j;
public: public:
student() student()
{ {
i++; i++;
roll = i; roll = i;
cout << "const of " << roll << endl; cout << "const of " << roll << endl;
} }
student(int num) student(int num)
{ {
i = i + 1; i = i + 1;
roll = i; roll = i;
cout << "11111111 const of " << roll << cout << "11111111 const of " << roll <<
endl; endl;
z } z }
~student() ~student()
{ {
j++; j++;
cout << "dstr of " << roll << endl; cout << "dstr of " << roll << endl;
cout << "i " << i << endl; cout << "i " << i << endl;
cout << "j " << j << endl; cout << "j " << j << endl;
} }
void display() void display()
{ {
cout << "Value of Roll Number =" << roll << endl; cout << "Value of Roll Number =" << roll << endl;
cout << "Value of i=" << i << endl; cout << "Value of i=" << i << endl;
cout << "Value of j=" << j << endl; cout << "Value of j=" << j << endl;
} }
void show() static void show()
{ {
cout << "Value of Roll Number =" << roll << endl; cout << "Value of Roll Number =" << roll << endl;
cout << "Value of i=" << i << endl; cout << "Value of i=" << i << endl;
cout << "Value of j=" << j << endl; cout << "Value of j=" << j << endl;
} }
}; };
int student::i = 0; int student::i = 0;
int student::j = 0; int student::j = 0;
void main() void main()
{ {
student s1; student s1;
s1.show(); s1.show();
s1.display(); s1.display();
} }
void main()
{
student::show();
student::display();
}