সব বই

পেশাদার

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ক্যারিয়ার অনলাইন
অ্যাপ পরিচিতি Coach সাইন ইন পড়া শুরু করো

বইগুলো এখনো বাংলা ভাষায় পাওয়া যায় না — ইংরেজি সংস্করণ দেখানো হচ্ছে।

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.

প্রসঙ্গসহ পড়ো →