Static Member in C++
Static Member in C++
1. #include <iostream>
2. #include <string.h>
3. using namespace std;
4. // create class of the Car
5. class Car
6. {
7. private:
8. int car_id;
9. char car_name[20];
10. int marks;
11.
12. public:
13. // declare a static data member
14. static int static_member;
15.
16. Car()
17. {
18. static_member++;
19. }
20.
21. void inp()
22. {
23. cout << " \n\n Enter the Id of the Car: " << endl;
24. cin >> car_id; // input the id
25. cout << " Enter the name of the Car: " << endl;
26. cin >> car_name;
27. cout << " Number of the Marks (1 - 10): " << endl;
28. cin >> marks;
29. }
30.
31. // display the entered details
32. void disp ()
33. {
34. cout << " \n Id of the Car: " << car_id;
35. cout << "\n Name of the Car: " << car_name;
36. cout << " \n Marks: " << marks;
37.
38. }
39. };
40.
41. // initialized the static data member to 0
42. int Car::static_member = 0;
43.
44. int main ()
45. {
46. // create object for the class Car
47. Car c1;
48. // call inp() function to insert values
49. c1. inp ();
50. c1. disp();
51.
52. //create another object
53. Car c2;
54. // call inp() function to insert values
55. c2. inp ();
56. c2. disp();
57.
58.
59. cout << " \n No. of objects created in the class: " << Car :: st
atic_member <<endl;
60. return 0;
61. }
Output
class Student {
public:
// static member
static int total;
// Constructor called
Student() { total += 1; }
};
int Student::total = 0;
int main()
{
// Student 1 declared
Student s1;
cout << "Number of students:" << s1.total << endl;
// Student 2 declared
Student s2;
cout << "Number of students:" << s2.total << endl;
// Student 3 declared
Student s3;
cout << "Number of students:" << s3.total << endl;
return 0;
}
Output
Number of students:1
Number of students:2
Number of students:3
Static Member Function in C++
Static Member Function in a class is the function that is declared as
static because of which function attains certain properties as defined
below:
A static member function is independent of any object of the
class.
A static member function can be called even if no objects of
the class exist.
A static member function can also be accessed using the
class name through the scope resolution operator.
A static member function can access static data members
and static member functions inside or outside of the class.
Static member functions have a scope inside the class and
cannot access the current object pointer.
You can also use a static member function to determine how
many objects of the class have been created.
The reason we need Static member function:
Static members are frequently used to store information
that is shared by all objects in a class.
For instance, you may keep track of the quantity of newly
generated objects of a specific class type using a static data
member as a counter. This static data member can be
increased each time an object is generated to keep track of
the overall number of objects.
Example:
C++
class Box
{
private:
static int length;
static int breadth;
static int height;
public:
// Driver Code
int main()
{
Box b;
cout << "Static member function is called through Object name: \n" << endl;
b.print();
cout << "\nStatic member function is called through Class name: \n" << endl;
Box::print();
return 0;
}
Output
Static member function is called through Object name:
Syntax
The data_type is the variable type in C++, such as int, float, string, etc.
The data_member is the name of the static data.
Syntax
1. class_name::function_name (parameter);
1. #include <iostream>
2. using namespace std;
3. class Note
4. {
5. // declare a static data member
6. static int num;
7.
8. public:
9. // create static member function
10.static int func ()
11. {
12.return num;
13. }
14.};
15. // initialize the static data member using the class name and t
he scope resolution operator
16.int Note :: num = 5;
17.
18.int main ()
19. {
20.// access static member function using the class name and the scope resol
ution
21. cout << " The value of the num is: " << Note:: func () << end
l;
22.return 0;
23. }
Output
1. #include <iostream>
2. using namespace std;
3. class Note
4. {
5. // declare a static data member
6. static int num;
7.
8. public:
9. // create static member function
10.static int func ()
11. {
12.cout << " The value of the num is: " << num << endl;
13. }
14.};
15. // initialize the static data member using the class name and t
he scope resolution operator
16.int Note :: num = 15;
17.
18.int main ()
19. {
20. // create an object of the class Note
21. Note n;
22.// access static member function using the object
23. n.func();
24.
25. return 0;
26.}
Output
1. #include <iostream>
2. using namespace std;
3. class Member
4. {
5.
6. private:
7. // declaration of the static data members
8. static int A;
9. static int B;
10.static int C;
11.
12.// declare public access specifier
13. public:
14.// define the static member function
15. static void disp ()
16.{
17. cout << " The value of the A is: " << A << endl;
18.cout << " The value of the B is: " << B << endl;
19. cout << " The value of the C is: " << C << endl;
20.}
21. };
22.// initialization of the static data members
23. int Member :: A = 20;
24.int Member :: B = 30;
25. int Member :: C = 40;
26.
27. int main ()
28.{
29. // create object of the class Member
30.Member mb;
31. // access the static member function using the class object na
me
32.cout << " Print the static member through object name: " << endl;
33. mb. disp();
34.// access the static member function using the class name
35. cout << " Print the static member through the class name: " <
< endl;
36.Member::disp();
37. return 0;
38.}
Output
Example
// C++ program to Count the number of objects
// using the Static member function
#include <iostream>
using namespace std;
class Student {
public:
int rollno;
static int count;
public:
Student(){
rollCall();
rollno=count;
}
~Student()
{ --count; }
static void rollCall(void){
cout <<endl<<"Student Count:" << ++count<< "\n"; //object count
}
};
int Student::count;
int main(){
Student stu1;
cout<<"Student 1: Roll No:"<<stu1.rollno;
Student stu2;
cout<<"Student 2: Roll No:"<<stu2.rollno;
Student stu3;
cout<<"Student 3: Roll No:"<<stu3.rollno;
Student stu4;
cout<<"Student 4: Roll No:"<<stu4.rollno;
return 0;
}
Output
If we run the above code it will generate the following output −
Student Count:1
Student 1: Roll No:1
Student Count:2
Student 2: Roll No:2
Student Count:3
Student 3: Roll No:3
Student Count:4
Student 4: Roll No:4
Example
#include <iostream>
using namespace std;
class My_Class{
private:
static int count;
public:
My_Class() { //in constructor increase the count value
cout << "Calling Constructor" << endl;
count++;
} static int objCount() {
return count;
}
};
int My_Class::count;
main() {
My_Class my_obj1, my_obj2, my_obj3;
int cnt;
cnt = My_Class::objCount();
cout << "Number of objects:" << cnt;
}
Output
Calling Constructor
Calling Constructor
Calling Constructor
Number of objects:3