Consider an unknown language with integer and real types in which 1+2, 1.0+2, 1+2.0, and 1.0+2.0 are all legal expressions.

a. Explane how ths could be the result of coercion, using no overloading.
b. Explane how this could be the result of overloading, using no coercion.
c. Explane how this could result from a combination of overloading and coercion.
d. Explane how this could result from subtype polymorphism, with no overloading or coercion.

Respuesta :

Answer:

Answer explained below

Explanation:

Operator Overloading:

Mostly operator is overloaded except direct member, scope resolution, and negation operator.

In the Operator overloading the language system check out the operator type and decide which type of definition use.

Coercion:

A coercion is an implicit type conversion, which conversion automatically.

a.

1 + 2

If they are defined like:

int x, y, z;

z = x +y;

z = 3.

Hence, it should be result of coercion using no overloading because here we didn’t use explicit type conversion.

b.

Consider 1.0 and 2

So, 1.0 is floating point and 2 is integer.

Thus, for converting integer to float and then in integer is called explicit conversion.

So, using no coercion, the resulting of overloading should be 1.0 + 2.0

c.

Here 2.0 is float so it implicit into with 1 integer

Then 1.0 + 2.0 is convert implicit type conversion, so it follows both operator overloading as well as coercion.

d.

and 2.0 are converting same type so it follows the adhoc sub type of polymorphism.

There is no need of overloading and coercion.

ACCESS MORE
EDU ACCESS