A container class is a class that can hold a collection of objects or data types. It is used to group related data together in a logical way. Some examples of container classes in C++ are arrays, vectors, queues, stacks, and lists.
Here is an example program that uses function templates to find the largest number among three given numbers:
using namespace std;
template<class T>
T max(T x, T y, T z) {
T max_num = x;
if (y > max_num) {
max_num = y;
}
if (z > max_num) {
max_num = z;
}
return max_num;
}
int main() {
int a = 10, b = 20, c = 30;
cout << "Largest number: " << max(a, b, c) << endl;
double x = 3.14, y = 2.71, z = 1.618;
cout << "Largest number: " << max(x, y, z) << endl;
return 0;
}
In this program, the max() function template is used to find the largest number among three given numbers. The function template takes three arguments of the same data type, and returns the largest number among them. The main() function demonstrates the use of this template with both integer and double data types.
No comments:
Post a Comment
If you have any doubts, please let me know