0
0
C++programming~20 mins

Access control in inheritance in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Access Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of access control with public inheritance
What is the output of this C++ code snippet?
C++
#include <iostream>
class Base {
public:
    int x = 10;
protected:
    int y = 20;
private:
    int z = 30;
};
class Derived : public Base {
public:
    void print() {
        std::cout << x << ' ' << y << '\n';
        // std::cout << z << '\n'; // z is private in Base
    }
};
int main() {
    Derived d;
    d.print();
    std::cout << d.x << '\n';
    // std::cout << d.y << '\n'; // y is protected
    return 0;
}
A10 20\n20\n
B10 20\n10\n
C10 30\n10\n
DCompilation error due to access of y
Attempts:
2 left
πŸ’‘ Hint
Remember that public inheritance keeps public members public and protected members protected in the derived class.
❓ Predict Output
intermediate
2:00remaining
Access control with protected inheritance
What will be the output or error of this C++ code?
C++
#include <iostream>
class Base {
public:
    int a = 5;
protected:
    int b = 10;
};
class Derived : protected Base {
public:
    void show() {
        std::cout << a << ' ' << b << '\n';
    }
};
int main() {
    Derived d;
    d.show();
    // std::cout << d.a << '\n'; // Is this allowed?
    return 0;
}
A5 10\nCompilation error on accessing d.a
B5 10\n5\n
CCompilation error inside show() due to access of a
DCompilation error inside show() due to access of b
Attempts:
2 left
πŸ’‘ Hint
Protected inheritance changes the access level of base class public members to protected in the derived class.
❓ Predict Output
advanced
2:00remaining
Private inheritance and member accessibility
What will happen when compiling and running this code?
C++
#include <iostream>
class Base {
public:
    int val = 100;
};
class Derived : private Base {
public:
    void print() {
        std::cout << val << '\n';
    }
};
int main() {
    Derived d;
    d.print();
    // std::cout << d.val << '\n'; // Is this allowed?
    return 0;
}
ACompilation error: val is private in Base
BCompilation error inside print() due to access of val
C100\n100\n
D100\nCompilation error on accessing d.val
Attempts:
2 left
πŸ’‘ Hint
Private inheritance makes all base class public and protected members private in the derived class.
🧠 Conceptual
advanced
1:30remaining
Effect of inheritance type on base class member accessibility
Given a base class with public, protected, and private members, which inheritance type will make the base class public members inaccessible from outside the derived class?
APrivate inheritance
BProtected inheritance
CPublic inheritance
DVirtual inheritance
Attempts:
2 left
πŸ’‘ Hint
Think about which inheritance type changes public members to private in the derived class.
πŸ”§ Debug
expert
2:30remaining
Why does this code fail to compile?
This code fails to compile. What is the cause of the error?
C++
#include <iostream>
class Base {
protected:
    int data = 42;
};
class Derived : public Base {
public:
    void show() {
        std::cout << data << '\n';
    }
};
int main() {
    Derived d;
    d.show();
    std::cout << d.data << '\n';
    return 0;
}
ANo error, code compiles and prints 42 twice
BError because 'data' is private in Base
CError because 'data' is protected and cannot be accessed outside the class or derived classes
DError because 'show' is not declared const
Attempts:
2 left
πŸ’‘ Hint
Check the access level of 'data' and where it is accessed.