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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:07:14+00:00 2026-06-15T16:07:14+00:00

Sorry if this answer is already on this site, but I’ve looked all over

  • 0

Sorry if this answer is already on this site, but I’ve looked all over and couldn’t find any solutions to my issue. It pertains to same-name functions from inherited classes. Here is my code:

class A
{
public:
    int foo(int c) { c = c+1; return c; };
};

class B : public A
{
public:
    int foo(int c) { c = c-1; return c; };
};

int main()
{
    A array[2];
    array[0] = A item1;
    array[1] = B item2;
    for (int n=0;n<2;n++)
    {
        cout << array[n].foo(10) << endl;
    }
    return 0;
}

I would expect an output of:

11    // foo() from A class  [10 + 1 = 11]
9     // foo() from B class  [10 - 1 = 9 ]

But instead I get

11
11

From testing this out, I have found that the foo() function in the B class does not get called within the for-loop. Instead, the foo() function in the A class is called, even on the B object at array[1].

Is this because I have defined the array as containing objects of the A class only? If so, is there a way I can have the foo() function from the B class be called on the second object within that for-loop?

Thank you in advance for any help!

  • 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-15T16:07:15+00:00Added an answer on June 15, 2026 at 4:07 pm

    I’ll forget that array[0] = A item1; isn’t valid C++ and just assume that you’re assigning an object of type A to array[0] and an object of type B to array[1]. Okay, so you have two problems.

    The first is known as object slicing. When you copy an object of type B to an object of type A, you only copy the A part of that object. So what you have in array[1] is not a B at all, it’s just an A. If you want polymorphism (which you do), then you need to use either pointers or references which provide polymorphic behaviour. That means make your array an A* array[2]; and do array[0] = &item1; array[1] = &item2;.

    Now, when you call a function on a pointer to A that points to a B it will still only call As foo member function. Why? Because by default, the function will be looked up on the static type of the object. That static type is A. If you want to tell the compiler to look up your function on the dynamic type of your object – the true type of your object, which is B – you need to make that member function virtual. So in A, do:

    virtual int foo(int c) { c = c+1; return c; };
    

    Now when your compiler see that you’re calling foo on an A*, it’ll see that it’s virtual and say “Oh okay, I should look up this function dynamically” and it’ll find B‘s implementation of foo.

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

Sidebar

Related Questions

Sorry if this question is answered already, but I didn't find a suitable answer.
I'm sorry if this is a foolish question, but I couldn't find answer with
First sorry if this was asked already but i cannot find an answer for
Sorry if this question has already been answered but I can't find an answer.
Sorry if this is a duplicate - couldn't find an answer by searching. How
Sorry if this question is very easy, but I can't find the answer anywhere.
Sorry that I can't find the answer through google - But I know this
Sorry if it was already asked before, but I really couldn't find a clear
I am sorry if this is a duplicate, but I cannot find the answer
Sorry if something similar has been posted here already, but I couldn't really find

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.