Search code examples
c++for-loopnestedfunction-callsclass-members

nested for loops call to class member functions


I was wondering why the outcome of the following code is just:

The constructor has been called.

The constructor has been called.


Why aren't the invocations of the class member functions (is that right?) doing anything at all? I thought that I would have gotten back the difference, product, sum, and quotient for the values substituted in by the nested for loops, but this is not the case.

#include <iostream>

using namespace std;

class math{

public:

    float divide(int a, int b);

    float multiply(int a, int b);

    float add(int a, int b);

    float subtract(int a, int b);

    math();

};

math::math(void){

    cout << "The constructor has been called.\n";
}

float math::divide(int a, int b){

    return a/b;
}

float math::multiply(int a, int b){

    return a*b;

}

float math::add(int a, int b){

    return a + b;

}

float math::subtract(int a, int b){

    return a - b;

}


int main(){

    math a;
    math b;

    for(int i = 10; i > 0; i--){

        for(int x = 10; x > 10; x--){
        cout << b.subtract(i, x) << endl;
        cout << b.multiply(i, x) << endl;
        cout << b.add(i, x) << endl;
        cout << b.divide(i, x) << endl;
        }

    }

return 0;

}

Solution

  • You have just a little mistake in your inner for loop:

    for (int x = 10 ; x > 0 ; x--)