Solved

Virtual functions

Posted on 1998-07-22
6
204 Views
Last Modified: 2013-11-18
#include <iostream.h>

class Animal
{
      public:
      virtual char* speak(int) {return "Silence";}
};
class Cat:public Animal
{
      public:
      virtual char* speak(int) {return "Meow";}
};
class Dog:public Animal
{
      public:
      virtual char* speak(short) {return "Bow-wow";}
};

int main()
{
      Animal *p = new Dog;
      cout << p->speak( 0 ) << endl;
      return 0;
}

The speech of dogs is supposed to be 'Bow-wow' but instead the program prints 'silence'.Why did the dog not bark?
0
Comment
Question by:sreejesh
6 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 20 total points
Comment Utility
Because of different signatures.
Explanation comming...
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
In C, a function is known by its name.  In C++, a function is known by its "signature", which is the name and the types of the arguments.  Therefore f(int) and f(short) are two different functions.

Now, for your example:

        Animal *p = new Dog;

Although p points to Dog, its static type is Animal*.

        /* ... */    p->speak( 0 )    /* ... */

The compiler does not know what actual object p will point to at run-time.  So, it generates code to call the virtual function whose signature is known at compile-time, based on the information it knows about p.

        class Animal
        {
        public:
            virtual char* speak(int) {return "Silence";}
        };

Since p is a pointer to Animal, the compiler will (virtually) call the speak(int) function.

        class Dog:public Animal
        {
        public:
            virtual char* speak(short) {return "Bow-wow";}
        };

p points to a Dog object.  However, the Dog object does not define a speak(int) finction (the speak(short) function has a different signature and thus considered a different function).  Therefore, the inherited speak(int) function of the superclass (Animal) is called.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
By the way, A nice thing about the Borland C++ compiler  (there aren't many) is that it warns you when you do this.  Most compilers don't warn you since this is legal C++.  However, almost no one every really wants to do this on purpose, so Borland has enough sense to warn you.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:danny_pav
Comment Utility
this looks like one of those PC-lint bug of the month ads.
0
 

Author Comment

by:sreejesh
Comment Utility
Alexo, You pointed out the correct answer. Congrats ! and Thank You. Same to nietod and danny_pav also.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
>> this looks like one of those PC-lint bug of the month ads.
Bingo!  No wonder the "Bow-wow" looked familiar...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now