Static variable must be declared in public section of the class - C++ basic concepts

Q.  Static variable must be declared in public section of the class.
- Published on 17 Jul 15

a. True
b. False

ANSWER: False
 

    Discussion

  • Digvijay   -Posted on 24 Oct 15
    You can declare static variable anywhere in the class but you have to initialize it outside the class.
    Example:
    class MyClass
    {
    static int staticVar;
    public:

    MyClass()
    {
    staticVar=10;
    cout << "staticVar =:" << staticVar << endl;
    }


    };

    int MyClass::staticVar;
    void main()
    {
    MyClass obj;

    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)