Sunday, April 4, 2021

static data member (OOP) (10)

 #include<iostream>

using namespace std;

class test // making the class name test

{

private: //private part that mostly consist of data 

int no;

static int count;  /*Uwe use the static keyword with the data member when we want make the static 

data member. when we make the more then one object of a clas then the value will be same on all

object of the class.*/

public: //public part that mostly consist of 

void getval(int); //function declearation

void dispcount();//function declearation

};

void test :: getval(int x) //defination of the class function

{

no=x;

cout<<"the number is:"<<no<<endl;

count++;

}

void test :: dispcount() //defination of the class function

{

cout<<"the counter is:"<<count<<endl;

}

int test :: count; //this is the method of defining the static data member outside the class

int main()

{

test t1,t2,t3; //making the three object of the single class

t1.dispcount(); //the static data member "counter" value same 

t2.dispcount(); //before calling the getval function t1,t2 and t3

t3.dispcount(); //t1=0,t2=0 and t3=0

t1.getval( 100);

t2.getval( 200);

t3.getval( 300);

t1.dispcount();  // the static data member "counter" value same after calling the getval function

t2.dispcount(); // t1=3,t2=3 and t3=3

t3.dispcount();

}

No comments:

Post a Comment