Clear, practical technology insights About · Contact

Static Member Functions in C ++: Static Member of Class in C ++

Understand Static Member Functions in C ++: Static Member of Class in C ++ with clear explanations, practical examples, and useful tips. This updated guide...

Published: 2 minutes read
Table of Contents

Static Member Functions in C ++: Static Member of Class in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

We can define class members as static using the static keyword in C ++. When we declare a member of a class that is static, that is, no matter how many objects of the class are created, there will only be one copy of the static member.

A static member is shared by all objects of the class. All static data is initialized to 0 when the first object is created, if no other initialization is present. We cannot put it in the class definition, but it can be defined outside that class as done in the example below by declaring a static variable, using the scope resolution operator:: to identify Which class owns it.

You try the example below to understand the concept of static members in C ++:

 #include using namespace std ; class Box { public : static int biendemDT ; // phan dinh nghia Constructor Box ( double dai = 1.0 , double rong = 1.0 , double cao = 1.0 ) { cout << "Constructor duoc goi." << endl ; chieudai = dai ; chieurong = rong ; chieucao = cao ; // gia tri duoc tang len moi khi doi tuong duoc tao biendemDT ++; } double theTich () { return chieudai * chieurong * chieucao ; } private : double chieudai ; // chieu dai cua mot box double chieurong ; // chieu rong cua mot box double chieucao ; // chieu cao cua mot box }; // Khoi tao ham thanh vien cua lop Box int Box :: biendemDT = 0 ; int main ( void ) { Box Box1 ( 2.4 , 4.2 , 2.2 ); // khai bao box1 Box Box2 ( 4.5 , 2.0 , 3.2 ); // khai bao box2 // in tong so doi tuong. cout << "Tong so doi tuong la: " << Box :: biendemDT << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Static member functions in C ++

By declaring a static member function in C ++, you do it independently of any specific object of the class. A static member function can be called even if no class object exists and static functions are accessed only by using the class name and scope resolution operator :: in C ++.

A member function can only access static data members, other static member functions, and any other functions from outside that class.

Static member functions have a class scope and they have no access to this class's pointer in C ++. You can use a member function to determine whether or not some objects of the class have been created.

You try the following example to understand the concept of static member functions in C ++:

 #include using namespace std ; class Box { public : static int biendemDT ; // phan dinh nghia Constructor Box ( double dai = 1.0 , double rong = 1.0 , double cao = 1.0 ) { cout << "Constructor duoc goi." << endl ; chieudai = dai ; chieurong = rong ; chieucao = cao ; // gia tri cua biendemDT tang len moi khi doi tuong duoc tao biendemDT ++; } double theTich () { return chieudai * chieurong * chieucao ; } static int layBienDem () { return biendemDT ; } private : double chieudai ; // Chieu dai cua mot box double chieurong ; // Chieu rong cua mot box double chieucao ; // Chieu cao cua mot box }; // khoi tao thanh vien static cua lop Box int Box :: biendemDT = 0 ; int main ( void ) { // in tong so doi tuong duoc tao truoc khi tao cac doi tuong. cout << "So doi tuong ban dau la: " << Box :: layBienDem () << endl ; Box Box1 ( 2.4 , 4.2 , 2.2 ); // khai bao box1 Box Box2 ( 4.5 , 2.0 , 3.2 ); // khai bao box2 // in tong so doi tuong duoc tao sau khi tao cac doi tuong. cout << "Tong so doi tuong sau khi tao la: " << Box :: layBienDem () << endl ; return 0 ; } 

Next lesson: Calculate inheritance in C ++

Frequently Asked Questions

What should you know about static member functions in C ++?

By declaring a static member function in C ++, you do it independently of any specific object of the class. A static member function can be called even if no class object exists and static functions are accessed only by using the class name and scope.

What is Static Member Functions in C ++: Static Member of Class in C ++?

We can define class members as static using the static keyword in C ++.

Why is Static Member Functions in C ++: Static Member of Class in C ++ important?

A clear understanding of Static Member Functions in C ++: Static Member of Class in C ++ helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.