Currently I have three functions that individually add int+int, double+double/double+double/int+int, and a fucntion that concatenates strings.
Is there a way instead of having three functions, to simply use a template amnd make it a simple one function.
The goal of the code:
write a template function to add two arrays and return the resultant array
Arrays are added element by element.
The template function should work for integers , doubles, and strings
For strings the addition is actually concatenation.
Embed the function in a simple interactive test program and test it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <string>
using namespace std;
template <class Item>
Item add(Item a, Item b){ return a+b; }
void addingIntegers() // first function
{
int first[20], second[20]; int c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
cout << "Enter elements of first array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "Enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << " " << add(first[c], second[c]) << " ";
}
void addingDoubles() // second
{
float first[20], second[20]; int c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
cout << "Enter elements of first array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "Enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << " " << add(first[c], second[c]) << " ";
}
void AddingStrings() // third function
{
string first[20], second[20]; int c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
cout << "Enter elements of first array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "Enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << " " << add(first[c], second[c]) << " ";
}
int main()
{
char choice;
cout << "Press 'A' or 'a' to show the addition of integer-types. " << endl;
cout << "Press 'B' or 'b' to show the addition of double-types ." << endl;
cout << "Press 'C' or 'c' to show the concatenation of string literals." << endl;
cout << "Press any other character to end this program." << endl;
cout << "Your choice: ";
cin >> choice;
switch (choice)
{
case 'A':
case 'a':
addingIntegers();
break;
case 'B':
case 'b':
addingDoubles();
break;
case 'C':
case 'c':
AddingStrings();
break;
default:
cout << "\n\n\n\t\t\t";
break;
}
}
|
I know there is way to condense, I just need more enlightened eyes to guide me to a clearer solution.
I thank you in advance,
Giovanni