Menu
CoddyTech

Inheritance and Overrides

Lesson 37 of 48 in Coddy's Swift for C++ Developers course.

C++ requires a virtual base member for dynamic dispatch through a base pointer or reference; override checks that the derived declaration really overrides it. Swift class methods can generally be overridden within the allowed boundary unless final, and the subclass writes override. Cross-module Swift subclassing also involves open access. C++ permits multiple class inheritance, while Swift has a single class superclass and uses protocols for additional capabilities. Avoid slicing a C++ derived value into a base object when preserving dynamic behavior matters. Swift structs do not enter the class inheritance chain. The example observes one specialized renderer label.

C++

Complete program file: main.cpp.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <optional>
#include <memory>
#include <tuple>
#include <variant>
#include <stdexcept>
#include <algorithm>
struct Renderer { virtual std::string style() const { return "full"; } virtual ~Renderer() = default; };
struct CompactRenderer final : Renderer { std::string style() const override { return "compact"; } };

int main() {
CompactRenderer compact;
    const Renderer &renderer = compact;
    std::cout << renderer.style() << '\n';
}

Swift

Complete program file: Renderer.swift.

class Renderer { func style() -> String { "full" } }

Complete program file: CompactRenderer.swift.

final class CompactRenderer: Renderer { override func style() -> String { "compact" } }

Complete program file: main.swift.

let renderer: Renderer = CompactRenderer()
print(renderer.style())
challenge icon

Challenge

Easy

Implement class Rate with init() and fee(units: Int) -> Int returning twice units. Implement subclass RushRate, overriding fee to add 5 to the base fee. Input is base or rush then units 0 through 100. Select an instance through a Rate reference and output its fee.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Rate.swift, RushRate.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
let r: Rate = RushRate(); return r.fee(units: 7) == 19 && Rate().fee(units: 7) == 14 && r.fee(units: 0) == 5
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for C++ Developers

Practice on your own: Swift playground