# Thread: Need some help with inheritance and polymorphism

1. ## Need some help with inheritance and polymorphism

Hi.

I'm studing for the finals, and I came across this confusing code segment, to which I need to determine what will be the output:

Code:
```class A
{
public:
A(){cout<<"(1)"<<endl;}
A(const A& s){cout<<"(2)"<<endl;}
~A(){cout<<"(3)"<<endl;}

virtual void f1(){cout<<"(4)"<<endl;}
virtual void f2(){cout<<"(5)"<<endl;}
void f3(){cout<<"(6)"<<endl;}
};

class B: public A
{
public:
B(){cout<<"(7)"<<endl;}
B(const B& a):A(a){cout<<"(8)"<<endl;}
~B(){cout<<"(9)"<<endl;}

void f3(int i){cout<<"(10)"<<endl;}
};

class C: public B
{
public:
C(){cout<<"(11)"<<endl;}
~C(){cout<<"(12)"<<endl;}

void f1(){cout<<"(13)"<<endl;}
void f2(){cout<<"(14)"<<endl; B::f1();}
};

int main()
{
A obj=B();
B* ptr1=new C();
ptr1->f2();
B* ptr2=new B();
ptr2->f2();
delete ptr1;
delete ptr2;
return 0;
}```
I ran the code and this is the result I get:

Code:
```(1)
(7)
(2) //flag 1
(9) //flag 2
(3) //flag 2
(1)
(7)
(11)
(14)
(4) //flag 3
(1)
(7)
(5) //flag 4
(9)
(3)
(9)
(3)
(3) //flag 5```
there are few thing that are'nt clear enough for me,
I flagged each one, and I would really appreciate some clarifications:
flag 1: why is A's C.Ctor called here, right after A and B Ctors?
flag 2: why are these destructors called? which object was destroyed here?
flag 3: how come A's f1() is called when clearly there is no f1() in B?
you might say that B inherits f1() from A, but B does not exist, there is no B object, only C exist here.
flag 4: ptr1 points to a C object, so how is it that C's destructor is not called here right after (5), but rather B's and A's? (is it because B's destructor isn't virtual?)
flag 5: that bothers me the most: where did that last (3) come from?
we have ptr2 pointing to a B object, which was just destroyed in line 42, therefor B's and A's destructors are called, but why is A's destructor called again?

any help would be greatly appreciated,

2. flag 1: why is A's C.Ctor called here, right after A and B Ctors?
A obj=B(); creates a temporary B object but only the A part is used to initialize obj.
flag 2: why are these destructors called? which object was destroyed here?
The temporary B
flag 3: how come A's f1() is called when clearly there is no f1() in B?
ptr1->f2(); The C object calls f2() and that calls B::f1(); f1 is virtual -> A::f1()
flag 4: ptr1 points to a C object, so how is it that C's destructor is not called here right after (5), but rather B's and A's? (is it because B's destructor isn't virtual?)
Yes
flag 5: that bothers me the most: where did that last (3) come from?
The A obj is destructed
Kurt

3. Thanks, Zuk.

4. one think to keep in mind, when making classes with inheritance, is to make all of your destructors virtual. this helps to ensure that all of the appropriate destructors get called.

5. O_o

... and strongly consider making copy constructors and assignment operators `private' to prevent the kind of slicing Zuk describes.

Soma

6. Originally Posted by Elkvis
one think to keep in mind, when making classes with inheritance, is to make all of your destructors virtual. this helps to ensure that all of the appropriate destructors get called.
Originally Posted by phantomotap
O_o

... and strongly consider making copy constructors and assignment operators `private' to prevent the kind of slicing Zuk describes.

Soma

yep.
the followed question was: "What is this code missing?"