C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

virtual functions in virtual base class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon Aug 21, 2006 9:10 am    Post subject: virtual functions in virtual base class Reply with quote



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 ;

}
Back to top
red floyd
Guest





PostPosted: Mon Aug 21, 2006 9:10 am    Post subject: Re: virtual functions in virtual base class Reply with quote



sreelakshmi.rajula (AT) gmail (DOT) com wrote:
Quote:
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
Back to top
Guest






PostPosted: Mon Aug 21, 2006 9:10 am    Post subject: Re: virtual functions in virtual base class Reply with quote



Quote:
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.
Back to top
manish
Guest





PostPosted: Tue Aug 22, 2006 9:10 am    Post subject: Re: virtual functions in virtual base class Reply with quote

use the following to print age
cout<<animal->GetAge();
in the main( )
instead of
animal->GetAge();
Back to top
Ian Collins
Guest





PostPosted: Tue Aug 22, 2006 9:11 am    Post subject: Re: virtual functions in virtual base class Reply with quote

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

What are you replying to?


--
Ian Collins.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.