Solved

Virtual functions

Posted on 1998-07-22
6
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Accepted Solution

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

Expert Comment

by:alexo
ID: 1168263
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
ID: 1168264
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 3

Expert Comment

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

Author Comment

by:sreejesh
ID: 1168266
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
ID: 1168267
>> this looks like one of those PC-lint bug of the month ads.
Bingo!  No wonder the "Bow-wow" looked familiar...
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

734 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