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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:56:47+00:00 2026-05-29T22:56:47+00:00

I was just doing some stuff and wrote this program. I got the following

  • 0

I was just doing some stuff and wrote this program. I got the following output. I know that function resolution happens at run time while binding of the variable takes place during compile time, but what I could not understand was the first output I got (this->n). Can anyone explain this to me?

    # include <iostream>
    # include <stdio.h>
    # include <conio.h>

    using namespace std;

    class A
    {
      int n;      
      public:
      virtual void Fun1(int no=10)
       {
         int n=no;        
         cout<<"A::Fun1() "<<n <<"\n";
       }
    };

    class B :public A
    {
     int n;      
     public:
     virtual void Fun1(int no=20)
     {
      int n=no;        
      cout<<"B::Fun1() " << this->n << "\n"; // WHY SO ? gives B::Fun1() 40
      cout<<"B::Fun1() " << n << "\n";
     }
    };

    int main()
    {
     B b;
     A &a =b;
     a.Fun1();
     A a1=b;
     a1.Fun1();
     getch();
     return 0;
    }

The output I got was

    B::Fun1() 40
    B::Fun1() 10
    A::Fun1() 10    
  • 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-29T22:56:49+00:00Added an answer on May 29, 2026 at 10:56 pm

    For the first output, this->n refers to the member variable B::n, which you never initialise – this could print anything.

    For the second, n refers to the local variable n, which you initialise with the function argument. Since default arguments are provided by the caller, and you’re calling the function via a reference to A, you get the default value specified in A::Fun1, not the one in B::Fun1. If you were to call b.Fun1(), then it would print B::Fun1() 20.

    For the third, you’ve “sliced” the B object, giving an object of type A, hence the call to A::Fun1.

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

Sidebar

Related Questions

I was doing some stuff with jQuery parseJSON function. This is the code, which
I can proxy a single function in javascript by doing something like this (just
I've just started doing some PowerShell scripting, and I'm running into a problem testing
I've just started doing some real-world performance testing on my Fluent NHibernate / SQLite
I am doing some significant refactoring and feature-adding on a project, and have just
i'm currently doing some reports for SSRS, and i just confirm what i already
I've just installed the 3.5 .NET Framework and while doing some cleaning I've notice
So I'm working on just a learning project to expose myself to doing some
I am just learning some pointers stuff in C and I happened to learn
After doing some stuff in Ruby on Rails with Cucumber, RSpec and Ruby BDD

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.