Lab P9 β Use classes with static member functions.
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=2Explanation
- count is static shared.
- showCount() is static; called via class.
Viva / Important Questions
Short Questions
- What is static member function?
- Can it access non-static members?
Long Questions
- Explain the program in detail.
- 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.