جميع الكتب

مهني

1 Markets I: The Ecosystem and Exchange-Traded Marketsالأسواق عبر الإنترنت 2 Markets II: Rates, FX and Creditالأسواق عبر الإنترنت 3 Markets III: Commodities, Energy and Cryptoالأسواق عبر الإنترنت 4 Quantitative Methodsالأساليب عبر الإنترنت 5 Derivatives and Volatilityالمشتقات عبر الإنترنت 6 Rates, Credit, XVA and Riskالفائدة والائتمان والمخاطر عبر الإنترنت 7 Research Craft: Predictors, Backtests, Measurement, Portfoliosالبحث عبر الإنترنت 8 Strategies I: Equities and Futuresالاستراتيجيات عبر الإنترنت 9 Strategies II: Volatility, Relative Value, Macro and the Bank Desksالاستراتيجيات عبر الإنترنت 10 Microstructure and Executionالتنفيذ عبر الإنترنت 11 Market Making and High-Frequency Tradingصناعة السوق عبر الإنترنت 12 Machine Learning for Marketsتعلم الآلة عبر الإنترنت 13 Low-Latency Softwareالتكنولوجيا عبر الإنترنت 14 Networks, Hardware and Trading Infrastructureالتكنولوجيا عبر الإنترنت 15 Research, Data and Risk Platformsالتكنولوجيا عبر الإنترنت 16 The Desk and the Firmالشركة عبر الإنترنت 17 The Industry: Firms, Roles and Careersالمسارات المهنية عبر الإنترنت 18 Interviews I: Maths, Probability and Speedالمسارات المهنية عبر الإنترنت 19 Interviews II: Markets, Trading Games and the Final Roundالمسارات المهنية عبر الإنترنت 20 Interviews III: Codeالمسارات المهنية عبر الإنترنت
التطبيقات حول المدرب تسجيل الدخول ابدأ القراءة

Computer Science · المسرد

ما معنى Substitution principle؟

Definition 3.9 University Computer Science — Year 2 · الفصل 3 — Inheritance and Polymorphism

A derived class D of B satisfies the substitution principle if every property that a client can prove about objects of B from the specification of B (the preconditions and postconditions of its functions, and its invariant) also holds for objects of D. Equivalently, a correct client of B stays correct when given a D. The principle bears the name of Barbara Liskov, who stated it in a keynote in 1987 and, with Jeannette Wing, as a definition of subtyping in 1994.

The design of the fines. The code that sums fines holds a policy (composition) and knows only the abstract class; the three policies derive from it (inheritance). A fourth policy, for maps, is one more class in one more file, plus one line in policy_for.
The design of the fines. The code that sums fines holds a policy (composition) and knows only the abstract class; the three policies derive from it (inheritance). A fourth policy, for maps, is one more class in one more file, plus one line in policy_for.

أمثلة

Example 3.10 (A square that is not a rectangle)

class Rectangle {
public:
    Rectangle(double w, double h) : w_(w), h_(h) {}
    virtual ~Rectangle() = default;
    virtual void set_width(double w) { w_ = w; }    // postcondition: width w, height unchanged
    virtual void set_height(double h) { h_ = h; }   // postcondition: height h, width unchanged
    double area() const { return w_ * h_; }

protected:
    double w_, h_;
};

class Square : public Rectangle {                    // keeps its sides equal
public:
    explicit Square(double s) : Rectangle(s, s) {}
    void set_width(double w) override { w_ = h_ = w; }
    void set_height(double h) override { w_ = h_ = h; }
};

// A client written against Rectangle: resize a plot to 4 m by 5 m.
bool resize_plot(Rectangle &r)
{
    r.set_width(4);
    r.set_height(5);
    return r.area() == 20;                           // what the postconditions promise
}

int main()
{
    Rectangle r(2, 3);
    Square s(3);
    bool ok_r = resize_plot(r), ok_s = resize_plot(s);
    std::printf("rectangle: %s (area %g)\n", ok_r ? "as promised" : "broken", r.area());
    std::printf("square:    %s (area %g)\n", ok_s ? "as promised" : "broken", s.area());
    return 0;
}
Listing 3.9. A client of Rectangle given a Square code/bachelor-2/03-cpp-polymorphism/listings/square.cpp
$ g++ -std=c++20 -O2 -Wall -Wextra -o square square.cpp && ./square
rectangle: as promised (area 20)
square:    broken (area 25)
Output 3.8. The client’s promise holds for one of the two.

resize_plot relies on the postconditions of Rectangle: after set_width(4) and set_height(5) the area is 20. Square must keep its sides equal, so its set_height(5) also changes the width, and the client sees 25. The square of geometry is a rectangle because a geometric rectangle never changes; a mutable Square cannot keep the promises of a mutable Rectangle, whichever way it overrides the setters (Exercise 3.8). Either the two classes have no setters, or Square is not derived from Rectangle.

اقرأ في الفصل →