In: Computer Science
create overloaded functions called lastValue. The first function should take as a parameter a string and the second function should take as a parameter an int. each of you functions should return an int value. in the case of the function that takes the string as an argument you will return the ascii value of the last character in the string. in the case of the function that takes an int parameter your function will return the last digit in the number c++
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
using namespace std;
//method that takes a string and returns the ascii value of last character
int lastValue(string text){
//assuming text is not empty, finding last character
char lastChar=text[text.length()-1];
//returning the integer value of character, which is the ascii code of lastChar
return (int)lastChar;
}
//method that takes a number and returns the last digit
int lastValue(int number){
//performing modulo operation by 10 to get the last digit
int lastDigit=number%10;
return lastDigit;
}
int main(){
//testing both methods using string and integer arguments
cout<<"lastValue(abc): "<<lastValue("abc")<<endl; //99 (ascii code of 'c')
cout<<"lastValue(A): "<<lastValue("A")<<endl; //65 (ascii code of 'A')
cout<<"lastValue(25): "<<lastValue(25)<<endl; //5 (last digit in 25)
cout<<"lastValue(9): "<<lastValue(9)<<endl; //9 (last digit in 9)
return 0;
}
/*OUTPUT*/
lastValue(abc): 99
lastValue(A): 65
lastValue(25): 5
lastValue(9): 9