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 6612013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:42+00:00 2026-05-25T20:02:42+00:00

Can anyone explain the output of the following code? #include <iostream> #include <string> class

  • 0

Can anyone explain the output of the following code?

#include <iostream>
#include <string>
class Animal
{
public:
    Animal(const std::string & name) : _name(name) { }
    ~Animal() { }
    virtual void printMessage() const
    {
        std::cout << "Hello, I'm " << _name << std::endl;
    }

private:
    std::string _name;
    // other operators and stuff
};

class Cow : public Animal
{
public:
    Cow(const std::string & name) : Animal(name) { }
    ~Cow() { }
    virtual void printMessage()
    {
        Animal::printMessage();
        std::cout << "and moo " << std::endl;
    }
};

int main() {
    Cow cow("bill");
    Animal * animal = &cow;
    cow.printMessage();
    animal->printMessage();
}

The output is

Hello, I’m bill
and moo
Hello, I’m bill

I don’t understand why. The pointer animal points at an object of type Cow. printMessage is a virtual function. Why isn’t the implementation of the Cow class the one that is called?

  • 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-05-25T20:02:43+00:00Added an answer on May 25, 2026 at 8:02 pm

    Cow isn’t overriding the virtual function from Animal because it has a different signature. What’s actually happening is that Cow is hiding the function in Animal.

    The result of this is that calling printMessage on an Animal will just use the version in Animal, regardless of the one in Cow (it isn’t overriding it), but calling it from Cow will use the one in Cow (because it hides the one from Animal).

    To fix this, remove the const in Animal, or add the const in Cow.

    In C++ 2011, you will be able to use the override keyword to avoid subtle traps like this:

    class Cow : public Animal 
    { 
    public: 
        Cow(const std::string & name) : Animal(name) { } 
        ~Cow() { } 
        virtual void printMessage() override
        { 
            Animal::printMessage(); 
            std::cout << "and moo " << std::endl; 
        } 
    };
    

    Notice the added override after printMessage(). This will cause the compiler to emit an error if printMessage doesn’t actually override a base class version. In this case, you would get the error.

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

Sidebar

Related Questions

Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain the behaviour of the below code. The output of the below
Can anyone explain why the following code outputs what it does: char c =
I wanted to see if anyone can explain why the following code works with
Can anyone explain exactly why Method 1 in the following code does not alter
Can anyone explain the output of this program and how I can fix it?
Here's a puzzler: can anyone explain why cd fails when the output is redirected
Can any one explain why the output of this code is only 'hello' and
Anyone can explain why the lines below appear in the output console at runtime
Given the following code: #include <iostream> struct implicit_t { implicit_t(int x) : x_m(x) {

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.