Docsity

Function Overloading and Call-by-Reference in C++, Exams of Computer Science

Toggle sidebar
bg1
Exercise 11/04/03
Objective: Learn about overloading function names, functions as procedures, and call-by-reference parameters.
1. Answer all questions below.
2. Test and correct your answer.
#include <iostream>
using namespace std;
int convert_to_inches(int feet, int inches);
void convert_to_inches(int feet, int inches, int& total_inches);
int main()
{
cout << “Enter two integers that represent the length “
<< “in feet and inches: “;
cin >> feet >> inches;
// a) Will two functions with the same name in the same program cause
// syntax error? Explain your answer.
// b) Show statements needed to call the first convert_to_inches function
// to obtain the converted amount and output it. Add all missing
// statements so that the program will work.
// c) Show statements needed to call the second convert_to_inches function
// to obtain the converted amount and output it. Add all missing
// statements so that the program will work.
return 0;
}
int convert_to_inches(int feet, int inches)
{
return 12*feet + inches;
}
void convert_to_inches(int feet, int inches, int& total_inches)
{
total_inches = 12*feet + inches;
}

Partial preview of the text

Download Function Overloading and Call-by-Reference in C++ and more Exams Computer Science in PDF only on Docsity!

Exercise 11/04/

Objective: Learn about overloading function names, functions as procedures, and call-by-reference parameters.

1. Answer all questions below.

2. Test and correct your answer.

#include using namespace std; int convert_to_inches(int feet, int inches); void convert_to_inches(int feet, int inches, int& total_inches); int main() { cout << “Enter two integers that represent the length “ << “in feet and inches: “; cin >> feet >> inches; // a) Will two functions with the same name in the same program cause // syntax error? Explain your answer. // b) Show statements needed to call the first convert_to_inches function // to obtain the converted amount and output it. Add all missing // statements so that the program will work. // c) Show statements needed to call the second convert_to_inches function // to obtain the converted amount and output it. Add all missing // statements so that the program will work. return 0; } int convert_to_inches(int feet, int inches) { return 12feet + inches; } void convert_to_inches(int feet, int inches, int& total_inches) { total_inches = 12feet + inches; }