LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Predict the Output · Question 20

Overloads with different numbers of parameters Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

What is the output of the following C++ program?

main.cpp
#include <iostream>

int area(int side)              { return side * side; }
int area(int l, int w)          { return l * w; }
double area(double r)           { return 3.0 * r * r; }

int main() {
    std::cout << area(4) << ' ' << area(4, 5) << ' ' << area(2.0) << ' ' << area('\2') << '\n';
    return 0;
}
  • A 16 20 12 4 Correct
  • B 16 20 12 12
  • C 16 20 4 4
  • D 16 9 12 4
Correct Answer: Option A

The number of arguments narrows the candidates; then types decide. '\2' is the char with value 2, promoted to int, so area(int) gives 4.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links