Thread: Need some help with inheritance and polymorphism

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    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,
    thanks in advance.
    Last edited by Absurd; 06-21-2013 at 03:24 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    Thanks, Zuk.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

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

    Soma

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by Elkvis View Post
    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.
    Quote Originally Posted by phantomotap View Post
    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?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance or polymorphism
    By asysyah in forum C++ Programming
    Replies: 4
    Last Post: 10-26-2010, 11:22 AM
  2. polymorphism and inheritance
    By stewie griffin in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2009, 02:45 AM
  3. Polymorphism, inheritance and containers
    By musicalhamster in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2007, 10:23 AM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. inheritance and polymorphism uses...
    By tetra in forum C++ Programming
    Replies: 5
    Last Post: 05-06-2003, 05:30 PM