Results 1 to 3 of 3

Thread: Isuue in virtual function

  1. #1

    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.

  2. #2
    Junior Member
    Join Date
    Mar 2008
    Answers
    1

    Smile Re: Isuue in virtual function

    Yes, when you try to delete the derived class, it also call the base calss destructor


  3. #3
    Junior Member
    Join Date
    Mar 2008
    Answers
    1

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact