Complete the code to make the member variable accessible only within the class.
class MyClass { private: int [1]; };
The member variable name can be any valid identifier. Here, value is used as the variable name.
Complete the code to declare a public member function that returns the private variable.
class MyClass { private: int value; public: int [1]() { return value; } };
The function name getValue clearly indicates it returns the value of the private variable.
Fix the error in the code by choosing the correct access specifier to allow derived classes to access the base class member.
class Base { [1]: int protectedVar; }; class Derived : public Base { public: void show() { std::cout << protectedVar << std::endl; } };
The protected access specifier allows derived classes to access the member variable.
Fill both blanks to declare a class with a private variable and a public setter function.
class Sample { [1]: int data; [2]: void setData(int d) { data = d; } };
The variable data should be private to restrict direct access, and the setter function should be public to allow controlled modification.
Fill all three blanks to create a class with a private variable, a public getter, and a protected setter.
class Data { [1]: int value; [2]: int getValue() { return value; } [3]: void setValue(int v) { value = v; } };
The variable value is private to protect it. The getter is public to allow reading the value. The setter is protected to allow modification only within the class and derived classes.