0
0
C++programming~20 mins

Why inheritance is used in C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Inheritance in C++

Why do programmers use inheritance in C++?

ATo reuse code from existing classes and create a hierarchy of related classes
BTo make programs run faster by avoiding function calls
CTo store data in a file for later use
DTo prevent any changes to the base class
Attempts:
2 left
πŸ’‘ Hint

Think about how you can build new things based on old things without rewriting everything.

❓ Predict Output
intermediate
2:00remaining
Output of Inheritance Example

What is the output of this C++ code using inheritance?

C++
#include <iostream>
class Animal {
public:
    void speak() { std::cout << "Animal speaks" << std::endl; }
};

class Dog : public Animal {
public:
    void speak() { std::cout << "Dog barks" << std::endl; }
};

int main() {
    Dog d;
    d.speak();
    return 0;
}
AAnimal speaks
BCompilation error due to speak() redefinition
CNo output
DDog barks
Attempts:
2 left
πŸ’‘ Hint

Which speak() method is called when using a Dog object?

πŸ”§ Debug
advanced
2:00remaining
Identify the Inheritance Error

What error does this C++ code produce?

C++
#include <iostream>
class Base {
public:
    void show() { std::cout << "Base" << std::endl; }
};

class Derived : Base {
};

int main() {
    Derived d;
    d.show();
    return 0;
}
ACompilation error: 'show' is inaccessible due to private inheritance
BRuntime error: segmentation fault
CNo error, output is 'Derived'
DCompilation error: missing semicolon after class definition
Attempts:
2 left
πŸ’‘ Hint

Check the default inheritance access level in C++ classes.

πŸ“ Syntax
advanced
1:30remaining
Correct Syntax for Public Inheritance

Which option shows the correct syntax for public inheritance in C++?

Aclass Derived extends Base {};
Bclass Derived inherits Base {};
Cclass Derived : public Base {};
Dclass Derived < Base {};
Attempts:
2 left
πŸ’‘ Hint

Remember the keyword used in C++ for inheritance.

πŸš€ Application
expert
2:30remaining
Number of Accessible Members in Derived Class

Given the following code, how many members are accessible directly from an object of class Derived?

class Base {
public:
    int a;
protected:
    int b;
private:
    int c;
};

class Derived : public Base {
public:
    void func() {
        a = 1;
        b = 2;
        // c = 3; // Is this accessible?
    }
};

int main() {
    Derived d;
    d.a = 5;
    // d.b = 6; // Is this accessible?
    // d.c = 7; // Is this accessible?
    return 0;
}
ANo members are accessible directly from Derived objects
BOnly 1 member (a) is accessible directly from Derived objects
CAll 3 members (a, b, and c) are accessible directly from Derived objects
D2 members (a and b) are accessible directly from Derived objects
Attempts:
2 left
πŸ’‘ Hint

Consider the access levels and where they apply.