For the main question, the base class destructor will be called and the
output of the program will be: "baseClass Des called".

This is because, when  delete bPtr; is called, the base class destructor
will be called as bPtr is of type baseClass.
And since baseClass destructor is not decalred as virtual, so the type of
the object that bPtr points to does not come into picture.
In other words, the base part of memory held by  derivedClass2 object will
only be destructed here.

@Prem Krishna Chettri's question: In this case also only the baseClass's
destructor will be called and hence we will have two lines output as
"baseClass Des called"
"baseClass Des called"

This is why it is always advisable to make the destructor as virtual.


Amit

On Wed, Dec 7, 2011 at 3:27 PM, Prem Krishna Chettri <hprem...@gmail.com>wrote:

> The Output would be only  the base class object destructor would be called
> so output :-
>
> derivedClass2 Des called
>
> Now why ..
>
>   It is no more than just a base parent concept here, so base pointer
> capable if holding its derived object address get's derived object as new
> derivedClass2() creates only derived object. However,  by deleting that
> pointer would results in destructing of this object which is the object of
> the derived class type "derivedClass2".
>
>
> Well,  I Guess what would be more interesting if you have a main something
> like this :-
>
> int main() {
>
> baseClass *aPtr = new derivedClass1();
>
> baseClass *bPtr = new derivedClass2();
>  delete aPtr;
> delete bPtr;
> }
>
>
> On Wed, Dec 7, 2011 at 2:51 PM, deepak kumar <deepak.kuma...@gmail.com>wrote:
>
>> Hi All,
>>         I am sharing  with you all a problem . The problem is as follow
>>
>>
>> class baseClass {
>> public :
>>       ~baseClass() { cout<< "baseClass Des called"<< endl; }
>> };
>>
>> /*Derived class*/
>>
>> class derivedClass1 : public baseClass {
>> public:
>>     *virtual *~derivedClass1() {cout <<"derivedClass1 Des called"<<
>> endl;}
>> };
>>
>> /*Derived class*/
>>
>> class derivedClass2 : public baseClass {
>> public:
>>     ~derivedClass2() {cout <<"derivedClass2 Des called"<< endl;}
>> };
>>
>>
>> int main() {
>>
>> baseClass *bPtr = new derivedClass2();
>> delete bPtr;
>> }
>>
>>
>> Can anyone tell me what will be the output and why ?
>> Note: the virtual destructor in derivedClass1.
>>
>>
>> --
>> Thanks and Regards
>> Deepak Kumar
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to