Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Isuue in virtual function

This is a discussion on Isuue in virtual function within the C and C++ forums, part of the Software Development category; Is output is correct or not #include<iostream.h> class a { public: a() { cout<<"class a constructor"<<endl; } ~a() { cout<<"class ~a distructor"<<endl; } }; class b ublic a { public: ...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 03-05-2008
Junior Member
 
Join Date: Nov 2007
Location: Bangalore
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jinendrashankar is on a distinguished road
Isuue in virtual function

Is output is correct or not

#include<iostream.h>
class a
{
public: a()
{
cout<<"class a constructor"<<endl;
}
~a()
{
cout<<"class ~a distructor"<<endl;
}
};

class bublic a
{
public: b()
{
cout<<"class b constructor"<<endl;
}
~b()
{
cout<<"class ~b distructor"<<endl;
}
};

int main()
{
//b b;
cout<<"inside main"<<endl;
b *ptr_a=new b;
delete ptr_a;
//cout<<"<-------->"<<endl;
return 0;
}

inside main class a constructor class b constructor class ~b distructor class ~a distructor

Last edited by jinendrashankar; 03-05-2008 at 06:53 AM.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008
Junior Member
 
Join Date: Mar 2008
Location: chennai
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
gviswa.blue is on a distinguished road
Smile Re: Isuue in virtual function

Yes, when you try to delete the derived class, it also call the base calss destructor
Reply With Quote
  #3 (permalink)  
Old 03-13-2008
Junior Member
 
Join Date: Mar 2008
Location: Bangalore
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ashish.mahalka is on a distinguished road
Re: Isuue in virtual function

The output which u have mentioned will be observed only when the base class destructor is made virtual. When u use a base class ptr to point to a derived class object, the base class destructor function should be made virtual in order to get the derived class destructor called.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
virtual function abhijithvardhan C and C++ 6 05-08-2008 06:19 AM
Virtual function Output jitudash C and C++ 0 10-16-2007 06:08 AM
C++ Pure Virtual Function and Virtual Base Class Lokesh M C and C++ 0 10-04-2007 07:29 AM
A derivation inherits both a virtual and non-virtual instance Geek_Guest OOPS 1 07-18-2007 05:51 AM
Diff in using Virtual Obj wiz and GUI Spy bvani Test Cases 1 05-29-2006 11:08 AM


All times are GMT -4. The time now is 10:47 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved