ccplusplus.com
Learn C, C++ Concepts
Monday, December 19, 2011
static member variable example
/******************************************************* * File : static-member-variable.cpp * Author : Saurabh Gupta * Description : static member variable example c++ * Description : static member variable c++ * Date : PM 10:17 19 December 2011 * Source : http://www.ccplusplus.com/p/c_15.html * Note : *******************************************************/ #include
using namespace std; class CStatic { private: static int s_nStaticMember; int m_nConuter; public: CStatic(); ~CStatic(); int m_Show() const { return m_nConuter; } }; int CStatic::s_nStaticMember = 1; CStatic :: CStatic() { m_nConuter = s_nStaticMember++; } CStatic :: ~CStatic() { } int main() { /* * create the three instances of the class * and check the statis variable value */ CStatic cFirst; CStatic cSecond; CStatic cThird; cout << cFirst.m_Show() << endl; cout << cSecond.m_Show() << endl; cout << cThird.m_Show() << endl; return 0; } /* * OUTPUT * [sgupta@rhel6x64 19122011]$ c++ -o static-member-variable static-member-variable.cpp [sgupta@rhel6x64 19122011]$ ./static-member-variable 1 2 3 [sgupta@rhel6x64 19122011]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment