virtual functions in virtual base class

Discussion in 'C++' started by sreelakshmi.rajula@gmail.com, Aug 21, 2006.

  1. Guest

    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 ;

    }
    , Aug 21, 2006
    #1
    1. Advertising

  2. red floyd Guest

    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.com/c++-faq-lite/how-to-post.html#faq-5.8
    red floyd, Aug 21, 2006
    #2
    1. Advertising

  3. Guest

    > 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.
    , Aug 21, 2006
    #3
  4. Guest

    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?
    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.
    , Aug 21, 2006
    #4
  5. Guest

    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.
    , Aug 22, 2006
    #5
  6. manish Guest

    use the following to print age
    cout<<animal->GetAge();
    in the main( )
    instead of
    animal->GetAge();
    manish, Aug 22, 2006
    #6
  7. Ian Collins Guest

    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.
    Ian Collins, Aug 22, 2006
    #7
  8. Richard Guest

    red floyd <> writes:

    > 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.com/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.
    Richard, Aug 28, 2006
    #8
    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. Michael Winter
    Replies:
    9
    Views:
    607
    Michael Winter
    Sep 24, 2003
  2. tomek
    Replies:
    2
    Views:
    664
    Andrey Tarasevich
    Dec 1, 2003
  3. tomek
    Replies:
    3
    Views:
    731
    David Fisher
    Nov 30, 2003
  4. Amy Matlock
    Replies:
    4
    Views:
    350
    Amy Matlock
    Nov 10, 2005
  5. John Goche
    Replies:
    10
    Views:
    720
    Marcus Kwok
    Dec 8, 2006
Loading...

Share This Page