virtual functions in virtual base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sreelakshmi.rajula@gmail.com

    #1

    virtual functions in virtual base class

    Does it allow virtual functions in Virtual base class?
    I am getting errors in the following code.Anything wrong in this code?I
    want to print Eagle's age. Can anybody clarify my doubt?

    class Animal
    {
    public:
    Animal() { age = 0 ;}
    virtual int GetAge() const { return age; }
    private:
    int age;
    };
    class Eagle : virtual public Animal
    {
    public:
    int GetAge() { age = 4 ; return age; }
    private:
    int age ;
    };
    class Lion : virtual public Animal
    {
    public:
    int GetAge() { age = 10 ; return age; }
    private:
    int age ;
    };

    class Griffin : public Eagle ,public Lion
    {
    };
    int main()
    {
    Animal *animal = new Eagle();
    animal->GetAge();
    return 0 ;

    }

  • red floyd

    #2
    Re: virtual functions in virtual base class

    sreelakshmi.raj ula@gmail.com wrote:
    Does it allow virtual functions in Virtual base class?
    I am getting errors in the following code.Anything wrong in this code?I
    want to print Eagle's age. Can anybody clarify my doubt?
    [code redacted]
    What errors are you getting? What were you expecting? Read FAQ 5.8.
    http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8

    Comment

    • amirkam1@yahoo.com

      #3
      Re: virtual functions in virtual base class

      virtual int GetAge() const { return age; }
      This code should not give you any error, unless you remove const from
      the GetAge method in most base class. As you have declared the function
      to be const in base class but not in derived class, it is not
      overriding.

      Also the virtual inheritance does not resolve the error for ambiguous
      functions. You need to override the function in the most derived
      (Griffin) class and call the appropriate base version.

      Comment

      • sreelakshmi.rajula@gmail.com

        #4
        Re: virtual functions in virtual base class

        I am sorry. What you told is correct. Actually when I override GetAge()
        function it'll give errors. But one more is how can I print Eagle's
        age?
        amirkam1@yahoo. com wrote:
        virtual int GetAge() const { return age; }
        This code should not give you any error, unless you remove const from
        the GetAge method in most base class. As you have declared the function
        to be const in base class but not in derived class, it is not
        overriding.
        >
        Also the virtual inheritance does not resolve the error for ambiguous
        functions. You need to override the function in the most derived
        (Griffin) class and call the appropriate base version.

        Comment

        • amirkam1@yahoo.com

          #5
          Re: virtual functions in virtual base class


          sreelakshmi.raj ula@gmail.com wrote:
          I am sorry. What you told is correct. Actually when I override GetAge()
          function it'll give errors. But one more is how can I print Eagle's
          age?
          If the functions are overriden properly, then by means of virtual
          mechanism the call to getAge will be resolved correctly. But in Griffin
          class you will have to override the function and hardcode which version
          of gateAge should be called (Eagle::getAge OR Lion::getAge()) to avoid
          ambiguous error.

          Comment

          • manish

            #6
            Re: virtual functions in virtual base class

            use the following to print age
            cout<<animal->GetAge();
            in the main( )
            instead of
            animal->GetAge();

            Comment

            • Ian Collins

              #7
              Re: virtual functions in virtual base class

              manish wrote:
              use the following to print age
              cout<<animal->GetAge();
              in the main( )
              instead of
              animal->GetAge();
              >
              What are you replying to?

              --
              Ian Collins.

              Comment

              • Richard

                #8
                Re: virtual functions in virtual base class

                red floyd <no.spam@here.d udewrites:
                sreelakshmi.raj ula@gmail.com wrote:
                >Does it allow virtual functions in Virtual base class?
                >I am getting errors in the following code.Anything wrong in this code?I
                >want to print Eagle's age. Can anybody clarify my doubt?
                >[code redacted]
                >
                What errors are you getting? What were you expecting? Read FAQ
                5.8. http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8
                >
                Why dont you just write a bot to post your "OT" responses? It would save
                you a lot of time. In about 20 posts you have added help possibly once :
                in all the rest you are "OT"ing and referring to netiquette
                documents.

                You clearly have a very high opinion of yourself.

                Comment

                Working...