<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 23 Static Members and Methods_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 23: Static Members and Methods
 More of this Feature
• Introduction, Static Members
• Example Program
• Static Members Concluded, Static Methods
• Static Methods Continued.
 
 Related Resources
• C++ Tutorial - Classes, Members and Methods.
• C++ Tutorial - See all lessons.
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. This lesson covers static class members and methods. Static members provide a way to create objects that are shared by an entire class, rather than being part of a particular instance of the class, an object. Static methods provide a way to access and update static members.

Static Members
Suppose it's necessary to keep track of some data that's related to a class as a whole, rather than to a particular object or instance of that class. A simple example is a count of the total number of objects instantiated. Maybe we have an Employee class and we want to keep track of the total number of employees. We could create a global variable to hold this data.

class Employee
{
    ....
}

int numEmployees;

main()
{
    Employee janitor;
    numEmployees++;

    Employee manager;
    numEmployees++;

    .....
}

While this approach will work, it has a major short fall. The global variable, numEmployees is not really related to the class Employee. Ideally, we want all data associated with the class to be encapsulated within that class. Although in this simple example it is obvious that numEmployees pertains to the Employee class, in larger programs the relationship might not apparent. The static keyword provides a way to declare that an object is to be shared by all instances of a class. Here's an example.



Next page > Example of Static Members > Page 1, 2, 3, 4


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.