c++ : template ( summery ) , short description or conclusion of template in hindi

short description or conclusion of template in hindi , c++ : template ( summery ) :-
इससे पहले के article मे template के सभी भागो और quality को discuss किया है अब इस article मे template के सार को पढेगे जिससे आ सभी को template के  basic को एक जगह से पढ़ा सके |
template को  generic class or function को create करेने के लिए ब्लूप्रिंट की तरह कार्य करता है | library containers जैसे  algorithm और  iterators generic programming का उदाहरन होता है और इसके के लिए template concept को अच्छी तरह पढना पढ़गे |
किसी एक container का definition होता है जिसे vector कहते है | लेकिन वेस्टर को अलग अलग तरह से define किया जासकता है जैसे  vector <int> or vector <string>.
template को क्लास से भी define किया जा सकता है और इसके अलावा function से भी template को define किया जा सकता है |
Function Template
जब template को function से define किया जाता है तब इसका syntax निन्म होता है :
template <class type> retern-type function-name(parameter list) {
// body of function
}
यहा पर :
class type : ये class के data type को define करता है |
return type: ये function के return type को define करता है |
function name : ये function का नाम होता है |
parameter list : इसमें function मे pass किये गये argument की लिस्ट होती है |
dattype और parameter लिस्ट को function definition मे declare किया जाता है |
निसछे दिए गये उदाहरन मे function template को use किया जाता है जिसमे दो values मे से maximum value को return किया जाता है |
Source Code
#include <iostream>
#include <string>
using namespace std;
template <typename any>
inline any const& Max (any const& a, any const& b) {
return a < b ? b:a;
}
int main () {
int i = 39;
int j = 20;
cout << “Max(i, j): ” << Max(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << “Max(f1, f2): ” << Max(f1, f2) << endl;
string s1 = “Hello”;
string s2 = “World”;
cout << “Max(s1, s2): ” << Max(s1, s2) << endl;
return 0;
}
जब उपर वाला code execute होता है तब इसका आउटपुट होगा :
Max(i, j): 39
Max(f1, f2): 20.7
Max(s1, s2): World
Class Template
जब template को class से भी define किया जाता है तब इसका syntax निन्म होता है :
template <class type> class class-name {
.
.
.
}
यहा पर :
class type : ये class के data type को define करता है |
return type: ये function के return type को define करता है |
class name : ये class का नाम होता है |
class body  : इसमें class की body को define किया जाता है  |
नीचे दिए गये उदहारण मे stack के push और pop operation को perform किया जाता है | stack के विशेष type data type जो की फीफो concept पर based होती है : −
Source Code
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;
template <class T>
class s{
private:
vector<T> element ;    // elements
public:
void push(T const&);  // push element
void pop();               // pop element
T top() const;            // return top element
bool empty() const {      // return true if empty.
return element .empty();
}
};
template <class T>
void s<T>::push (T const& elem) {
// append copy of passed element
element .push_back(elem);
}
template <class T>
void s<T>::pop () {
if (element .empty()) {
throw out_of_range(“s<>::pop(): empty stack”);
}
// remove last element
element.pop_back();
}
template <class T>
T s<T>::top () const {
if (element.empty()) {
throw out_of_range(“s<>::top(): empty stack”);
}
// return copy of last element
return element .back();
}
int main() {
try {
s<int>         intStack;  // sof ints
s<string> stringStack;    // sof strings
// manipulate int s
intStack.push(7);
cout << intStack.top() <<endl;
// manipulate string s
stringStack.push(“hello”);
cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
} catch (exception const& ex) {
cerr << “Exception: ” << ex.what() <<endl;
return -1;
}
}
If we compile and run above code, this would produce the following result −values
hello
Exception: s<>::pop(): empty stack
इसके अलावा निन्म उदाहरन मे template को एक और उदाहरन है जिसमे तीन values मे से सबसे बड़ी value को find करने केलिए template को use किया जाता है ||
#include<iostream>
#include<conio>
template <class any>
class maximum
{
private :
any n1, n2, n3;
public :
any compare(n1, n2, n3)
{
if(n1 >= n2 && n1 >= n3)
{
cout << “Large number: ” << n1;
}
if(n2 >= n1 && n2 >= n3)
{
cout << “Large number: ” << n2;
}
if(n3 >= n1 && n3 >= n2) {
cout << “Large number: ” << n3;
}
}
void main()
{
int num1 , num2 , num3 ;
cout<<“enter Value:”<<endl;
cin>>num1>>num2>>num3;
maximum m;
m.compare(num1 , num2 , num3)
float nu1 , nu2 , nu3 ;
cout<<“enter Value:”<<endl;
cin>>nu1>>nu2>>nu3;
m.compare(nu1 , nu2 , nu3)
getch();
}
इस उदाहरन मे , तीन number को input किया जाता है जिसमे से बड़ी number को find किया जाता है |इस article मे , template के सार को discuss किया है |