Elevation to power function using class templates I attached below a small working example of a function that is able to perform x^y. As it is possible to see the base (x) takes double and the power takes an int. As last the main loop will also round up or down the result accordingly. 

Not always I have these type of formats, and I could have base as int and power as double or base as long int and power as additional type. How can I improve this function to make it more general with different type format using template template &lt;class identifier&gt; or/and template &lt;typename identifier&gt;? 

#include &lt;cmath&gt;

using namespace std;

class Power {
public:
    double raiseToPower(double x, int power)
    {
        double result;
        int i;
        result = 1.0;
        for(i = 1; i &lt;=power; i++){
            result = result*x;
        }
        return result;
    }

    double floor0(double num)
      {
        if( (num + 0.5) &gt;= (int(num) + 1) )
        return int(num)+1;
        else
        return int(num);
    }
};


int main()
{
    double x;
    int i;
    Power example;
    std::cout&lt;&lt;"please enter the number"&lt;&lt;std::endl;
    std::cin&gt;&gt;x;
    std::cout&lt;&lt;"please enter the integer power that you want this number raised to"&lt;&lt;std::endl;
    std::cin&gt;&gt;i;
    std::cout&lt;&lt;"rise to power "&lt;&lt;i&lt;&lt;" is equal to "&lt;&lt;example.raiseToPower(x,i)&lt;&lt;std::endl;
    std::cout&lt;&lt;"the result rounded is "&lt;&lt; example.floor0(example.raiseToPower(x,i))&lt;&lt;std::endl;
}


Thank for any insight and point to the right direction.
Headers and namespaces

Don't using namespace, especially a big and growing namespace like std that's not designed for it.

There's no need to include &lt;cmath&gt;.  On the other hand, there is a clear need for &lt;iostream&gt;, which has been omitted.

Structure

There's no need for the Power class; it maintains no state.  The functions should simply be free functions, perhaps in a namespace.

Consider full range of types

raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs.  Perhaps like this:

    if (power &lt; 0) {
        return raiseToPower(1/x, -power);
    }


Improve the algorithm

For large power, the loop is executed many times.  We can use binary exponentation to reduce that to log₂ power iterations of the loop.

Avoid over-complication

floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
    return int(num + 0.5);
}


It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type.  std::floor() doesn't have that problem.

Validate inputs

If I enter a non-number, I don't get a clear error message.  Instead, the program uses uninitialised values, which is Undefined Behaviour.  Don't do that; instead check that std::cin is still good before using x or i.

Avoid std::endl unless you need output flushing

None of the uses of std::endl here are necessary, and we can use \n instead.  (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).



Modified code

double raiseToPower(double x, int power)
{
    if (power &lt; 0) {
        return raiseToPower(1/x, -power);
    }

    double result = 1.0;
    double m = x;

    for (;  power;  power /= 2) {
        if (power % 2) {
            result *= m;
        }
        m *= m;
    }
    return result;
}

constexpr double floor0(double num)
{
    return int(num + 0.5);
}


  

#include &lt;iostream&gt;

int main()
{
    double x;
    int i;
    std::cout &lt;&lt; "Please enter the number\n";
    std::cin &gt;&gt; x;
    std::cout &lt;&lt; "Please enter the integer power that "
                 "you want this number raised to\n";
    std::cin &gt;&gt; i;
    if (!std::cin) {
        std::cerr &lt;&lt; "Input format error\n";
        return 1;
    }
    auto const result = raiseToPower(x,i);
    std::cout &lt;&lt; x &lt;&lt; " raised to power " &lt;&lt; i &lt;&lt; " is equal to "
              &lt;&lt; result &lt;&lt; '\n';
    std::cout &lt;&lt; "The result rounded is "
              &lt;&lt; floor0(result) &lt;&lt; '\n';
}