Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C++ programs » C++ Class and Objects programs

Count the created objects using static member function in C++



Here, we are going to learn by a program how to count the created objects using static member function in C++?
Submitted by IncludeHelp, on September 14, 2018

Static data member

A static data member is also known as "Class Member" and it is available for all classes. In other words – we can say "A static data member is just like a global variable for a class". Generally, static data members are used to maintain the common things related to the class, like counting the objects etc.

Static Member Function

A static member function is used to access or manipulate the static data member, other data member cannot be used with a static member function.

Program:

This program is implemented to count the total number of created objects with the help of static data member and static member function.

  • A static data member needs to be declared first that we declared in private section: Example: static int count;
  • To count the object, data member count must be incremented by 1, that we have done in the constructor because the constructor is invoked when a new object is created.
  • To print the total number of created objects, we created a static member function named totalObjects() which is returning the value of count.
  • Final and important thing – A static data member must be initialized outside of the class with the class name. Example: int Message::count =0;
#include <iostream>
#include <string.h>
using namespace std;

class Message
{
	private:
		char str[30];
		//static data member to count the objects
		static int count;
		
	public:
		//constructor
		Message()
		{
			count++;
		}
		
		//member function to initialise message
		void initMessage(char s[])
		{
			strcpy(str,s);
		}
		
		//member function to print message
		void printMessage(void)
		{
			cout << str << endl;
		}
		
		//static member function
		static int totalObjects(void)
		{
			return count;
		}
};

//initialise static member function
int Message::count =0;

//main function
int main()
{
	Message M1;
	Message M2;
	Message M3;
	
	M1.initMessage("Message one");
	M2.initMessage("Message two");
	M3.initMessage("Message three");
	
	M1.printMessage();
	M2.printMessage();
	M3.printMessage();
	
	//printing object count 
	cout << "Total objects created: " << Message::totalObjects() << endl;
	
	return 0;
}

Output

Message one
Message two
Message three
Total objects created: 3





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.