Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Output needs some explanation (http://www.go4expert.com/forums/output-explanation-t388/)

shabbir 19Jul2005 16:48

Output needs some explanation
 
Can any one give any valid reason why the output of the following program in VC++ 6.0 Compiler is
Code:

#include <iostream>
using namespace std;

class base
{
public:
  int a;
  base(){ a=0;}
};

class der:public base
{
public:
  int b;
  der(){ b=1;}
};
void F(base *ptr,int n)
{
  for(int i=0; i<n; i++,ptr++)
    cout<<ptr->a;
  cout<<endl;
}

int main()
{
  base Base1[5];
  F(Base1,5);
  der Der1[5];
  F(Der1,5);
  return 0;
}

Output
00000
01010

coderzone 19Jul2005 17:27

Re: Output needs some explanation
 
Hey very interesting problem. I will try this.

coderzone 23Jul2005 10:28

Re: Output needs some explanation
 
The catch lies here

for(int i=0; i<n; i++,ptr++)

If you see there is a ptr ++ and ptr is always a base pointer and so the amount of values that are incremented at\re size of base and so once it prints a and then b and so the output is 01010


All times are GMT +5.5. The time now is 07:44.