-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope-resolution.cpp
More file actions
55 lines (39 loc) · 1.17 KB
/
scope-resolution.cpp
File metadata and controls
55 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
int x = 10; // Global variable
class MyClass
{
public:
void display(); // Declaration inside the class
static int count; // Static member declaration
static void displayCount()
{
std::cout << "Count: " << count << std::endl;
}
};
// Definition outside the class using scope resolution
void MyClass::display()
{
std::cout << "Hello, World!" << std::endl;
}
int MyClass::count = 5; // Definition of static member outside the class
namespace MyNamespace
{
int value = 42;
}
int main()
{
// 1) Accessing Global Variables:
int x = 20; // Local variable
std::cout << "Local x: " << x << std::endl;
std::cout << "Global x: " << ::x << std::endl; // Accessing global variable
// Local x: 20
// Global x: 10
// 2) Defining Class Member Functions Outside the Class:
MyClass obj;
obj.display(); // Hello, World!
// 3) Accessing Static Members:
MyClass::displayCount(); // Accessing static member using scope resolution // Count: 5
// 4) Accessing Namespaces:
std::cout << "Value: " << MyNamespace::value << std::endl; // Accessing value inside namespace // Value: 42
return 0;
}