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
- 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
May be interested
- How to set up static IPs for computersusually your computer to dynamic ip address, but in some cases you need to set a static ip for the machine. the following article details how to set up static ip for computers on windows 7 and 8 operating systems.
- How to set a static IP on Windows 11 simplydo you want to change the static ip address on windows 11, set a static ip for your win 11 to share the folder with other lan users, or avoid the same ip that you can't access the network but don't know how? let's join taimienphi to set a static ip on windows 11 through the following article.
- Why use dynamic DNS instead of static IP address?most internet users don't think much about their ip addresses, but for anyone who hosts a server or accesses a device remotely, choosing between dynamic dns and static ip is important for both cost and convenience.
- Cursor to class in C ++a pointer to a class in c ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in c ++ as the -> operator, as you do with pointers to the structure.
- How to assign static IP addresses in Windows 7, 8, 10, XP or Vistasometimes, assigning an ip address to a better computer will allow the router to automatically assign an ip address. read this article to learn about assigning static ip addresses in windows.
- Class selector in CSSin css, class is used to style the element with the specified class.
- Pseudo-Class in CSSpseudo-class in css is used to add special effects to some selector.
- Here's how to check if your IP address is static or dynamicif using a cable or using a dsl service, most of the ip addresses you use are dynamic ip addresses. however, there are some internet service providers that assign static ip addresses. to check that the ip address you are using is a dynamic ip address or static ip, you can refer to the following article of network administrator.
- Static IP address or dynamic IP more secure?every device connected to the internet has its own ip (internet protocol) address, a unique string of numbers that distinguishes it from other machines.
- Property (Property) in C #properties - property is the member named for the layer, structure, and interface. member variables or methods in a class or structure are called fields. attribute is an inheritance of fields and is accessed using the same syntax. they use accessor through the values of private fields that can be read, written, and manipulated.