Static members of a C + + class

Source: Internet
Author: User

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class be created, there is only One copy of the static member.

A Static member is shared by all objects of the class . all static data are initialized to zero when the first object is created , if no and initialization is P Resent. We can ' t put it in the class definition but it can be initialized outside the class as did in the following example by re Declaring the static variable, using the scope resolution OPERATOR  ::  to Identify which class I T belongs to.

let US Try the following example to understand the concept of the static data members:

#include  <iostream> using namespace std;class box{   public:       static int objectcount;      //  constructor definition      box (double l=2.0, double b =2.0, double h=2.0)       {          cout << "constructor called."  << endl;         length = l;          breadth = b;          height = h;         // increase  every time object is created          Objectcount++;      }     &nbSp;double volume ()       {          return length * breadth * height;      }    private:      double length;     //  Length of a box      double breadth;     // Breadth of a box      double height;      // height of a box};// initialize static member  of class boxint box::objectcount = 0;int main (void) {   Box  box1 (3.3, 1.2, 1.5);     // declare box1   box  box2 (8.5, 6.0, 2.0);     // declare box2   //  print total number of objects.   cout <<  "total objects: "  <<  box::objectcount << endl;   return 0;}

When the above code is compiled and executed, it produces the following result:

Constructor called. Constructor called. Total Objects:2
Static Function Members:

By declaring a function member as static, you do it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions is Accesse D using only the class name and the scope resolution operator ::.

A static member function can only access to static data member, other static member functions and any other functions from OU Tside the class.

Static member functions has a class scope and they do not has access to the this pointer of the class. You could use a static member function to determine whether some objects of the class has been created or not.

Let us try the following example to understand the concept of the static function members:

#include  <iostream> using namespace std;class box{   public:       static int objectcount;      //  constructor definition      box (double l=2.0, double b =2.0, double h=2.0)       {          cout << "constructor called."  << endl;         length = l;          breadth = b;          height = h;         // increase  every time object is created          Objectcount++;      }     &nbSp;double volume ()       {          return length * breadth * height;      }       static int getcount ()       {          return objectCount;      }    private:      double length;     //  Length of a box      double breadth;     // Breadth of a box      double height;      // height of a box};// initialize static member  of class boxint box::objectcount = 0;int main (void) {      // Print total number of objects before creating object.   cout < <  "inital stage count: "  << box::getcount ()  << endl;    box box1 (3.3, 1.2, 1.5);     // declare box1    box box2 (8.5, 6.0, 2.0);     // declare box2    // Print total number of objects after creating  object.   cout <<  "final stage count: "  << Box:: GetCount ()  << endl;   return 0;}

When the above code is compiled and executed, it produces the following result:

Inital Stage Count:0constructor called. Constructor called. Final Stage Count:2


Static members of a C + + class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.