Challenge - 5 Problems
Access Specifiers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of accessing private member in derived class
What is the output of this C++ code?
C++
#include <iostream> using namespace std; class Base { private: int x = 10; public: int getX() { return x; } }; class Derived : public Base { public: void printX() { // cout << x << endl; // line A cout << getX() << endl; } }; int main() { Derived d; d.printX(); return 0; }
Attempts:
2 left
π‘ Hint
Private members are not accessible directly in derived classes, but public methods can access them.
β Incorrect
The private member 'x' is not accessible directly in Derived, so line A is commented out. Instead, the public method getX() returns the value 10, which is printed.
β Predict Output
intermediate2:00remaining
Output of protected member access in derived class
What will this program print?
C++
#include <iostream> using namespace std; class Parent { protected: int val = 5; }; class Child : public Parent { public: void show() { cout << val << endl; } }; int main() { Child c; c.show(); return 0; }
Attempts:
2 left
π‘ Hint
Protected members are accessible in derived classes.
β Incorrect
The protected member 'val' is accessible in the derived class Child, so printing val outputs 5.
β Predict Output
advanced2:00remaining
Accessing private member from outside class
What error does this code produce?
C++
#include <iostream> using namespace std; class Sample { private: int secret = 42; }; int main() { Sample s; cout << s.secret << endl; return 0; }
Attempts:
2 left
π‘ Hint
Private members cannot be accessed outside their class.
β Incorrect
The member 'secret' is private and cannot be accessed directly from main, causing a compilation error.
β Predict Output
advanced2:00remaining
Output of mixed access specifiers in inheritance
What is the output of this program?
C++
#include <iostream> using namespace std; class A { public: int x = 1; protected: int y = 2; private: int z = 3; }; class B : public A { public: void print() { cout << x << ' ' << y << ' '; // cout << z << ' '; // line X } }; int main() { B b; b.print(); return 0; }
Attempts:
2 left
π‘ Hint
Private members are not accessible in derived classes.
β Incorrect
The public member x and protected member y are accessible in B, but private member z is not. The commented line X would cause a compilation error if uncommented.
π§ Conceptual
expert2:00remaining
Number of accessible members in derived class
Given the class below, how many members are accessible directly in class Derived?
C++
class Base { public: int a; protected: int b; private: int c; }; class Derived : public Base { void func() { // Which members can be accessed here? } };
Attempts:
2 left
π‘ Hint
Private members are not accessible in derived classes.
β Incorrect
In public inheritance, public and protected members of Base are accessible in Derived. Private members are not accessible.