#include <bits/stdc++.h>
using namespace std;
class Students {
int roll;
string name;
public:
void getData()
{
cout << "Enter roll number: ";
cin >> roll;
cout << "Enter name: ";
cin >> name;
}
void showData() {
cout << "Roll no. = " << roll << endl;
cout << "Name: " << name << endl;
}
};
int main() {
Students stu[20];
int n;
cout << "Enter the number of students: ";
cin >> n;
for (int i = 0; i < n; i++) {
stu[i].getData();
}
cout << "Displaying the data of student(s): " << endl;
for (int i = 0; i < n; i++) {
stu[i].showData();
}
return 0;
}
# Output
> Enter the number of students: 2
> Enter roll number: 1
> Enter name: Ram
> Enter roll number: 2
> Enter name: Shyam
> Displaying the data of student(s):
> Roll no. = 1
> Name: Ram
> Roll no. = 2
> Name: Shyam
Declaration of Static Data Member is done inside the class
static int a;
Definition is done outside the class.
int A:: a = 45;
#include <bits/stdc++.h>
using namespace std;
class StaticDemo {
public:
static int a; //Declaration
};
int StaticDemo::a = 5; //Defination
int main() {
cout << "a = " << StaticDemo::a;
return 0;
}
# Output
> a = 5
#include <bits/stdc++.h>
using namespace std;
class StaticDemo {
public:
static int a; //Declaration
static void output() {
cout << "a = " << a << endl;
}
};
int StaticDemo::a = 5; //Defination
int main() {
StaticDemo::output();
return 0;
}