Lab P9 β€” Use classes with static member functions.

NBC351 Β· Program 9⏱ 8 min read❓ 5 MCQs

Aim

Aim: Demonstrate static member functions.

Theory

Demonstrate static member functions.

C++ Program

#include 
using namespace std;
class Counter {
    static int count;
public:
    static void showCount(){ cout<<"Count="<

Expected Output

Count=2

Explanation

  • count is static shared.
  • showCount() is static; called via class.

Viva / Important Questions

Short Questions

  1. What is static member function?
  2. Can it access non-static members?

Long Questions

  1. Explain the program in detail.
  2. Discuss key concepts demonstrated.

MCQ Practice

1. Static member function belongs to:

  • Class
  • Object
  • Instance
  • None
Answer: Class.

2. Called using:

  • ClassName::fn()
  • object.fn()
  • Both
  • None
Answer: Both.

3. Static fn can access:

  • Static members only
  • All members
  • Nothing
  • Only const
Answer: Static members only.

4. Has this pointer?

  • No
  • Yes
  • Sometimes
  • Always
Answer: No.

5. Static data shared among:

  • All objects
  • One object
  • None
  • Functions
Answer: All objects.

Quick Revision

  • Static fn: no this; only static data.
  • Called via class name.