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

  • SEARCH
  • Home
  • 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 7513813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:18:05+00:00 2026-05-30T00:18:05+00:00

Having gotten comfortable with the idea of basic classes and encapsulation, I’ve launched myself

  • 0

Having gotten comfortable with the idea of basic classes and encapsulation, I’ve launched myself towards understanding polymorphism, but I can’t quite figure out how to make it work. Many of the examples I’ve searched through come across as really, really forced (classes Foo and Bar are just too abstract for me to see the utility), but here’s how I understand the basic concept: you write a base class, derive a whole bunch of other things from it that change what the base methods do (but not what they “are”), then you can write general functions to accept and process any of the derived classes because you’ve somewhat standardized their appearance. With that premise, I’ve tried to implement the basic Animal->cat/dog hierarchy like so:

class Animal {
  public:
    virtual void speak() = 0;
};

class Dog : public Animal {
  public:
    void speak() {cout << "Bark bark!" << endl;}
};

class Cat : public Animal {
  public:
    void speak() {cout << "Meow!" << endl;}
};

void speakTo(Animal animal) {
    animal.speak();
}

where speakTo can take can take a general kind of animal and make it, well, speak. But as I understand it, this doesn’t work because I can’t instantiate Animal (specifically in the function argument). I ask, then, do I understand the basic utility of polymorphism, and how can I really do what I’ve tried to do?

  • 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-30T00:18:06+00:00Added an answer on May 30, 2026 at 12:18 am

    You cannot pass an Animal object to the derived class function because you cannot create an object of Animal class et all, it is an Abstract class.
    If an class contains atleast one pure virtual function(speak()) then the class becomes an Abstract class and you cannot create any objects of it. However, You can create pointers or references and pass them to it.
    You can pass an Animal pointer or reference to the method.

    void speakTo(Animal* animal) 
    {
        animal->speak();
    }
    
    int main()
    {
        Animal *ptr = new Dog();
        speakTo(ptr);
    
        delete ptr;   //Don't Forget to do this whenever you use new()
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a great time learning Python, but I've just gotten a bit stuck
I have gotten rid of a circular dependence but am still having issues with
Having recently gotten into test driven development I am using the Nunit test runner
Having successfully gotten a sample program working, I'm now starting to do Real Work
Having integrated merb_auth_password_slice as per the README, I can successfully login as redirect_after_login is
Having worked with Spring MVC for JAVA, I've gotten used to having @Transactional behaviour
I've recently just gotten into programming with ruby on rails, and I've been having
I am having another problem with my table and checkboxes. I ahve gotten my
I'm having trouble updating my rails app on heroku. I've gotten my app up
I'm having trouble describing this issue, which is probably why I can't find the

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.