Search⌘ K

Challenge: Implement Inheritance Hierarchy

Explore implementing an inheritance hierarchy in D by overriding the abstract makeSound() method in subclasses representing railway vehicles. Learn how to create specialized subclasses that produce custom behaviors, improve your understanding of abstract classes, and prepare to handle subclass-specific functionality in D.

We'll cover the following...

Problem statement

Let’s modify RailwayVehicle. In addition to reporting the distance that it advances, let’s have it also make sounds. To keep the output short, let’s print the sounds per 100 kilometers:

class RailwayVehicle {
    void advance(size_t kilometers) {
        writefln("The vehicle is advancing %s kilometers", kilometers);

        foreach (i; 0 .. kilometers /
...