Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ short questions. if you can provide an exmaple to understand its better The

ID: 662902 • Letter: C

Question

C++ short questions. if you can provide an exmaple to understand its better

The key aspects of object oriented programming (OOP) that makes it easier to develop extensible systems are dynamic binding and polymorphism. (a) Explain how these two aspects of OOP are used to make an OO design more flexible. (b) What must a designer do, when inheriting from a base class, to ensure that this flexibility is maintained? (c) Describe what features of C++ must be used to make use of dynamic binding and polymorphism.

Explanation / Answer

a)

Inheritance is not just a module combination and enrichment mechanism. It also enables the definition of flexible entities that may become attached to objects of various forms at run time, a property known as polymorphism.

Assume for example a class POLYGON , describing polygons, whose features include an array of points representing the vertices and a function perimeter which computes a polygon's perimeter by summing the successive distances between adjacent vertices. An heir of POLYGON may begin as:

class RECTANGLE inherit

    POLYGON redefine perimeter end

feature -- Specific features of rectangles, such as:

    side1: REAL; side2: REAL

    perimeter: REAL is

            -- Rectangle-specific version

        do Result := 2 * (side1 + side2) end

    ... Other RECTANGLE features ...

Here it is appropriate to redefine perimeter for rectangles as there is a simpler and more efficient algorithm. Note the explicit redefine subclause (which would come after the rename if present).

Other descendants of POLYGON may also have their own redefinitions of perimeter . The version to use in any call is determined by the run-time form of the target. Consider the following class fragment:

        p: POLYGON; r: RECTANGLE

        ... create p; create r; ...

        if c then

            p := r

        end

        print (p . perimeter)

The polymorphic assignment p := r is valid because of the above rule. If condition c is false, p will be attached to an object of type POLYGON for the computation of p . perimeter , which will thus use the polygon algorithm. In the opposite case, however, p will be attached to a rectangle; then the computation will use the version redefined for RECTANGLE . This is known as dynamic binding .

Dynamic binding provides a high degree of flexibility. The advantage for clients is the ability to request an operation (such as perimeter computation) without explicitly selecting one of its variants; the choice only occurs at run-time. This is essential in large systems, where many variants may be available; dynamic binding protects each component against changes in other components.

This technique is particularly attractive when compared to its closest equivalent in traditional approaches, where you would need records with variant components, or union types (C), together with case(switch) instructions to discriminate between variants. This means that every client must know about every possible case, and that any extension may invalidate a large body of existing software.

The combination of inheritance, feature redefinition, polymorphism and dynamic binding supports a development mode in which every module is open and incremental. When you want to reuse an existing class but need to adapt it to a new context, you can define a new descendant of that class (with new features, redefined ones, or both) without any change to the original. This facility is of great importance in software development, an activity that -- by design or circumstance -- is invariably incremental.

b) To maintain flexibility of design ,desginer should inherit only from abstract classes, since they usually provide little or no implementation.

c)Following features should be used to make use of dynamic binding and polymorphism:

i) Virtual Function:

A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.

What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.

ii) Pure Virtual functions:

It's possible that you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.

class RECTANGLE inherit

    POLYGON redefine perimeter end

feature -- Specific features of rectangles, such as:

    side1: REAL; side2: REAL

    perimeter: REAL is

            -- Rectangle-specific version

        do Result := 2 * (side1 + side2) end

    ... Other RECTANGLE features ...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote