Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Write recursive function getpower to calculate x y, aiming at integer and real overloaded functions in the same program: int getPower(int
Write recursive function getpower to calculate x y, aiming at integer and real overloaded functions in the same program: int getPower(int
If you use function overload, you still need to define two recursive functions based on integer and real number respectively, but when calling the main function, the real number and integer function names are the same.

int get power(int t,int a)

{

If (a==0)

Returns1;

Returns t * get power(t, a-1);

}

double get power(double t,int a)

{

If (a==0)

Returns1;

Returns t * get power(t, a-1);

You can use function templates to simplify it.

Template & lttypename T>

T get power(T T,int a)

{

If (a==0)

Returns1;

Returns t * get power(t, a-1);

}