मराठी
महाराष्ट्र राज्य शिक्षण मंडळएचएससी विज्ञान (संगणक विज्ञान) इयत्ता १२ वी

Explain use of scope resolution operator with suitable example. - Computer Science 1

Advertisements
Advertisements

प्रश्न

Explain use of scope resolution operator with suitable example.

स्पष्ट करा
Advertisements

उत्तर

The scope resolution operator: qualifies an identifier with the scope (class, namespace, or global) it belongs to. Common uses:

  • Define a class member function outside the class (ClassName::member).
  • Access a global variable that is hidden by a local variable (:: name).
  • Select a specific base-class member when multiple bases define the same name (Base::member).
  • Refer to namespace members (e.g., std::cout) and to static members (Class::staticMember).

Examples: 

Defining a member function outside the class Code: 

#include <iostream>
class MyClass {
public:
    int x;
    void set(int v);   // Declaration
    void print();      // Declaration
};
// Using scope resolution to define members outside the class
void MyClass::set(int v) { 
    x = v; 
}
void MyClass::print() { 
    std::cout << x << std::endl; 
}
int main() {
    MyClass m;
    m.set(42);
    m.print(); // Prints 42
    return 0;
}
shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2025-2026 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×