Static members of a C++ class
2015-10-22 15:44
375 查看
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 are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
Let us try the following example to understand the concept of static data members:
When the above code is compiled and executed, it produces the following result:
A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to thethis pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not.
Let us try the following example to understand the concept of static function members:
When the above code is compiled and executed, it produces the following result:
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
Let us try the following example to understand the concept of 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++; } 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 make 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 are accessed using only the class name and the scope resolution operator ::.A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to thethis pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not.
Let us try the following example to understand the concept of 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++; } 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
相关文章推荐
- 1026. 程序运行时间(15)
- 整数转换为字符串的方法
- C,C++,VC, VC++,MFC有什么区别?
- 2014 蓝桥杯 预赛 c/c++ 本科B组 第三题:李白打酒 (8' )
- C/C++中volatile关键字
- C++ 读写注册表
- c语言学习之基础知识点介绍(三):scanf函数
- C++STL之queue队列容器
- 黑马程序员——OC语言日志——多态的概念和实现
- 2014 蓝桥杯 预赛 c/c++ 本科B组 第二题:切面条(5' )
- Google C++ 命名规范
- public protected private继承访问权限—C++
- C++STL之堆栈容器
- C++STL之bitset位集合容器
- C++ builder 使用TZipForge组件解压/压缩
- c++常用的字符串操作函数
- C语言的枚举类型
- C++各大有名科学计算库(转)
- c++虚拟函数virtual
- 黑马程序员——OC语言日志——description方法介绍及重写