static member, static member function , static data member, member function in class
Write a program in C++ to implement static member function in class.

Algorithm to solve the above problem:
1. Create class count with static data member as total.
2. Create a member function to increment the count.
3. Declare the static data member using scope resolution operator.
4. Display the count and total value.


Implementation of the above problem in C++:
#include<iostream>
using namespace std;
class count
{
private:
int number;
static int total;
public:
count();
int get_number();
static int get_total();
};
count::count()
{
number=100+ total++;
}
int count::get_number()
{
return number;
}
int count::get_total()
{
return total;
}
int count::total=0;
int main ()
{
cout<<" Total count = "<<count::get_total() <<endl;
count a, b, c;
cout<<" a="<<a.get_number() << endl;
cout<<" b="<<b.get_number() << endl;
cout<<" b="<<c.get_number() << endl;
cout<<" Total count="<<count::get_total() << endl;
return 0;
}


The output of the above program is tested on www.jdoodle.com
Output:
Total count = 0
a=100
b=101
c=102
Total count = 3


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: