forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39-overloading_last.cpp
37 lines (33 loc) · 933 Bytes
/
39-overloading_last.cpp
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
#include <iostream>
#include <string>
#include <vector>
std::string last(const std::vector<std::string> &input)
{
/* Question: What should we do if the input vector
has no elements at all? */
return input.at(input.size() - 1);
}
int last(const std::vector<int> &input)
{
/* Question: What should we do if the input vector
has no elements at all? */
return input.at(input.size() - 1);
}
char last(const std::string &input)
{
/* Question: What should we do if the input string
has no elements at all? */
return input.at(input.size() - 1);
}
int main()
{
std::vector<std::string> V{"Raspberry", "Pineapple", ""};
std::vector<int> W{6, 10, 17};
auto x{last(V)};
auto y{last(x)};
auto z{last(W)};
std::cout << "x: " << x << std::endl;
std::cout << "y: " << y << std::endl;
std::cout << "z: " << z << std::endl;
return 0;
}