Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Why do programmers use inheritance in C++?
easy
A. To create unrelated classes
B. To reuse code from an existing class
C. To make programs run faster
D. To avoid using functions

Solution

  1. Step 1: Understand inheritance purpose

    Inheritance allows a new class to use code from an existing class, avoiding repetition.
  2. Step 2: Analyze options

    Only To reuse code from an existing class correctly states code reuse. Others are unrelated or incorrect.
  3. Final Answer:

    To reuse code from an existing class -> Option B
  4. Quick Check:

    Inheritance = Code reuse [OK]
Hint: Inheritance means reusing code from another class [OK]
Common Mistakes:
  • Thinking inheritance speeds up program execution
  • Confusing inheritance with unrelated class creation
  • Believing inheritance removes need for functions
2. Which of the following is the correct syntax to declare a class Car that inherits from class Vehicle in C++?
easy
A. class Car Vehicle {};
B. class Car inherits Vehicle {};
C. class Car extends Vehicle {};
D. class Car : public Vehicle {};

Solution

  1. Step 1: Recall C++ inheritance syntax

    In C++, inheritance is declared using a colon and access specifier like public.
  2. Step 2: Check each option

    class Car : public Vehicle {}; uses correct syntax. inherits and extends use keywords from other languages. class Car Vehicle {}; is invalid.
  3. Final Answer:

    class Car : public Vehicle {}; -> Option D
  4. Quick Check:

    C++ inheritance syntax = colon + access specifier [OK]
Hint: Use colon and public keyword for inheritance in C++ [OK]
Common Mistakes:
  • Using 'inherits' or 'extends' keywords (not C++ syntax)
  • Omitting colon before base class name
  • Forgetting access specifier like public
3. Consider this code:
class Animal {
public:
  void sound() { std::cout << "Animal sound"; }
};
class Dog : public Animal {
public:
  void sound() { std::cout << "Bark"; }
};

int main() {
  Dog d;
  d.sound();
  return 0;
}

What is the output when this program runs?
medium
A. Bark
B. Animal sound
C. Compilation error
D. No output

Solution

  1. Step 1: Understand method overriding in inheritance

    The Dog class defines its own sound() method, which overrides Animal's method.
  2. Step 2: Check which method is called

    Calling d.sound() uses Dog's version, printing "Bark".
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Overridden method runs = Bark [OK]
Hint: Child class method overrides parent method when called [OK]
Common Mistakes:
  • Assuming parent method runs instead of child
  • Thinking code causes compilation error
  • Expecting no output from method call
4. Find the error in this code snippet:
class Base {
public:
  void show() { std::cout << "Base"; }
};
class Derived : Base {
public:
  void show() { std::cout << "Derived"; }
};

int main() {
  Derived d;
  d.show();
  return 0;
}
medium
A. Derived class cannot have a function named show()
B. Function show() must be virtual
C. No error, code runs fine
D. Missing access specifier before Base in inheritance

Solution

  1. Step 1: Verify inheritance syntax

    class Derived : Base is valid syntax; class inheritance defaults to private.
  2. Step 2: Trace program execution

    Derived d; d.show(); calls Derived's public show(), printing "Derived". No errors.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    Private inheritance defaults ok here [OK]
Hint: Classes inherit privately by default in C++ [OK]
Common Mistakes:
  • Assuming default inheritance is public
  • Thinking virtual keyword is mandatory here
  • Believing derived class cannot have same function name
5. You want to model a system where Car and Bike share common features like start() and stop(), but also have their own unique features. How should you use inheritance in C++ to design this?
hard
A. Create a base class Vehicle with common features, then inherit Car and Bike from it
B. Make Car inherit from Bike to reuse code
C. Write all features separately in Car and Bike without inheritance
D. Use inheritance only if Car and Bike have identical features

Solution

  1. Step 1: Identify common features

    Both Car and Bike share start() and stop(), so these belong in a common base class.
  2. Step 2: Design inheritance structure

    Create a base class Vehicle with common features, then inherit Car and Bike from it to add unique features.
  3. Final Answer:

    Create a base class Vehicle with common features, then inherit Car and Bike from it -> Option A
  4. Quick Check:

    Use base class for shared features [OK]
Hint: Put shared features in base class, inherit for unique parts [OK]
Common Mistakes:
  • Inheriting Car from Bike (wrong 'is-a' relationship)
  • Duplicating code instead of reusing via inheritance
  • Thinking inheritance requires identical features
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard