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 October 1, 2007 |
C++ Tutorials

Topics

  • Syntax
  • How to access
  • Example
  • Keep in Mind

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, a static member of class, it has only one 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

Static function is defined by using the keyword static before the member function that is to be declared as static function.

Syntax

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

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

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

Accessing Static Function

A normal member function is accessed using the object and an operator called the dot member access operator. The functions declared static or static functions are accessed using only the class name and the scope resolution operator, unlike in normal member functions where these are not used.

Static Function Example

The declaration of static member function and how to access static member function:

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

The output of the above program is:

In the above example, the function exforsys() is defined as static function and the integer data type sum is declared as static data type. 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.

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 the static function exforsys() is called, there was one object created and thus, the sum is 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 results in the sum incremented thrice from 1 in the constructor of the corresponding class example, resulting in the value of sum as 4, which is displayed in the second result. Applying the above explanation, it is clear that the static function operates on the class and not in object. To access static function the programmer can use the class name, followed by the scope resolution operator, as seen in example above.

Things to keep in mind while using static member functions

1. A static member function can only access static member data, static member functions and data and functions outside the class.

2. You must take note not to use static member function in the same manner as non-static member function, as non-static member function can access all of the above including the static data member.

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

4. The programmer must first understand the concept of static data while learning the context of static functions. It is possible to declare a data member of a class as static irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once. Non-static data members are created again and again. For each separate object of the class, the static data is created and initialized only once. As in the concept of static data, all objects of the class in static functions share the variables. This applies to all objects of the class.

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

6. A static member function cannot have access to the ‘this’ pointer of the class.

« « SOA Principles
C++ Pointers » »

Author Description

Avatar

Free Training

RSSSubscribe 392 Followers
  • Popular
  • Recent
  • C++ Functions

    September 21, 2006 - 0 Comment
  • Object Oriented Programming Paradigm

    June 10, 2006 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Looping

    September 4, 2007 - 0 Comment
  • C++ Functions with Arguments

    September 21, 2006 - 0 Comment
  • Elements of Object Oriented Programming

    June 10, 2006 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Type Conversions

    August 13, 2006 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment
  • Basic concepts of OOPS and Structure of C++ program

    July 26, 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++ 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
  • C++ Storage Classes

    September 10, 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++ Friend 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