M
maadhuu
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 derivedublic 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 = &derived(a++);b->virt();
}
for(int i = 0; i<3;++i)
b->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->virt() for i = 0 to 2)
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 derivedublic 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 = &derived(a++);b->virt();
}
for(int i = 0; i<3;++i)
b->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->virt() for i = 0 to 2)