What the diff between a virtual method and an inheirited method

Discussion in 'C++' started by jlopes, Nov 19, 2004.

  1. jlopes

    jlopes Guest

    There seems to bet no diff between a vitual method and an inheirited method.

    class A_Base
    {
    public:
    virtual void filter(){ /* some code */ }
    };

    class D_of_A_Base : public A_Base
    {
    public:
    void filter(){ /* could have used the filter of the base class */ }
    };

    class AnotherBase
    {
    public:
    void filter(){ /* some code */ }
    };

    class D_of_A_Base : public AnotherBase
    {
    public:
    filter(){ /* could have used the filter of the base class */ }
    };
    jlopes, Nov 19, 2004
    #1
    1. Advertising

  2. jlopes wrote:
    > There seems to bet no diff between a vitual method and an inheirited method.
    >
    > class A_Base
    > {
    > public:
    > virtual void filter(){ /* some code */ }
    > };
    >
    > class D_of_A_Base : public A_Base
    > {
    > public:
    > void filter(){ /* could have used the filter of the base class */ }
    > };
    >
    > class AnotherBase
    > {
    > public:
    > void filter(){ /* some code */ }
    > };
    >
    > class D_of_A_Base : public AnotherBase
    > {
    > public:
    > filter(){ /* could have used the filter of the base class */ }
    > };


    I didn't get what you mean by no difference. Do you understand what are
    virtual functions? Do you understand polymorphism?

    As far as C++ is considered they are chalk and cheese.
    I guess in Java they are the same i.e., inherited functions are also
    virtual. Correct me on this if I am wrong.

    I will advise you to read the chapters on virtual function in Stroustrup
    or Lippman's book.

    --
    Surendra Singhi

    www.public.asu.edu/~sksinghi
    Surendra Singhi, Nov 19, 2004
    #2
    1. Advertising

  3. jlopes

    Cheng Mo Guest

    Another question pops up in my mind about virtual funtion.
    Call to inline member function will be replaced by the function body
    during compilation; and virtual function is reference by VTABLE. So,
    what about virtual inline member function?

    class Foo
    {
    public:
    virtual static print(const std::string &str)
    {
    std::cout << str << std::endl;
    }
    }

    Is call to print replaced by function body? or "inline" is ignored if
    the function is virtual?




    Surendra Singhi wrote:
    > jlopes wrote:
    >
    >> There seems to bet no diff between a vitual method and an inheirited
    >> method.
    >> class A_Base
    >> {
    >> public:
    >> virtual void filter(){ /* some code */ }
    >> };
    >>
    >> class D_of_A_Base : public A_Base
    >> {
    >> public:
    >> void filter(){ /* could have used the filter of the base class */ }
    >> };
    >>
    >> class AnotherBase
    >> {
    >> public:
    >> void filter(){ /* some code */ }
    >> };
    >>
    >> class D_of_A_Base : public AnotherBase
    >> {
    >> public:
    >> filter(){ /* could have used the filter of the base class */ }
    >> };

    >
    >
    > I didn't get what you mean by no difference. Do you understand what are
    > virtual functions? Do you understand polymorphism?
    >
    > As far as C++ is considered they are chalk and cheese.
    > I guess in Java they are the same i.e., inherited functions are also
    > virtual. Correct me on this if I am wrong.
    >
    > I will advise you to read the chapters on virtual function in Stroustrup
    > or Lippman's book.
    >
    Cheng Mo, Nov 19, 2004
    #3
  4. > Another question pops up in my mind about virtual funtion.
    > Call to inline member function will be replaced by the function body
    > during compilation; and virtual function is reference by VTABLE. So,
    > what about virtual inline member function?
    >
    > class Foo
    > {
    > public:
    > virtual static print(const std::string &str)
    > {
    > std::cout << str << std::endl;
    > }
    > }
    >
    > Is call to print replaced by function body? or "inline" is ignored if
    > the function is virtual?


    I guess you have made a mistake in the function above and written static
    instead of inline.
    However if the compiler knows where the object originates from, then it
    might be sure that the virtual function is not overridden and then it can
    make the function inline. If the origin is unsure (object is a parameter to
    a function), then it must call the function via VTABLE.

    Niels Dybdahl
    Niels Dybdahl, Nov 19, 2004
    #4
  5. jlopes

    jlopes Guest

    Surendra Singhi <> wrote in message news:<cnk1is$p46$>...
    > jlopes wrote:
    > > There seems to bet no diff between a vitual method and an inheirited method.
    > >
    > > class A_Base
    > > {
    > > public:
    > > virtual void filter(){ /* some code */ }
    > > };
    > >
    > > class D_of_A_Base : public A_Base
    > > {
    > > public:
    > > void filter(){ /* could have used the filter of the base class */ }
    > > };
    > >
    > > class AnotherBase
    > > {
    > > public:
    > > void filter(){ /* some code */ }
    > > };
    > >
    > > class D_of_A_Base : public AnotherBase
    > > {
    > > public:
    > > filter(){ /* could have used the filter of the base class */ }
    > > };

    >
    > I didn't get what you mean by no difference. Do you understand what are
    > virtual functions? Do you understand polymorphism?
    >
    > As far as C++ is considered they are chalk and cheese.
    > I guess in Java they are the same i.e., inherited functions are also
    > virtual. Correct me on this if I am wrong.
    >
    > I will advise you to read the chapters on virtual function in Stroustrup
    > or Lippman's book.


    Let me refine my question. Given a set of objects, each with common
    behavior. I then pull out that common behavior into a single object.
    That becomes the base of the set of objects. The set need not use
    polymorphism, I could make the common methods virtual or not. Since
    the common methods are inherited I need only use them. Having read
    Stroustrup 3rd ed, he presents vitual methods in their pure form. To
    say the a Base class method is virtual and its children in herit that
    method is no different than simplly defining the method.

    So my qusetion:
    If one removes the concept of polymorphism and pure virtual
    methods. To inherit a method from a base class defined as virtual is
    no different than a defined method in that base class.
    jlopes, Nov 19, 2004
    #5
  6. (jlopes) wrote:

    > So my qusetion:
    > If one removes the concept of polymorphism and pure virtual
    > methods. To inherit a method from a base class defined as virtual is
    > no different than a defined method in that base class.


    I think the whole point of using virtual functions is
    to allow the derived classes to define their own unique
    behaviour for that function.

    Say you want to call a function through a single pointer to
    the base class which at runtime may arbitrarily point to any
    derived object. If the function is virtual, it will use the
    appropriate code for that derived class. Extremely useful.
    le ténébreux, Nov 19, 2004
    #6
    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. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    502
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    621
    Infant Newbie
    Dec 5, 2003
  3. jlopes
    Replies:
    7
    Views:
    410
    jlopes
    Nov 19, 2004
  4. Santa
    Replies:
    1
    Views:
    1,062
    Mark A. Odell
    Jul 17, 2003
  5. Sally
    Replies:
    0
    Views:
    104
    Sally
    Apr 13, 2004
Loading...

Share This Page