example3_class_scope.cpp

 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
#include <iostream>

class MyClass {
private:
    int privateVar;

public:
    MyClass() : privateVar(0) {}
    
    void setVar(int value) {
        privateVar = value;
    }
    
    void printVar() {
        std::cout << "privateVar = " << privateVar << std::endl;
    }
};

int main() {
    MyClass obj;
    obj.setVar(42);
    obj.printVar();
    
    // std::cout << obj.privateVar << std::endl;  // This would cause a compilation error
    
    return 0;
}
Back to context