Answer to Question #259492 in C++ for zain

Question #259492

static member function



1
Expert's answer
2021-11-01T01:58:55-0400
#include <iostream>
 
using namespace std;
 
class MyClass{
    static int num;
        int n;
     
    public:
        void set_n(){
            n = ++num;
        }
     
    void display_n(){
         
        cout<<"n : "<<n<<endl;
    }
    
    //static member function 
    static void display_num(){
         
        cout<<"num : "<<num<<endl;
    }
     
     
};
 
int MyClass:: num;
 
int main()
{
    MyClass e1, e2;
    e1.set_n();
    e2.set_n();
    e1.display_n();
    e2.display_n();
    MyClass::display_num();
     
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS