主页
C++
C++ 访问修饰符
demo_access_private
运行 ❯
×
更改方向
保存代码
更改主题
#include
using namespace std; class MyClass { public: // Public access specifier int x; // Public attribute private: // Private access specifier int y; // Private attribute }; int main() { MyClass myObj; myObj.x = 25; // Allowed (x is public) myObj.y = 50; // Not allowed (y is private) return 0; }
In function 'int main()':
Line 8: error: 'int MyClass::y' is private
Line 14: error: within this context