Definition of static data members in c++ :
It is known as a class data members and declared by using the Static keyword. it is a single copy of the variable created for all objects in C++.
Declaration :
Static Data_type Data_member_name
Static: Static is a keyword
Datatype : data_type in variable type in C++ like : int, float
Data Member Name: data member name is a user defined name of the variable
Example
static int sum;
Define the value of static data member
Also Read This :- function overloading in c++
it should be declared outside of class like this:
data_type class_name :: static_data_member_name = value;
Example of Static Data Member
#include<iostream> #include<string.h> using namespace std; class Student { Â private: Â int rollNo; Â char name[10]; Â int marks; Â public: Â static int objectCount; Â Student() { Â Â objectCount++; } void getdata() { Â cout << "Enter roll number: "<<endl; Â cin >> rollNo; Â cout << "Enter name: "<<endl; Â cin >> name; Â cout << "Enter marks: "<<endl; Â cin >> marks; } void putdata() { Â cout<<"Roll Number = "<< rollNo <<endl; Â cout<<"Name = "<< name <<endl; Â cout<<"Marks = "<< marks <<endl; Â cout<<endl; } }; int Student::objectCount = 0; int main(void) { Â Student s1; Â s1.getdata(); Â s1.putdata(); Â Student s2; Â s2.getdata(); Â s2.putdata(); Â Student s3; Â s3.getdata(); Â s3.putdata(); Â cout << "Total created Object of Student Class = " << Student::objectCount << endl; Â return 0; }
output
Enter roll number: 1 Enter name: AAA Enter marks: 80 Roll Number = 1 Name = AAA Marks = 80 Â Enter roll number: 2 Enter name: BBB Enter marks: 65 Roll Number = 2 Name = BBB Marks = 65 Â Enter roll number: 3 Enter name: CCC Enter marks: 90 Roll Number = 3 Name = CCC Marks = 90 Â Total created Object of Student Class = 3