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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:16:35+00:00 2026-06-10T07:16:35+00:00

This is kind of a specific question that I had not been able to

  • 0

This is kind of a specific question that I had not been able to find a solution to for quite a while. I have this code:

#include <iostream>

using namespace std;

class Mammal
{
  public:
  Mammal() {cout << "Mammal Constructor\n";}
  virtual ~Mammal() {cout << "Mammal Destructor\n";}
  virtual void Run() {cout << "Mammal Ran One Space\n";}

  protected:
  int mammalDistance;
};

class Horse : public Mammal
{
    public:
    Horse() {cout << "Horse Constructor\n";}
    ~Horse() {cout << "Horse Destructor\n";}
    void Run() {cout << "Horse Ran One Space\n";}
    void Run(int distance) {horseDistance = distance;
                            cout << "Horse Ran " << horseDistance << " Spaces\n";}

    protected:
    int horseDistance;
};

int main()
{
    Mammal *pHorse = new Horse;
    pHorse->Run(5);
    delete pHorse;
    return 0;
}

Now this code works if I take the void Run(int horseDistance) and move it up into Mammal but I wanted to know if there was a way to keep it in horse without it remaining hidden.

Edit: I mean it compiles and works as intended if I take the function that accepts input and move it up into Mammal instead of having it within the Horse class like it is currently.
Yes I would like it if it could change the value of horseDistance when it is being passed in.
Edit: O I get what your saying. I edited the code.

  • 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-10T07:16:36+00:00Added an answer on June 10, 2026 at 7:16 am

    You have two options. One is to keep track of the fact that your pointer is a horse:

    int main()
    {
        Horse *pHorse = new Horse;
        pHorse->Run(5);
        delete pHorse;
        return 0;
    }
    

    The other is to declare a virtual function in Mammal:

    virtual void Run(int mammalDistance) = 0;
    

    I’d pick option #1 if horses were the only things running specific distances, option #2 otherwise.

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

Sidebar

Related Questions

This question is kind of related to another question but I have a specific
This is kind of a 'double' question that might have a single answer. I'm
This is kind of more generic question, isn't language-specific. More about idea and algorithm
As a kind of follow on to this question , what specific services are
I have this kind of question. In my form, i got this as a
This is similar to this question , but kind of the opposite. I have
I half realize that this question is probably asked already, but I'm not familiar
I have a stored procedure that goes something like this (pseudo code) storedprocedure param1,
Please read Update1 first! This is SSIS specific question. I have the following tasks:
I know this is an old question and must have been answered hundred times

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.