please run the following code, and kindly advise why the result is 20 instead of 10? thanks in advance
Code:
#include <iostream> #include <string> #include <vector> using namespace std; class A { public: A() : i(1) {} int i; }; class B : public A { public: B() : j(3) {} int j; }; int f(A* p, int count) { int total=0; for(int k=0; k<count; k++) { int temp = p->i; p++; cout << " k = " << k << ", " << temp << endl; total += temp; } return (total); } int main() { B b[10]; cout << f(b,10); return 0; }
Comment