Knowee
Questions
Features
Study Tools

Suppose that the following function declarations (in C++ syntax) are available in a program:(1) int pow(int, int);(2) double pow(double,double);(3) double pow(int, double);and suppose that the following code calls the pow function:int x;double y;x = pow(2,3);y = pow(2,3);x = pow(2,3.2);y = pow(2,3.2);x = pow(2.1,3);y = pow(2.1,3);x = pow(2.1,3.2);y = pow(2.1,3.2);Given the language (a) C++; (b) Java; or (c) Ada, write down the number of the pow function called ineach of the eight calls, or write “illegal” if a call cannot be resolved in the language chosen, or if a datatype conversion cannot be made.7.18 Pointers present some special problems for overload resolution. Consider the following C++ code:void f( int* x) { ... }void f( char* x) { ... }int main(){ ...f(0);...}What is wrong with this code? How might you fix it? Can this problem occur in Java? In Ada?Exercises 319(continued)C7729_ch07.indd 319C7729_ch07.indd 319 03/01/11 10:07 AM03/01/11 10:07 AM

Question

Suppose that the following function declarations (in C++ syntax) are available in a program:(1) int pow(int, int);(2) double pow(double,double);(3) double pow(int, double);and suppose that the following code calls the pow function:int x;double y;x = pow(2,3);y = pow(2,3);x = pow(2,3.2);y = pow(2,3.2);x = pow(2.1,3);y = pow(2.1,3);x = pow(2.1,3.2);y = pow(2.1,3.2);Given the language (a) C++; (b) Java; or (c) Ada, write down the number of the pow function called ineach of the eight calls, or write “illegal” if a call cannot be resolved in the language chosen, or if a datatype conversion cannot be made.7.18 Pointers present some special problems for overload resolution. Consider the following C++ code:void f( int* x) { ... }void f( char* x) { ... }int main(){ ...f(0);...}What is wrong with this code? How might you fix it? Can this problem occur in Java? In Ada?Exercises 319(continued)C7729_ch07.indd 319C7729_ch07.indd 319 03/01/11 10:07 AM03/01/11 10:07 AM

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

For the first part of the question, here's how each call to the pow function would be resolved in C++, Java, and Ada:

(a) C++:

  • x = pow(2,3); // Calls function (1) int pow(int, int)
  • y = pow(2,3); // Calls function (2) double pow(double, double)
  • x = pow(2,3.2); // Calls function (3) double pow(int, double), but it's illegal because you can't assign a double to an int without explicit casting
  • y = pow(2,3.2); // Calls function (3) double pow(int, double)
  • x = pow(2.1,3); // Calls function (2) double pow(double, double), but it's illegal because you can't assign a double to an int without explicit casting
  • y = pow(2.1,3); // Calls function (2) double pow(double, double)
  • x = pow(2.1,3.2); // Calls function (2) double pow(double, double), but it's illegal because you can't assign a double to an int without explicit casting
  • y = pow(2.1,3.2); // Calls function (2) double pow(double, double)

(b) Java: Java does not support function overloading based on return type, so all these calls would be illegal.

(c) Ada: Ada also does not support function overloading based on return type, so all these calls would be illegal.

For the second part of the question, the problem with the C++ code is that the call to f(0) is ambiguous. The compiler cannot determine whether 0 should be treated as an int* or a char*. To fix this, you could explicitly cast the 0 to the desired type, like so: f((int*)0) or f((char*)0).

This problem cannot occur in Java because Java does not allow pointers. In Ada, this problem can occur because Ada allows overloading based on parameter type.

This problem has been solved

Similar Questions

What is the output for the following code? 123456789#include <stdio.h>#include <math.h> int main() { double a = 3.0, b = 4.0; double c = sqrt(pow(a, 2) + pow(b, 2)); printf("%.2f", c); return 0;}

In C, what is the typical form of a function declaration when implementing call by value?

Can we declare function inside structure of C Programming?

When is the function declaration needed in C?

Consider the following code (which does not do anything significant)int h(int p) {    return p++;}int g(int x, int y) {    int z;   return h(y);}int f(int a, int b) {    if(a % 3 == 0) {        g(a, 2);        } else {        h(a);    }       return 10;}int main() {    int m;    scanf("%d", &m);    if(m % 2 == 0) {        f(m, 20);    } else {        g(20, m);    }       return 0;   }How many functions can be active the same time, in the execution of the above program?

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.