Problem statement In this question first we will practice function overloading and then function templates. Please follow below instructions. 1. We can extend multiplication easily for string types if we interpret the operation as repetition. For example "code" * 3 may be interpreted as "codecodecode". In fact, languages like Python already support this operation. Write a C++ function named multiply that can multiply(repeat) an std::string by a given integer number and return the repeated string. 2. Write another C++ function named multiply that can multiply two given integer (int type) numbers and return the product as an integer. 3. Write another C++ function with the same name that can multiply a floating point number (double type) by a given integer number and return the product as a floating point number. 4. We defined three functions with the same name without a problem. It is either because they have a different number of parameters, or because any of their parameters are of a different type. This allows the compiler to recognize which one to consider, each function is unique though the names are same. 5. What if we can write a single function that can handle all three data types and give the results accordingly? We can do so using C++ function templates. Write a C++ function template named multiply_type that can take any floating point number(double type) or integer(int type) or an std::string as the first parameter and any integer number as the second and returns the respective product of the two. Do the following 1. Write your algorithm as code comments. I recommend to follow UMPIRE technique ( 2. Implement your functions (60) 3. In your driver program, test your function for the criteria given below. Note that this may be either fully or partially set up already in CodeCheck. points) 1 2 3 5 6 Test # 10 10 4 6 2.8 10 "C++" 10 "Rain" 2 input Criteria 100 24 28.0 Output "C++C++C++C++C++C++C++C++C++C++" "RainRain"

icon
Related questions
Question

C++ questions 

why I'm getting these errors please help 

Problem statement
In this question first we will practice function overloading and then function templates. Please follow below instructions.
1. We can extend multiplication easily for string types if we interpret the operation as repetition. For example "code" * 3 may be interpreted as
"codecodecode". In fact, languages like Python already support this operation. Write a C++ function named multiply that can multiply(repeat) an
std::string by a given integer number and return the repeated string.
2. Write another C++ function named multiply that can multiply two given integer (int type) numbers and return the product as an integer.
3. Write another C++ function with the same name that can multiply a floating point number (double type) by a given integer number and return the product
as a floating point number.
4. We defined three functions with the same name without a problem. It is either because they have a different number of parameters, or because any of
their parameters are of a different type. This allows the compiler to recognize which one to consider, each function is unique though the names are
same.
5. What if we can write a single function that can handle all three data types and give the results accordingly? We can do so using C++ function templates.
Write a C++ function template named multiply_type that can take any floating point number(double type) or integer(int type) or an std::string as the first
parameter and any integer number as the second and returns the respective product of the two.
Do the following
1. Write your algorithm as code comments. I recommend to follow UMPIRE technique (
2. Implement your functions (
3. In your driver program, test your function for the criteria given below. Note that this may be either fully or partially set up already in CodeCheck.
points)
1
2
3
5
6
Test #
10
10
4
6
2.8
10
"C++"
10
"Rain"
2
input
Criteria
100
24
28.0
Output
"C++C++C++C++C++C++C++C++C++C++"
"RainRain"
Transcribed Image Text:Problem statement In this question first we will practice function overloading and then function templates. Please follow below instructions. 1. We can extend multiplication easily for string types if we interpret the operation as repetition. For example "code" * 3 may be interpreted as "codecodecode". In fact, languages like Python already support this operation. Write a C++ function named multiply that can multiply(repeat) an std::string by a given integer number and return the repeated string. 2. Write another C++ function named multiply that can multiply two given integer (int type) numbers and return the product as an integer. 3. Write another C++ function with the same name that can multiply a floating point number (double type) by a given integer number and return the product as a floating point number. 4. We defined three functions with the same name without a problem. It is either because they have a different number of parameters, or because any of their parameters are of a different type. This allows the compiler to recognize which one to consider, each function is unique though the names are same. 5. What if we can write a single function that can handle all three data types and give the results accordingly? We can do so using C++ function templates. Write a C++ function template named multiply_type that can take any floating point number(double type) or integer(int type) or an std::string as the first parameter and any integer number as the second and returns the respective product of the two. Do the following 1. Write your algorithm as code comments. I recommend to follow UMPIRE technique ( 2. Implement your functions ( 3. In your driver program, test your function for the criteria given below. Note that this may be either fully or partially set up already in CodeCheck. points) 1 2 3 5 6 Test # 10 10 4 6 2.8 10 "C++" 10 "Rain" 2 input Criteria 100 24 28.0 Output "C++C++C++C++C++C++C++C++C++C++" "RainRain"
34
35 // MATCH.
36
//
37
38
39 // PLAN. Please comment out your writing.
40 //
41 //
42
43 //Replace ... with your code. Hit enter key to get more vertical space
// IMPLEMENT
44
45 #include "multiply.h"
46 std::string multiply (std::string stringl, int number) {
47
48
49
50
51
52
53
54
55 int multiply(int numberl, int number2) {
56
return number1 * number2;
57
58
59 double multiply (double numberl, int number2) {
60
return number1 * number2;
61
10
11
12
13
14
15
}
multiply.h
16
17
1 // IMPLEMENT
2 #ifndef MULTIPLY_H
3
#define MULTIPLY H
4 #include <string>
18
19
20
21
}
5 std::string multiply(std::string string1, int number);
6 int multiply(int numberl, int number2);
7 double multiply (double numberl, int number2);
8
9
}
T return value = T();
for (int i = 0; i < number; ++i) {
return value += string1;
}
return return value;
12
13
14
15
16
17
18
19
20
21
22
23
// In templets, we can't separate the declaration from definition; We have to write it in a
template<class T>
T multiply_type (T entityl, int number) {
22 }
23 #endif // MULTIPLY_H
multiply_driver.cpp
1 // REVIEW
2 #include <iostream>
3 #include "multiply.h"
4
30
31
32 }
// Write your code below
// You may need to initialize your return value like,
// T return value = T();
T return value = T();
for (int i = 0; i < number; ++i) {
return value += entityl;
5 //Replace ... with your code. Hit enter key to get more vertical space
6
int main(int argc, char *argv[])
7
{
8
9
10
11
}
return return_value;
Score
0
// Testing multiply
std::cout << multiply (10, 10) <<std::endl;
/* By exactly following the pattern above, test your function below for rest of the test
in the order they are given*/
std::cout << multiply (4, 6) <<std::endl;
std::cout << multiply (2.8, 10) <<std::endl;
std::cout << multiply("C++", 10) <<std::endl;
std::cout << multiply("Rain", 2) <<std::endl;
std::cout << "Testing template function to prove the ability to write generic (type indep
std::cout << multiply_type<int>(10, 10) <<std::endl;
/* By exactly following the pattern above, test your function below for rest of the test
in the order they are given*/
24
25
26
27 std::cout<<< multiply_type<std::string>("C++", 10) <<std::endl;
28
std::cout << multiply_type<std::string>("Rain", 2) <<std::endl;
29
CodeCheck
std::cout << multiply_type<int>(10, 10) <<std::endl;
std::cout << multiply_type<int>(4, 6) <<std::endl;
std::cout << multiply_type<double> (2.8, 10) <<std::endl;
return 0;
Reset
Running multiply_driver.cpp
Error:
multiply.cpp: In function 'std::string multiply (std::string, int)':
multiply.cpp:47:3: error: 'T' was not declared in this scope
47 | T return_value = T();
multiply.cpp:49:5: error: 'return_value' was not declared in this scope
49 | return value += string1;
multiply.cpp:51:10: error: 'return_value' was not declared in this scope
51 | return return_value;
Transcribed Image Text:34 35 // MATCH. 36 // 37 38 39 // PLAN. Please comment out your writing. 40 // 41 // 42 43 //Replace ... with your code. Hit enter key to get more vertical space // IMPLEMENT 44 45 #include "multiply.h" 46 std::string multiply (std::string stringl, int number) { 47 48 49 50 51 52 53 54 55 int multiply(int numberl, int number2) { 56 return number1 * number2; 57 58 59 double multiply (double numberl, int number2) { 60 return number1 * number2; 61 10 11 12 13 14 15 } multiply.h 16 17 1 // IMPLEMENT 2 #ifndef MULTIPLY_H 3 #define MULTIPLY H 4 #include <string> 18 19 20 21 } 5 std::string multiply(std::string string1, int number); 6 int multiply(int numberl, int number2); 7 double multiply (double numberl, int number2); 8 9 } T return value = T(); for (int i = 0; i < number; ++i) { return value += string1; } return return value; 12 13 14 15 16 17 18 19 20 21 22 23 // In templets, we can't separate the declaration from definition; We have to write it in a template<class T> T multiply_type (T entityl, int number) { 22 } 23 #endif // MULTIPLY_H multiply_driver.cpp 1 // REVIEW 2 #include <iostream> 3 #include "multiply.h" 4 30 31 32 } // Write your code below // You may need to initialize your return value like, // T return value = T(); T return value = T(); for (int i = 0; i < number; ++i) { return value += entityl; 5 //Replace ... with your code. Hit enter key to get more vertical space 6 int main(int argc, char *argv[]) 7 { 8 9 10 11 } return return_value; Score 0 // Testing multiply std::cout << multiply (10, 10) <<std::endl; /* By exactly following the pattern above, test your function below for rest of the test in the order they are given*/ std::cout << multiply (4, 6) <<std::endl; std::cout << multiply (2.8, 10) <<std::endl; std::cout << multiply("C++", 10) <<std::endl; std::cout << multiply("Rain", 2) <<std::endl; std::cout << "Testing template function to prove the ability to write generic (type indep std::cout << multiply_type<int>(10, 10) <<std::endl; /* By exactly following the pattern above, test your function below for rest of the test in the order they are given*/ 24 25 26 27 std::cout<<< multiply_type<std::string>("C++", 10) <<std::endl; 28 std::cout << multiply_type<std::string>("Rain", 2) <<std::endl; 29 CodeCheck std::cout << multiply_type<int>(10, 10) <<std::endl; std::cout << multiply_type<int>(4, 6) <<std::endl; std::cout << multiply_type<double> (2.8, 10) <<std::endl; return 0; Reset Running multiply_driver.cpp Error: multiply.cpp: In function 'std::string multiply (std::string, int)': multiply.cpp:47:3: error: 'T' was not declared in this scope 47 | T return_value = T(); multiply.cpp:49:5: error: 'return_value' was not declared in this scope 49 | return value += string1; multiply.cpp:51:10: error: 'return_value' was not declared in this scope 51 | return return_value;
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer