Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:39:15+00:00 2026-06-07T22:39:15+00:00

I was trying to understand virtual functions. Consider the following code, #include <iostream> #include

  • 0

I was trying to understand virtual functions.

Consider the following code,

#include <iostream>
#include <memory>
#include <vector>

class Animal 
{
public:
     virtual void eat() 
    {
        std::cout << "I eat like a generic animal.\n";
    }

};

class Wolf : public Animal 
{
public:
    void eat() 
    {
        std::cout << "I eat like a wolf!\n";
    }
};


int main() 
{

  Animal      a;
  Wolf        w;

  a.eat();
  w.eat();

}

With the virtual keyword I get the output

I eat like a generic animal.
I eat like a wolf!

as it should.

But If I remove the virtual keyword I still get the same output! From my
elementary understanding of virtual functions, without the virtual I should have got the output

I eat like a generic animal.
I eat like a generic animal.

Is there anything elementary here I am missing ?

I am using the g++ compiler on Linux

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:39:16+00:00Added an answer on June 7, 2026 at 10:39 pm

    Polymorphism works by identifying the type of object that an instance actually refers to.

    In your case, your actual animals are as follows:

    Animal      a;  //a is an animal.
    Wolf        w;  //w is a wolf.
    

    So, you’re not using polymorphism at all.

    What you need to do is more like this:

    //Create a couple animal pointers.
    Animal* a;
    Animal* b;
    
    //Create an animal instance and have a point to it.
    a = new Animal();
    
    //Create a wolf instance and have b point to it.
    b = new Wolf();
    
    //Calls Animal::eat as a is an animal.
    a->eat();
    
    //Calls Wolf::eat as a is a wolf.
    b->eat();
    

    Note that you can use pointers or references to achieve this use of polymorphism.

    That is why you should usually pass objects by const-reference when working with class types.

    //Will call Animal::eat or Wolf::eat depending on what animal was created as.
    void foo(const Animal& animal) {
        animal.eat();
    }
    
    //Will always call Animal::eat and never Wolf::eat since this isn't a reference or
    //a pointer.  Will also "slice" a Wolf object.
    void foo(Animal animal) {
        animal.eat();
    }
    

    Note that slicing means it will turn a more derived class (wolf) into a less derived copy of that class (animal) indiscriminately which can be very misleading and unexpected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand virtual memory paging. I have the following code snippet
I am trying some code like this //A.hpp class A{ public: A() {} virtual
I'm trying to understand the function of the virtual keyword in C++ - consider
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
Im trying to understand how class generics work and this bit just doesnt make
I'm trying to understand whay i get an error on this code: (the error
I am trying to understand my embedded linux memory usage. By using the top
Hey so i'm trying to build the following member Functors of class ConcavePolygon ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.