Static member of class in C ++
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 following example by declaring a static variable, using the scope resolution operator :: to identify Which class owns it.
You try the following example 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 ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Cursor to class in C ++
Next lesson: Calculate inheritance in C ++
You should read it
- Polymorphism in C #
- What is a static IP address?
- How to set up static IPs for computers
- How to set a static IP on Windows 11 simply
- How to assign static IP addresses in Windows 7, 8, 10, XP or Vista
- Here's how to check if your IP address is static or dynamic
- Static IP address or dynamic IP more secure?
- Which is better DHCP or static IP?
- How to Configure a Static Internet Protocol (IP) Address on a Computer
- Fix can not change static IP, fix can not change DNS on Windows 10
- Set up the Linksys router with a static IP address
- What is static IP? What is dynamic IP? Distinguish between static IP and dynamic IP
Maybe you are interested
Set up static routing between the router and another network
How to assign a static IP address in Windows 11, 10, 8, 7, XP or Vista
Which is better DHCP or static IP?
How to use AWS S3 Bucket to store static and media files in Django
How to configure static IP address on Ubuntu 22.04 LTS and 22.10
Static IP address or dynamic IP more secure?