-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from aayush105/template-programs
feat:add templates program in cpp
- Loading branch information
Showing
9 changed files
with
242 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
- [x] [q1](q1.cpp): WAP to find MAX value of array with default argument with class template. | ||
- [ ] [q2](q2.cpp): WAP to show use of multiple template. | ||
- [ ] [q3](q3.cpp): WAP to show template and non template argument. | ||
- [ ] [q4](q4.cpp): WAP to find maximum and minimum value of an array using class template. | ||
- [ ] [q5](q5.cpp): WAP to demonstrate different functions of vector and reverse() which is algorithm. | ||
- [ ] [q6](q6.cpp): WAP to compare 2 values using function template and display largest value. Check your program for char, int and float type data. | ||
- [x] [q2](q2.cpp): WAP to show use of multiple template. | ||
- [x] [q3](q3.cpp): WAP to show template and non template argument. | ||
- [x] [q4](q4.cpp): WAP to find maximum and minimum value of an array using class template. | ||
- [x] [q5](q5.cpp): WAP to demonstrate different functions of vector and reverse() which is algorithm. | ||
- [x] [q6](q6.cpp): WAP to compare 2 values using function template and display largest value. Check your program for char, int and float type data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// WAP to show use of multiple template. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Class template with multiple parameters | ||
template <class T, class U> | ||
class Pair | ||
{ | ||
private: | ||
T first; // First value of the pair | ||
U second; // Second value of the pair | ||
|
||
public: | ||
// Constructor to initialize the pair | ||
Pair(T a, U b) : first(a), second(b) {} | ||
|
||
T getFirst() { return first; } | ||
U getSecond() { return second; } | ||
}; | ||
|
||
int main() | ||
{ | ||
// Creating instances of Pair with different data types | ||
Pair<int, double> pair1(5, 6.7); // Instantiating Pair with int and double types | ||
Pair<char, int> pair2('a', 10); // Instantiating Pair with char and int types | ||
|
||
// Displaying the values stored in the pairs | ||
cout << "Pair 1: " << pair1.getFirst() << " and " << pair1.getSecond() << endl; // Displaying the first and second values of pair1 | ||
cout << "Pair 2: " << pair2.getFirst() << " and " << pair2.getSecond() << endl; // Displaying the first and second values of pair2 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// WAP to show template and non-template argument. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Function template with one template argument and two non-template arguments | ||
template <class T> | ||
T add(T a, T b, T c) { | ||
return a + b + c; | ||
} | ||
|
||
int main() { | ||
int x = 5, y = 10, z = 15; // Non-template arguments | ||
double d1 = 1.5, d2 = 2.5, d3 = 3.5; // Non-template arguments | ||
|
||
cout << "Sum of integers: " << add(x, y, z) << endl; | ||
cout << "Sum of doubles: " << add(d1, d2, d3) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// WAP to find the maximum and minimum value of an array using class template. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Class template to analyze an array | ||
template<class T, int size> | ||
class ArrayOperation { | ||
private: | ||
T arr[size]; // Array to store elements | ||
|
||
public: | ||
// Method to input elements into the array | ||
void inputArray() { | ||
cout << "Enter " << size << " elements: "; | ||
for (int i = 0; i < size; ++i) { | ||
cin >> arr[i]; | ||
} | ||
} | ||
|
||
// Method to find the maximum value in the array | ||
T findMax() { | ||
T maxVal = arr[0]; | ||
for (int i = 1; i < size; ++i) { | ||
if (arr[i] > maxVal) { | ||
maxVal = arr[i]; | ||
} | ||
} | ||
return maxVal; | ||
} | ||
|
||
// Method to find the minimum value in the array | ||
T findMin() { | ||
T minVal = arr[0]; | ||
for (int i = 1; i < size; ++i) { | ||
if (arr[i] < minVal) { | ||
minVal = arr[i]; | ||
} | ||
} | ||
return minVal; | ||
} | ||
}; | ||
|
||
int main() { | ||
const int arraySize = 5; | ||
ArrayOperation<int, arraySize> arr; // Creating an instance of the ArrayOperation class with integer type | ||
arr.inputArray(); // Inputting elements into the array | ||
|
||
cout << "Maximum value: " << arr.findMax() << endl; | ||
cout << "Minimum value: " << arr.findMin() << endl; | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// WAP to demonstrate different functions of vector and reverse() which is an algorithm. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
int main() { | ||
// Declare and initialize a vector | ||
vector<int> myVector = {1, 2, 3, 4, 5}; | ||
|
||
// Display the original vector | ||
cout << "Original Vector: "; | ||
for (int i = 0; i < myVector.size(); ++i) { | ||
cout << myVector[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
// Demonstrating different functions of vector | ||
myVector.push_back(6); // Adding an element to the end of the vector | ||
cout << "Vector after push_back: "; | ||
for (int i = 0; i < myVector.size(); ++i) { | ||
cout << myVector[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
cout << "Size of the Vector: " << myVector.size() << endl; // Displaying the size of the vector | ||
|
||
// Using the reverse algorithm to reverse the vector | ||
reverse(myVector.begin(), myVector.end()); | ||
cout << "Reversed Vector: "; | ||
for (int i = 0; i < myVector.size(); ++i) { | ||
cout << myVector[i] << " "; | ||
} | ||
cout << endl; | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// WAP to compare 2 values using a function template and display the largest value. Check your program for char, int, and float type data. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Function template to find the maximum of two values | ||
template <class T> | ||
T findMax(T a, T b) { | ||
return (a > b) ? a : b; | ||
} | ||
|
||
int main() { | ||
// Taking input for comparing two char values | ||
char char1, char2; | ||
cout << "Enter two characters: "; | ||
cin >> char1 >> char2; | ||
cout << "Largest char: " << findMax(char1, char2) << endl; | ||
|
||
// Taking input for comparing two int values | ||
int int1, int2; | ||
cout << "Enter two integers: "; | ||
cin >> int1 >> int2; | ||
cout << "Largest int: " << findMax(int1, int2) << endl; | ||
|
||
// Taking input for comparing two float values | ||
float float1, float2; | ||
cout << "Enter two float values: "; | ||
cin >> float1 >> float2; | ||
cout << "Largest float: " << findMax(float1, float2) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
- [ ] [q1](q1.cpp): WAP to show more than one template mechanism. | ||
- [x] [q1](q1.cpp): WAP to show more than one template mechanism. | ||
- [x] [q2](q2.cpp): WAP to show function template overlaoding with function. | ||
- [ ] [q3](q3.cpp): WAP to show overloading template with template and normal function. | ||
- [x] [q3](q3.cpp): WAP to show overloading template with template and normal function. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// WAP to show more than one template mechanism. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// First function template with one type parameter | ||
template <class T> | ||
void print(T value) { | ||
cout << "Value: " << value << endl; | ||
} | ||
|
||
// Second function template with two type parameters | ||
template <class T, class U> | ||
void multiplyAndPrint(T a, U b) { | ||
cout << "Product: " << a * b << endl; | ||
} | ||
|
||
int main() { | ||
|
||
print(5); | ||
multiplyAndPrint(4, 5.5); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// WAP to show overloading template with template and a normal function. | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// Template function that takes one type parameter | ||
template <class T> | ||
T add(T a, T b) { | ||
cout << "Template add function called." << endl; | ||
return a + b; | ||
} | ||
|
||
// Overloading the template function with another template function | ||
template <class T, class U> | ||
T add(T a, U b) { | ||
cout << "Overloaded template add function called." << endl; | ||
return a + static_cast<T>(b); | ||
} | ||
|
||
// Overloading the template function with a regular function | ||
int add(int a, int b) { | ||
cout << "Regular add function called." << endl; | ||
return a + b; | ||
} | ||
|
||
int main() { | ||
cout << add(5, 6) << endl; // Calls the regular add function | ||
cout << add(5, 6.7) << endl; // Calls the overloaded template add function | ||
cout << add(5.5, 6.7) << endl; // Calls the template add function | ||
|
||
return 0; | ||
} | ||
|