The following is a piece of code and the output of the code is pasted after
it . I dont understand how it works (while giving the output) .Can someone
enlighten me on this ?
thanking you .
maadhuu .
the following is the code
class base
{
int data;
public :
base() { }
virtual void virt()
{
}
};
class derived:public base
{
int data;
public :
derived(int a):data(a) { }
virtual void virt()
{
std::cout << data ;
}
};
int main()
{
int a = 1;
base *b[3] ;
for(int i = 0; i<3;++i)
{ b[i] = &derived(a++);b[i]->virt();
}
for(int i = 0; i<3;++i)
b[i]->virt();
return 0 ;
}
the output is 123333 (1 2 3 is for what is printed in the loop soon after
initializing it with an object ) .My question is why is the output 3 in
the other case ?(i.e.for b[i]->virt() for i = 0 to 2)