Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ template function

template <class T>
void swap(T & lhs, T & rhs)
{
 T tmp = lhs;
 lhs = rhs;
 rhs = tmp;
}
Comment

C++ template function

/*
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;
}
Comment

function template c++

template<class T1 , class T2>
float fun(T1 a, T2 b){
    float avg2 = (a+b)/2.0;
    return avg2;
}
Comment

C++ Templates as function

template <class T>
class Number {
    ... .. ...
    // Function prototype
    T getnum();
};

// Function definition
template <class T>
T Number<T>::getNum() {
    return num;
}
Comment

Function Template in c++

// 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Set Specific Time in youtube Video url 
Cpp :: program to find third smallest number c++ 
Cpp :: how to include a library in arduino 
Cpp :: cpp tutorial 
Cpp :: split string by delimiter cpp 
Cpp :: c++ destructor 
Cpp :: https://www.codegrepper.com 
Cpp :: imgui menu bar 
Cpp :: call by value in c++ 
Cpp :: c++ forloop 
Cpp :: do while loop c++ 
C :: c colour text 
C :: how to remove button decoration 
C :: how to download file in powershell 
C :: vscode arduino [Warning] Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README. 
C :: disable lua errors 
C :: grep find and replace 
C :: %hd c 
C :: thread in c 
C :: determination of armstrong number in c 
C :: add field to model rails 
C :: hi servicenow 
C :: redirect to url page asp.net mvc 
C :: sum average min max in c array 
C :: strcmp c 
C :: c in array 
C :: #define arduino 
C :: convert c program to assembly language online 
C :: passing two dimensional array to function in c 
C :: c long to string 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =