Categories: Blog

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

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 Patel

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.

Recent Posts

The Dark Side of SEO: How to Protect Your Website from Negative Attack

"The Dark Side of SEO: How to Protect Your Website from Negative Attacks" – this…

2 days ago

Inspiring Young Minds: Writing, Websites, and Unleashing Creativity

In a world brimming with digital possibilities, inspiring young minds to express themselves through writing…

2 months ago

Why SEO is Essential for Business Growth: Tips for Growing Your Website

In today's fiercely competitive digital landscape, understanding why SEO is essential for business growth and…

2 months ago

10 Free Online Courses from MIT

Dive into the world of knowledge with "10 Free Online Courses from MIT"! Presented in…

2 months ago

10 ChatGPT Prompts to create your CV

Welcome to "10 ChatGPT Prompts to Create Your CV." Crafting a compelling CV is essential…

2 months ago

StellarWP: Elevating Your WordPress Experience with Eight Exceptional Products

Welcome to our exploration of StellarWP, where we embark on a journey to elevate your…

2 months ago