0
0
C++programming~10 mins

Access control in inheritance in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class B that inherits publicly from class A.

C++
class B : [1] A {};
Drag options to blanks, or click blank then click option'
Aprotected
Bprivate
Cpublic
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private or protected inheritance changes access levels unexpectedly.
2fill in blank
medium

Complete the code to make class B inherit privately from class A.

C++
class B : [1] A {};
Drag options to blanks, or click blank then click option'
Aprivate
Bpublic
Cprotected
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Confusing private inheritance with private members.
3fill in blank
hard

Fix the error in the code to allow class B to access protected members of class A.

C++
class A {
protected:
    int x;
};

class B : [1] A {
public:
    void setX(int val) { A::x = val; }
};
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Cprotected
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private inheritance blocks access to protected members.
4fill in blank
hard

Fill both blanks to declare class B that inherits protectedly from class A and class C that inherits publicly from B.

C++
class B : [1] A {};
class C : [2] B {};
Drag options to blanks, or click blank then click option'
Aprotected
Bprivate
Cpublic
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Mixing up protected and public inheritance keywords.
5fill in blank
hard

Fill all three blanks to create a class D that inherits publicly from B and privately from C, where B inherits publicly from A and C inherits protectedly from A.

C++
class B : [1] A {};
class C : [2] A {};
class D : [3] B, private C {};
Drag options to blanks, or click blank then click option'
Apublic
Bprotected
Cprivate
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Confusing inheritance access specifiers and their effects on member visibility.