Create two overloaded functions called sumLast. The first function will take as
ID: 3873369 • Letter: C
Question
Create two overloaded functions called sumLast. The first function will take as parameters two int variables. The second function will take as parameters three int variables. The functions will return the sum of the last digits in the integer. Example: Given two variables int x = 143, y = 3901; The last two digits of the variables are 3 and 1. Given these values then your two argument function will return 4. NOTE: You are not allowed to convert any of the parameters to a string. Do your best to eliminate as much duplicate code as you can.
Example: Given two variables int x = 143, y = 3901; The last two digits of the variables are 3 and 1. Given these values then your two argument function will return 4. NOTE: You are not allowed to convert any of the parameters to a string. Do your best to eliminate as much duplicate code as you can.
Explanation / Answer
Hi Please find below the code and output. Let me know if you have any doubts.
In C++
Code:
#include <iostream>
using namespace std;
class Overloading {
public:
int sumLast(int i, int j, int k) {
return i%10 + j% 10 + k% 10;
}
int sumLast(int i, int j) {
return i%10 + j % 10;
}
};
int main(void) {
Overloading over;
// Call print to print integer
cout << over.sumLast(5,5) <<" ";
cout << over.sumLast(5,50,1) <<" ";
cout << over.sumLast(0,1) << " ";
return 0;
}
Output:
10
6
1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.