Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Static Functions

By Sripriya R | on February 12, 2007 |
C++ Tutorials

The static member functions have a class scope and they do not have access to the ‘this’ pointer of the class. When a member is declared as static that static member of class have only one such data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated.

Defining Static Function:

This is done by using the keyword static before the member function which is to be declared as static function.

General syntax:

  1. static return_data_type function_name() 
  2. //Static function defined with keyword static
  3. { 
  4. statement1; 
  5. statement2; 
  6. //Statements for execution inside static function
  7. ... 
  8. ... 
  9. }

For example if a function exforsys returning nothing is to be declared as static function it is done as follows:

  1. static void exforsys() 
  2. { 
  3. ... ; 
  4. ... ; 
  5. }

Accessing Static Function:

We have seen in our earlier sections about accessing a normal member function. To recollect, a normal member function gets accessed using the object and an operator called as the dot member access operator. The function declared as static or static functions gets accessed using just the class name and the operator called as scope resolution operator which is not possible in case of normal member functions.

Let us see an example to understand the declaration of static member function and how to access static member function in detail:

  1.  #include <iostream>
  2. using namespace std; 
  3.  
  4. class example 
  5. { 
  6. private: 
  7.  
  8. 	int x; 
  9. 	static int sum; //Static data
  10.  
  11. public: 
  12.  
  13. 	example() //Constructor of the class
  14. 	{ 
  15. 		sum=sum+1; 
  16. 		x=sum; 
  17. 	} 
  18.  
  19. 	~example() //Destructor of the class 
  20. 	{ 
  21. 		sum=sum-1; 
  22. 	} 
  23.  
  24. 	static void exforsys() 
  25. 		//Static function exforsys( ) defined with keyword static
  26. 	{ 
  27. 		cout << "nResult is: " << sum; 
  28. 	} 
  29.  
  30. 	void number() //Normal member function number( )
  31. 	{ 
  32. 		cout << "nNumber is: " << x; 
  33. 	} 
  34. }; 
  35.  
  36. int example::sum=0;
  37.  
  38. void main() 
  39. { 
  40. 	example e1; 
  41. 	example::exforsys(); 
  42. 	//Static function exforsys() accessed using class name example and the scope resolution operator ::
  43.  
  44. 	example e2,e3,e4; 
  45. 	example::exforsys(); 
  46. 	e1.number(); 
  47. 	e2.number(); 
  48. 	//Normal member function accessed using object e1 and the dot member access operator .
  49. 	e3.number(); 
  50. 	e4.number(); 
  51.  
  52. }

The output of the above program is:

In the above we have seen that the function exforsys() s defined as static function and the integer data type sum is declared as static data type. We see that four objects e1, e2, e3 and e4 are created for the class example. The constructor of the class example increments the sum by 1 and the destructor of the class decrements sum by 1.

We can see that the static function is accessed using the class name example and the scope resolution operator :: as

example::exforsys();

But the normal member function number() is accessed using the object name and the dot member access operator as

  1. e1.number() 
  2. e2.number() 
  3. e3.number() 
  4. e4.number()

The first time when the static function exforsys() is called there was one object created and this the sum gets incremented by 1 in the constructor printing the result of sum as 1.When the static function exforsys() is called the second time there were three more objects e2,e3 and e4 created which makes the sum to gets incremented thrice from 1 in the constructor of the corresponding class namely example making the value of sum as 4 which is displayed in the second result. From the above explanation it is clear that the static function operate on the class and not in object generally. Also for accessing static function one can use the class name followed by the scope resolution operator as seen in example above.

One must note the following while using static member functions:

A static member function can access only static member data, static member functions and data and functions outside the class. So one must take essential care not to use static member function like a non-static member function which can access all of the above including the static data member.

A non-static member functions can be declared as virtual but care must be taken not to declare a static member function as virtual.

One must first understand the concept of static data also while learning the context of static functions. It is possible to declare a data member of a class as static irrespective of it being public or private type in class definition. If a data is declared as static then the static data gets created and initialized only once. That is unlike non-static data members that are created again and again, for each separate object of the class the static data gets created and initialized only once. Just like the concept of static data, in which the variables are shared by all objects of the class in static functions also it apply to all objects of the class.

A non-static member function can be called only after instantiating the class as an object. But it is not the case with static member functions, because a static member function can be called, even when a class is not instantiated.

Also a static member function cannot have access to the ‘this’ pointer of the class.

« « How Does a Data Warehouse Differ From a Database
Creating an Efficient Process for Data Warehouses » »

Author Description

Avatar

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • C++ Inheritance

    August 19, 2007 - 0 Comment
  • C++ Standard Input Output Stream

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ String Representation and Handling

    September 8, 2007 - 0 Comment
  • C++ Abstraction

    April 26, 2007 - 0 Comment
  • C++ Operators Part I

    August 24, 2007 - 0 Comment
  • C++ Structure Part II

    August 19, 2007 - 0 Comment
  • C++ Encapsulation

    August 16, 2007 - 0 Comment
  • C++ Operators Part II

    August 27, 2007 - 0 Comment
  • C++ Operator Overloading Part 1

    September 7, 2006 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT