Inheration and Iteration

I need to create 2 programs that have the same function, the first one use inheration, and the second use iteration. can anyone help me?

and also can anyone explain is this iteration or inheration?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

int getDiff(int X, int Y) { // Tail Implementation
  X++; // Inc X
  if (X == Y)
    return 1;

  return (1+getDiff(X, Y));
}


void getDiff2(int X, int Y, int Diff = 0) {
  X++;
  Diff++;

  if (X != Y)
    getDiff2(X, Y, Diff);
  else
    cout << "Diff Was: " << Diff << endl;

  return;
}

void main(void) {

  cout << "Difference Between 1 & 7 Is: " << getDiff(1, 7) << endl;
  getDiff2(2, 8);
  getchar();
}
you need to read tutorials if you don't know what iteration or inheritance are, yes, inheritance, not inheration.

http://cplusplus.com/doc/tutorial/
I am Sorry, What I means is Recursive and iterative
Misstyping (My bad..)

I am pretty sure that code above is about Recursive..
Just, I don't Understand what's Iterative ( I am rarely use it )
recursive functions call themselves, whereas using iteration you would just loop to perform the same task. You say that you rarely use iteration but this can't be true. Observe the following functions which both calculate a number to the nth power.

1
2
3
4
5
6
7
8
9
10
// iterative
int numToPowerOfN(int num, int power)
{
        for (int i=0; i<power; i++)
        {
                num = num*num;
        }

        return num;
}


1
2
3
4
5
6
7
8
// recursive
int numToPowerOfN(int num, int power)
{
        if ( power == 0 )
                return num;
        else
                return numToPowerOfN(num*num, --power);
}
thanks!
Topic archived. No new replies allowed.