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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:28:10+00:00 2026-05-21T07:28:10+00:00

Bar and Box are derived classes of Foo and Foo has a virtual function

  • 0

Bar and Box are derived classes of Foo and Foo has a virtual function F() and Bar and Box both have function F(). From what I understand, polymorphism correctly allows Bar.F() instead of Box.F() or Box.F() instead of Bar.F() to override Foo.F() using some runtime routine without knowing whether your object is a Bar or a Box. It’s something like this:

Foo * boxxy = new Box;
boxxy->F();

The last line will call the right F() (in this case, Box.F()) independent of whether boxxy is a Box or a Bar or a Foo (in which case the implementation of the virtual Foo.F() is called).

Do I understand this right? And what changes if boxxy is a Box pointer? And what happens if the derived class doesn’t have an override for F()? Lastly, to avoid implementing a function for a base class but still allow polymorphism, do you just write an empty function body and declare it virtual? Thanks.

  • 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-21T07:28:11+00:00Added an answer on May 21, 2026 at 7:28 am

    Nearly right – consider this inheritance tree:

          Foo
         /   \
       Bar   Box
    

    If you now make a pointer like this:

    Bar* barry = new Box();
    

    You’ll get a nice compiler error, since a Box can’t be converted to a Bar. 🙂
    So it’s only Foo<->Bar and Foo<->Box, never Bar<->Box.
    Next, when boxxy is a Box pointer, it will only ever call the Box::F function, if it is provided.
    And last, to force subclasses to implement a certain function, you declare it pure virtual, like this:

    virtual void Func() = 0;
    //   note this --- ^^^^
    

    Now subclasses (in this case Bar and Box), must implement Func, else they will fail to compile.

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

Sidebar

Related Questions

I have a value like this: Foo Bar Another Value something else What regex
I want to have a small text box with a scroll bar that will
I have two public websites (foo.com and bar.com) that are pointed to a hardware
I want to create a simple box with a header bar containing a title
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
For example: me$ FOO=BAR * BAR me$ echo $FOO BAR file1 file2 file3 file4
Assuming a class called Bar in a namespace called foo , which syntax do
If I want to call Bar() instead of Foo() , does Bar() return me
I have a form with a progress bar and a cancel button which is
I have a JFrame with a menu bar and a canvas covering all 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.