I explain my problem. I want to code math with template general thing. So far I've made two classes. I have a
template<typename T> class rational{
T numerator;
T denominator;
operators and constructors
}
And I have a
template<typename T> class polynomial{
vector<T> coefficients;
int degree;
operators and constuctors
}
these are made so that I can use rational<polynomial<rational<int>>>
. This works so far. But I want to add a Derivative on those class. I want to define it on the class<T>
using it's definition on T too. This can be made. But how can I make this Derivative be =0 on int, on double, on fractional<int>
, etc ? It's like a specialization, but for every type which I didn't specifie ... Thanks ! After this, I try to template the matrix thing ...
the goal is to go as far as possible with templates :-D Next I want to use several variables for polynomial ... It's a great projet !
Thanks for any help / any information =)
I think answer would be like :
template<typename T> T Derivative(T object){ // for any other object !
return T(0);}
template<typename T> polynomial<T> Derivative(polynomial<T> object)
return compute_of_derivative, using too Derivative over T too (for generality);}
template<typename T> rational<T> Derivative(rational<T> object) // kind P / Q
return compute_of_derivative, which is ( D(P)Q -Q D(P) / Q^2 );}
is this working ? :-/ I will try it soon ...
CodePudding user response:
I want to define a function Derivative that returns 0 everytime, but I want my derivative to return something computed for the class polynomial and for the class fractional. That's my only problem now.
You can do that by overloading the derivative function:
#include<iostream>
template<typename T> struct polynomial{};
template<typename T> struct rational{};
template<typename T>
auto derivative(T t) { std::cout<<"I'm zero"<<std::endl; }
template<typename T>
auto derivative(rational<T> t) { std::cout<<"I'm a rational number"<<std::endl; }
template<typename T>
auto derivative(polynomial<T> t) { std::cout<<"I'm a polynomial"<<std::endl; }
int main()
{
derivative(rational<double>{});
derivative(polynomial<double>{});
derivative(int{});
}