Solved

Virtual functions

Posted on 1998-07-22
6
207 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is an update and follow-up of my previous article:   Storage 101: common concepts in the IT enterprise storage This time, I expand on more frequently used storage concepts.
VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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