polymorphism

Discussion in 'C++' started by subramanian, Jan 7, 2007.

  1. subramanian

    subramanian Guest

    Consider the program:

    #include <iostream>
    #include <string>

    class A {
    public:
    virtual void print(void) const;
    };

    void A::print(void) const
    {
    std::cout << "A::print()\n";
    return;
    }

    class B : public A {
    public:
    void print(void) const;
    };

    void B::print(void) const
    {
    std::cout << "B::print()\n";
    return;
    }

    class C : public B {
    public:
    void print(void) const;
    };

    void C::print(void) const
    {
    std::cout << "C::print()\n";
    return;
    }

    int main(void)
    {
    C c;
    A *pa = &c;
    pa->print();

    return 0;
    }

    If I run this program the output is:
    C::print()

    Question:
    A::print() is declared virtual. Since B::print()const is not virtual,
    how does pa->print() calls C::print() ?
     
    subramanian, Jan 7, 2007
    #1
    1. Advertising

  2. subramanian

    red floyd Guest

    subramanian wrote:
    > Consider the program:
    >
    > #include <iostream>
    > #include <string>
    >
    > class A {
    > public:
    > virtual void print(void) const;
    > };
    >
    > void A::print(void) const
    > {
    > std::cout << "A::print()\n";
    > return;
    > }
    >
    > class B : public A {
    > public:
    > void print(void) const;
    > };
    >
    > void B::print(void) const
    > {
    > std::cout << "B::print()\n";
    > return;
    > }
    >
    > class C : public B {
    > public:
    > void print(void) const;
    > };
    >
    > void C::print(void) const
    > {
    > std::cout << "C::print()\n";
    > return;
    > }
    >
    > int main(void)
    > {
    > C c;
    > A *pa = &c;
    > pa->print();
    >
    > return 0;
    > }
    >
    > If I run this program the output is:
    > C::print()
    >
    > Question:
    > A::print() is declared virtual. Since B::print()const is not virtual,
    > how does pa->print() calls C::print() ?


    You're proceeding from a faulty assumption. If a method is virtual in
    the base, it's virtual in all the derived classes. B::print() const
    *IS* virtual.
     
    red floyd, Jan 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Lawrence

    Polymorphism in GridView (ASP.Net 2.0)

    Kevin Lawrence, Feb 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    847
    Kevin Lawrence
    Feb 16, 2006
  2. Sony Antony

    Polymorphism in xml ??

    Sony Antony, Aug 26, 2003, in forum: Java
    Replies:
    0
    Views:
    560
    Sony Antony
    Aug 26, 2003
  3. Osaman
    Replies:
    2
    Views:
    449
    John C. Bollinger
    Jan 5, 2004
  4. Tony Morris

    polymorphism

    Tony Morris, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    728
    sonali
    Mar 7, 2004
  5. Krivenok Dmitry
    Replies:
    13
    Views:
    1,445
    Axter
    Jun 1, 2006
Loading...

Share This Page