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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:08:05+00:00 2026-06-12T12:08:05+00:00

Possible Duplicate: virtual function default arguments behaviour class Human{ virtual void print(int height =72){

  • 0

Possible Duplicate:
virtual function default arguments behaviour

class Human{ virtual void print(int height =72){ cout << "Human: " << height << endl; }};
class Child:public Human{ void print(int height =48){ cout << "Child: " << height << endl; }};

int main()
{
 Human * brother = new Child();
 brother->print();
}

Result is this

Child: 72

Why it gets default parameter from base class and function definition from derived class? Expecting an explanation associated with C++ standers.

  • 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-12T12:08:06+00:00Added an answer on June 12, 2026 at 12:08 pm

    The parameters passed to a function are decided by the code that calls the function. The behavior of the function, once called, is up to the class. If you need this behavior, create a virtual function that takes no parameters.

    Getting the default parameter of the derived function would be impossible because the calling code can’t even necessarily see the prototype for that function.

    Consider:

    void someFunction(Human *h)
    {
        h->print();
    }
    

    This code may have no idea there exists a class derived from Human that has a print function with a default value of 48. And, in any event, it would have no way to know to actually pass that value.

    Long after this code is compiled, it might get linked to code like this:

    class BasketBallPlayer : public Human
    {
        void print(int height = 84);
    }
    
    Human *j = new BasketBallPlayer();
    someFunction(j);
    

    See 8.3.6.10:

    A virtual function call uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.

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

Sidebar

Related Questions

Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicate: pure virtual function and abstract class I have a class and I
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have
Possible Duplicate: c++ virtual function return type I have a simple but confusing question
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
Possible Duplicate: Why C# implements methods as non-virtual by default? I'm speaking primarily about
Possible Duplicate: Why pure virtual function is initialized by 0? In C++, what does
Possible Duplicate: Why C# implements methods as non-virtual by default? It would be much
Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
Possible Duplicate: C# virtual static method Idea: public class Item { public Vector2 Position;

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.