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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:24:31+00:00 2026-06-13T05:24:31+00:00

I have been reading about Virtual functions and found, VF are used in polymorphism

  • 0

I have been reading about Virtual functions and found,

VF are used in polymorphism of an inherited class.

So , if a class and derived class both have the same function name, VF binds the appropriate function to the function call.

i.e. If the function in question is designated virtual in the base class then the derived class’s function would be called. If it is not virtual, the base class’s function would be called.

In Java by default: all functions are Virtual C++: Non-virtual and can be made Virtual in Java by using final, private access modifier while in C++ using Virtual keyword to make a function virtual.

Based on the above theory, i wrote the code:

#include <iostream>

class base{
    public : 
        virtual void function1(){
            std::cout<<"BaseVirtual"<<std::endl;
        }

        void function2(){
            std::cout<<"Base NonVirtual"<<std::endl;
        }
};


class derieved: public base
{
    public : 
        void function1(){
            std::cout<<"Derieved Virtual"<<std::endl;
        }

        void function2(){
            std::cout<<"Derieved NonVirtual"<<std::endl;
        }
};



int main()
{
    base b1;
    derieved d1;

    b1.function1();
    b1.function2();

    d1.function1();
    d1.function2();    

}

Now based on the fact, if its a virtual function then only derived class function is called, my output for the above program should be:

BaseVirtual
Base NonVirtual
Derieved Virtual
Base NonVirtual

however, it came out to be:

BaseVirtual
Base NonVirtual
Derieved Virtual
Derieved NonVirtual

which must be right of course. So my question is the output totally violates the statement If the function in question is designated virtual in the base class then the derived class’s function would be called. If it is not virtual, the base class’s function would be called. for the call:

  d1.function2();    
  • 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-13T05:24:32+00:00Added an answer on June 13, 2026 at 5:24 am

    Yes.. the role of virtual comes into picture if and only if you have are trying to access the derived class object with a base class pointer.

    With you example:-

    #include <iostream>
    
    class base{
        public : 
            virtual void function1(){
                std::cout<<"BaseVirtual"<<std::endl;
            }
    
            void function2(){
                std::cout<<"Base NonVirtual"<<std::endl;
            }
    };
    
    
    class derieved: public base
    {
        public : 
            void function1(){
                std::cout<<"Derieved Virtual"<<std::endl;
            }
    
            void function2(){
                std::cout<<"Derieved NonVirtual"<<std::endl;
            }
    };
    
    
    
    int main()
    {
        base *b1;
        derieved d1;
    
        b1=&d1;
    
        b1->function1();
        b1->function2();    
        return 0;
    }
    

    output:-

    Derieved Virtual
    Base NonVirtual
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about MVP, MVVM and have used asp.net MVC in couple
I have been reading about Quicksort and found that sometimes it' s referred to
i have been reading about a function that can overwrite its return address. void
I have been reading about Active Record Associations on RailGuides and I have found
I have been reading about try/finally on MSDN and found out following code. They
I have been reading about hashcode functions for the past couple of hours and
i have been reading about network flooding algorithm that it is used to discover
I have been reading about model inheritances in django and have found that many
I have been reading about text classification and found several Java tools which are
Have been reading about async and tasks and been attempting to convert the CopyFileEx

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.