Static function and static variable in cpp

Static variable and Static method in cpp

PROGRAM:

#include<iostream>
using namespace std;
class test{
    int code;
    static int Count;
public:
    void set_code(void){code=++Count;}
    void show_code(void){
    cout<<"Object number : "<<code<<endl;
    }
    static void show_Count(void){
    cout<<"Count : "<<Count<<endl;
    }
};

int test:: Count;

int main(){
test t1,t2; //!when 1st obj. created static Count=0
t1.set_code();//!t1=1,count=1
test::show_Count();//!because Static function
t2.set_code();  //!t2=2,count=2
test::show_Count();//!count=2
test t3;//!count=2
t3.set_code();//!t3=3,count=3
test::show_Count();//!count=3

t1.show_code();
t2.show_code();
t3.show_code();

return 0;
}

Comments

Popular posts from this blog

Introduction to ChatGPT: Explain what ChatGPT is, how it works, and what it is used for.

Multi Level Inheritance In cpp

Hybrid Inheritance In cpp