Overriding Class Variables in Inheritance

Medium Multiple & Virtual Inheritance
Solve in Playground →

Problem Statement

Create a `Base` class with a public string `name = “Base”`. Create a `Derived` class with its own public string `name = “Derived”`. Instantiate `Derived`, and print the `Derived`’s name, and then print the `Base`’s name using scope resolution (`obj.Base::name`).

Input Format

None

Output Format

Print the names on separate lines.

Constraints

None

Sample Output

Derived
Base

Explanation

If a derived class defines a variable with the exact same name as a base class variable, it shadows the base variable. Both still exist in memory.

Starter Code

#include <iostream>
#include <string>

class Base {
public:
    std::string name = "Base";
};

class Derived : public Base {
public:
    // Shadow the name variable
};

int main() {
    // Instantiate and print both names
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used