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

35. Why is overloading sometimes referred to as ad hoc polymorphism? 38. What ar

ID: 3666500 • Letter: 3

Question

35. Why is overloading sometimes referred to as ad hoc polymorphism?

38. What are first-class subroutines?What languages support them?

40. What is an object closure? How is it related to a subroutine closure?

42. Explain the distinction between limited and unlimited extent of objects in a

local scope.

43. What are macros? What was the motivation for including them in C? What

problems may they cause?

...........

28- Give an argument in favor of dynamic scoping. Describe how similar benefits

can be achieved in a language without dynamic scoping

32. Explain the differences among overloading, coercion, and polymorphism.

33. What is operator overloading ? Explain its relationship to “ordinary” overloading

in C++.

34. Define parametric and subtype polymorphism. Explain the distinction between

explicit and implicit parametric polymorphism.Which is also known as

genericity?

..............................................

Explanation / Answer

35)In programming languages, ad hoc polymorphismis a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. It is also known as function overloading or operator overloading. The term ad hoc in this context is not intended to be pejorative; it refers simply to the fact that this type of polymorphism is not a fundamental feature of the type system. This is in contrast to parametric polymorphism, in which polymorphic functions are written without mention of any specific type, and can thus apply a single abstract implementation to any number of types in a transparent way. This classification was introduced by Christopher Strachey in 1967.

38)

Second-class value just can be passed as an argument.

Third-class value even can't be passed as an argument.

40)In programming languages, closures (also lexical closures or function closures) are a technique for implementing lexically scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or storage location to which the name was bound when the closure was created. A closure—unlike a plain function—allows the function to access those captured variables through the closure's reference to them, even when the function is invoked outside their scope.

In computer programming, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.

42)

Scope and Extent

In early programming languages, identifiers and variables had essentially unlimited scope and extent. However, it soon became clear that it was important to find ways of restricting extent, to make the best use of limited computer memories, and scope, to make the best use of limited human memories.

Classical Block Structure

How can we safely reuse memory space for different purposes in different parts of a program?

Example C program showing nested block structure:

Scopes of identifiers:

Extents of variables (as accessed by identifiers):

43)

1) In Microsoft Word and other programs, a macro is a saved sequence of command s or keyboard strokes that can be stored and then recalled with a single command or keyboard stroke.

2) In computers, a macro (for "large"; the opposite of "micro") is any programming or user interface that, when used, expands into something larger. The original use for "macro" or "macro definition" was in computer assembler language before higher-level, easier-to-code languages became more common. In assembler language, a macro definition defines how to expand a single language statement or computer instruction into a number of instructions. The macro statement contains the name of the macro definition and usually some variable parameter information. Macros were (and are) useful especially when a sequence of instructions is used a number of times (and possibly by different programmers working on a project). Some pre-compilers also use the macro concept. In general, however, in higher-level languages, any language statement is about as easy to write as an assembler macro statement.

Assembler macros generate instruction s inline with the rest of a program. More elaborate sequences of instructions that are used frequently by more than one program or programmer are encoded in subroutines that can be branched to from or assembled into a program.

how macros related to c:

Macros with argument

Preprocessing directive #define can be used to write macro definitions with parameters as well in the form below:

Again, the token string is optional but, are used in almost every case. Let us consider an example of macro definition with argument.

Here, the argument passed is r. Every time the program encounters area(argument), it will be replace by (3.1415*(argument)*(argument)). Suppose, we passed (r1+5) as argument then, it expands as below:

28)

32)

The polymorphism is the base of the OOP, the overloading is one of ways to implement to polymorphism, specially when are involved operators. More generally, speaking about polymorphism when there are two or more classes involved. While the overloading can be made also inside the same class, we can overload the name of a method with several signatures (different list of parameters). While overriding is designed exclusively for involving two or more classes. Note that the overrided methods have all the same signature.

Coercion /korn/ is the practice of forcing another party to act in an involuntary manner by use of intimidation or threats or some other form of pressure or force. It involves a set of various types of forceful actions that violate the free will of an individual to induce a desired response, for example: a bully demanding lunch money to a student or the student gets beaten. These actions can include, but are not limited to, extortion, blackmail, torture, enhanced interrogation and threats to force the recipient to bend to the will of the interrogator. In law, coercion is codified as a duress crime. Such actions are used as leverage, to force the victim to act in a way contrary to their own interests. Coercion may involve the actual infliction of physical pain/injury or psychological harm in order to enhance the credibility of a threat. The threat of further harm may lead to the cooperation orobedience of the person being coerced.

33)

operator overloading:

C++ allows you to specify more than one definition for a function name or anoperator in the same scope, which is called function overloading andoperator overloading respectively.

An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).

When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

Function overloading in C++:

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.

Following is the example where same function print() is being used to print different data types:

When the above code is compiled and executed, it produces the following result:

Operators overloading in C++:

You can redefine or overload most of the built-in operators available in C++. Thus a programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. In case we define above function as non-member function of a class then we would have to pass two arguments for each operand as follows:

Following is the example to show the concept of operator over loading using a member function. Here an object is passed as an argument whose properties will be accessed using this object, the object which will call this operator can be accessed using this operator as explained below:

34)

Parametric polymorphism:

Parametric polymorphism occurs when a routine, type or class definition is parameterized by one or more types. It allows the actual parameter type to be selected by the user. This way, it is possible to define types or functions that are generics, which can be expressed by using type variables for the parameter type. The code below shows the use of atemplate, which is a way of implementing parametric polymorphism in C++.

Subtype Polymorphism:

In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which asubtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability, meaning that program elements, typicallysubroutines or functions, written to operate on elements of the supertype can also operate on elements of the subtype. If S is a subtype of T, the subtyping relation is often written S <: T, to mean that any term of type S can be safely used in a context where a term of type T is expected. The precise semantics of subtyping crucially depends on the particulars of what "safely used in a context where" means in a given programming language. The type system of a programming language essentially defines its own subtyping relation, which may well be trivial.

Implicit Polymorphism:

Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. For most purposes the default, polymorphism=”implicit”, is appropriate.

Explicit Polymorphism:

Explicit polymorphism means that class instances will be returned only by queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this <class> declaration as a <subclass> or <joined-subclass>.

Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a “lightweight” class that contains a subset of the table columns).

Generic programming means that you are not writing source code that is compiled as-is but that you write "templates" of source codes that the compiler in the process of compilation transforms into source codes. The simplest example for generic programming are container classes like arrays, lists or maps that contain a collection of other objects. But there's much more to generic programming. In the context of C++ (and called meta programming) it means to write programs that are evaluated atcompile time.

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