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.

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:

Picture 1 of Static member of class in C ++

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:

Picture 2 of Static member of class in C ++

According to Tutorialspoint

Previous article: Cursor to class in C ++

Next lesson: Calculate inheritance in C ++

« PREV POST
READ NEXT »