Program for Static Data and Member Function Using C++ Programming

Affiliate Disclosure: Every purchase made through our affiliate links earns us a pro-rated commission without any additional cost to you. Here are more details about our affiliate disclosure.

Program to count the object value using the storage keyword static in C++

Algorithm

Step 1: Start the program.
Step 2: Declare the class name as Stat with data member s and member functions.
Step 3: The constructor Stat() which is used to increment the value of count as 1 to to assign the variable code.
Step 4: The function showcode() to display the code value.
Step 5: The function showcount() to display the count value.
Step 6: Stop the program.

Program

/* Program to count the object value using the storage keyword static in C++
Pardeep Patel @ studywarehouse.com */

#include<iostream.h>
#include<conio.h>

class stat
{
int code;
static int count;

public:
stat()
{
code=++count;
}
void showcode()
{
cout<<"\n\tObject number is :"<<code;
}
static void showcount()
{
cout<<"\n\tCount Objects :"<<count;
}
};

int stat::count;

void main()
{
clrscr();
stat obj1,obj2;

obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
getch();
}

Output

Count Objects: 2
Object Number is: 1
Count Objects: 2
Object Number is: 2

Pardeep Patelhttps://pardeeppatel.com/
Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Articles

Discovering Possibilities: 19 Free AI Tools for Creative Minds

Welcome to a realm where creativity knows no bounds! In the ever-evolving landscape of technology, the fusion of innovation and...
Read more
Adding Personality to Your Photos with Free Tools to Create Your Signature - In the ever-evolving world of photography, there's...
Search engine optimization (SEO) is a digital marketing discipline that hinges on a crucial element: keywords. In the vast landscape...