C++ program to use Function Template

//read two integers and two fractional numbers from user
//calculate quotient for each pair dividing first number by second
// using single function

#include <iostream>
using namespace std;
template <class D>
D Divide(D x, D y)
{
    return x/y;
}
int main()
{
    int i1,i2,iQ;
    float f1,f2,fQ;
    cout<<endl<<"Enter two integers: ";
    cin>>i1>>i2;
    cout<<endl<<"Enter two floats: ";
    cin>>f1>>f2;
    iQ=Divide(i1,i2);
    fQ=Divide(f1,f2);
    cout<<endl<<"Quotient for integers is "<<iQ;
    cout<<endl<<"Quotient for floats is "<<fQ;
    return 0;
}

No comments:

Post a Comment