0% found this document useful (0 votes)
4 views12 pages

Static Member in C++

The document explains the concept of static member functions and static data members in C++. It details their characteristics, syntax, and provides multiple examples demonstrating their usage in C++ programs. Static members are shared among all instances of a class and can be accessed without creating an object of the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Static Member in C++

The document explains the concept of static member functions and static data members in C++. It details their characteristics, syntax, and provides multiple examples demonstrating their usage in C++ programs. Static members are shared among all instances of a class and can be accessed without creating an object of the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Static Member Function in C++

The static keyword is used with a variable to make the memory of


the variable static once a static variable is declared its memory
can’t be changed

Static Member in C++


Static members of a class are not associated with the objects of the
class. Just like a static variable once declared is allocated with
memory that can’t be changed every object points to the same
memory.
C++ Static Data Members
Static data members are class members that are declared
using static keywords. A static member has certain special characteristics
which are as follows:
 Only one copy of that member is created for the entire class and is
shared by all the objects of that class, no matter how many objects
are created.
 It is initialized before any object of this class is created, even before
the main starts.
 It is visible only within the class, but its lifetime is the entire
program.
Syntax:
static data_type data_member_name;

Example 1: Let's create a simple program to access the static data


members in the C++ programming language.

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

Enter the Id of the Car:


101
Enter the name of the Car:
Ferrari
Number of the Marks (1 - 10):
10

Id of the Car: 101


Name of the Car: Ferrari
Marks: 10

Enter the Id of the Car:


205
Enter the name of the Car:
Mercedes
Number of the Marks (1 - 10):
9

Id of the Car: 205


Name of the Car: Mercedes
Marks: 9
No. of objects created in the class: 2

Once a static member is declared it will be treated as same for all


the objects associated with the class.
Example:
 C++

// C++ Program to demonstrate


// Static member in a class
#include <iostream>
using namespace std;

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++

// C++ Program to show the working of


// static member functions
#include <iostream>
using namespace std;

class Box
{
private:
static int length;
static int breadth;
static int height;

public:

static void print()


{
cout << "The value of the length is: " << length << endl;
cout << "The value of the breadth is: " << breadth << endl;
cout << "The value of the height is: " << height << endl;
}
};

// initialize the static data members

int Box :: length = 10;


int Box :: breadth = 20;
int Box :: height = 30;

// 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:

The value of the length is: 10


The value of the breadth is: 20
The value of the height is: 30

Static member function is called through Class name:

The value of the length is: 10


The value of the breadth is: 20
The value of the height is: 30

Static Member Function in C++


The static is a keyword in the C and C++ programming language. We use
the static keyword to define the static data member or static member
function inside and outside of the class. Let's understand the static data
member and static member function using the programs.

Static data member


When we define the data member of a class using the static keyword, the
data members are called the static data member. A static data member is
similar to the static member function because the static data can only be
accessed using the static data member or static member function. And, all
the objects of the class share the same copy of the static member to
access the static data.

Syntax

1. static data_type data_member;

Here, the static is a keyword of the predefined library.

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.

Static Member Functions


The static member functions are special functions used to access the
static data members or other static member functions. A member function
is defined using the static keyword. A static member function shares the
single copy of the member function to any number of the class' objects.
We can access the static member function using the class name or class'
objects. If the static member function accesses any non-static data
member or non-static member function, it throws an error.

Syntax

1. class_name::function_name (parameter);

Here, the class_name is the name of the class.

function_name: The function name is the name of the static member


function.

parameter: It defines the name of the pass arguments to the static


member function.

Example 2: Let's create another program to access the static member


function using the class name in the C++ programming language.

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

The value of the num is: 5

Example 3: Let's create another program to access the static member


function using the class' object in the C++ programming language.

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

The value of the num is: 15

Example 4: Let's consider an example to access the static member


function using the object and class in the C++ programming language.

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

Print the static member through object name:


The value of the A is: 20
The value of the B is: 30
The value of the C is: 40
Print the static member through the class name:
The value of the A is: 20
The value of the B is: 30
The value of the C is: 40

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

You might also like