template <class T>
void swap(T & lhs, T & rhs)
{
T tmp = lhs;
lhs = rhs;
rhs = tmp;
}
/*
C++ template functions are an alternamive way to write a function that
can take different data types. C++ Template functions are only one
function, so if you need to make a change, then it only has to be done
once. Here is an example of a 'get_doubled' templated function:
*/
#include <iostream>
using std::cout;
template <typename T> // Now, T is a type of variable, for this scope:
void say_something(T input) {
cout << input << "
";
}
int main(void) {
say_something(45); // Uses a int
say_something("Hello"); // Uses a string
say_something(90.5); // Uses a float/double
return 0;
}
template <class T>
class className {
private:
T var;
... .. ...
public:
T functionName(T arg);
... .. ...
};
class Object
{
public:
template<typename T>
void function(T value){}
};
// function template in c++
#include <iostream>
using namespace std;
float funAvg(int a, int b){
float avg = (a+b)/2.0;
return avg;
}
float funAvg2(int a, float b){
float avg2 = (a+b)/2.0;
return avg2;
}
//function template in c++
template<class T1 , class T2>
float fun(T1 a, T2 b){
float avg2 = (a+b)/2.0;
return avg2;
}
int main(){
float a;
a = funAvg(22 , 7);
printf("The average of these number is %.3f ",a);
cout<<endl;
float a1;
a1 = funAvg2(11 , 8.6);
printf("The average of these number is %.3f ",a1);
cout<<endl;
// float T;
// T = fun(11 , 8.6f);
// printf("The average of these number is %.3f ",T);
// cout<<endl;
// ---------------------function template in c++-----------------
float T;
T = fun(11 , 98);
printf("The average of these number is %.3f ",T);
return 0;
}
class Object
{
public:
template<class T>
void DoX(){}
};
Code Example |
---|
Cpp :: youtube to facebook link converter |
Cpp :: C++ Join thread |
Cpp :: sideways triangle c++ xy plane |
Cpp :: check whether kth bit is 1 |
Cpp :: c++ x y in arrau 1d |
Cpp :: cpp fread |
Cpp :: CPPDEVELOPER |
Cpp :: comentar todas linhas de uma vez vs code |
Cpp :: cpp class access array member by different name |
Cpp :: file transfer socat |
Cpp :: CPP Find options passed from command line |
Cpp :: primtiive calculator in c++ |
Cpp :: map::begin |
Cpp :: c++ trim string |
Cpp :: DMA c/c++ |
Cpp :: 2000pp pp play osu std |
Cpp :: C++ Creating a Class Template Object |
Cpp :: displaying m images m windows opencv c++ |
Cpp :: Tricky Subset Problem |
Cpp :: find maximum contiguous Sub arrays |
Cpp :: esp8266 wifi.localip() to string |
Cpp :: c++ unordered set count |
Cpp :: c++ how to iterate through 2d array in c++ |
Cpp :: ex:Roblox |
Cpp :: yearly interest calculator c++ using for loop |
Cpp :: arrays to function c++ |
Cpp :: log like printf c++ |
Cpp :: cast c++ |
Cpp :: c++ else |
Cpp :: declare a structer in cpp |