virtual function and abstruct class

Hi
I have few qustions that i want please get an answer

1-alway when i want to use on polymorphisem i will must use on virtual distructor on the "father" class?
Because ,if no, when the father point on son the distructor will apllay only on father.
So alway i must do it?

2.always i heard that even we do low casting to father so the son can point on him
The son cant acsses to his "father part" and only to "son part"
But i tried snd it ok
It's wrong only when i use on new allocate
Why is that?
Look here on the try
http://up415.siz.co.il/up1/mzqmimzbddj0.jpeg

3-why when i use on virtual distructor the distructor of father apllay too?
On virtual ,only the function on son(if the father point in son) need to work

4-on abstract class i can define virtual distructor??

Thank a lot
Last edited on
1) Yes, you should declare destructor virtual if you intend to use child classes through pointer to parent.

2) Your code is terribly wrong. You are trying to cast pointer to invalid class. Do not use c-style casts. It often does wrong, dangerous or illegal thing without further confirmation.
Your example just happened to work as your classes are trivial and does not require to generate code which would actually manipulate classes.

3) Destructors are executed in order from most derived class to base class. You need to make it virtual, so destructors of child parts were executed too.

4) Yes, and you probably should as ABC are usually used for using child classes though pointers to base.
Topic archived. No new replies allowed.