4. Simple Inheritance

Your task is to define two classes:

  • Base class: Device
    • A method void printType() that prints:
       

Generic Device

  • Derived class: Sensor (inherits publicly from Device)
    • A method void printSensor() that prints:
      Sensor Device

The program will:

  1. Create a Sensor object.
  2. Call printType() (inherited from base).
  3. Call printSensor() (defined in derived).

     

Example

Output:

Generic Device
Sensor Device

Why this output?

  • The base class method printType() is accessible because of public inheritance.
  • The derived class method printSensor() prints its own message.
     

Question Significance

This problem demonstrates how a derived class can reuse base class functionality while adding its own behavior.

Loading...

Input

Expected Output

Generic Device Sensor Device