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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:41:55+00:00 2026-06-06T04:41:55+00:00

I read so many blogs and I understand how to use virtual function in

  • 0

I read so many blogs and I understand how to use virtual function in c++. But, still I don’t understand why we use virtual functions. Can you give me a real world example so that I can more easily visualize the actual meaning of virtual function.

  • 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-06T04:41:57+00:00Added an answer on June 6, 2026 at 4:41 am

    An important thing to mention is that inheritance (which the keyword virtual is fundamental for) should not be for the sole purpose of code re-use, use delegation for this.

    Delegation would be when we have a class say BroadbandConnection with a method called connection(). Then your manager says we want to add encryption, so you create a class BroadbandConnectionWithEncryption. Your natural instinct may be to use inheritance and then make the new class BroadbandConnectionWithEncryption derive from BroadbandConnection.

    Drawback’s to this is that the creator of the initial class had not designed it for inheritance so you would need to change its definition to make the method connection() virtual so you can override its behavior in the derived class. This is not always ideal. A better idea is to use delegation here for the purpose of code reuse.

    class BroadBandConnection
    {
    public:
        void Connection (string password)
        {
            //connection code.
        }
    };
    
    class BroadBandConnectionWithEndcryption
    {
    public:
        void Connection (string password)
        {
            mbroadbandconnection.Connection(password);
            //now do some stuff to zero the memory or
            //do some encryption stuff
        }
    
    private:
        BroadBandConnection mbroadbandconnection;
    };
    

    The keyword virtual is used for the purpose of polymorphism. As the name suggest, it is the ability for an object to have more than one form. This sort of decision would be made at the time of designing an interface or class.

    class IShape
    {
        virtual void Draw () = 0; 
    };
    
    class Square
    {
        void Draw()
        {
            //draw square on screen 
        }
    };
    
    class Circle
    {
        void Draw()
        {
            //draw circle on screen
        }
    };
    

    I made Draw() pure virtual with the = 0. I could have left this out and added some default implementation. Pure virtual makes sense for Interfaces where there is no reasonable default implementation.

    What this lets me do is pass around a Shape object to various methods and they do not need to be concerned with what I have just given them. All they know is that I have to provide something that supports the ability for a shape to draw itself.

    IShape* circle = new Circle ();
    IShape* square = new Square ();
    
    void SomeMethod (IShape* someShape)
    {
       someShape->Draw(); //This will call the correct functionality of draw
    }
    

    In the future as people begin thinking of new shapes, they can derive from IShape and so long as they implement some functionality for Draw. They can pass this object to SomeMethod.

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

Sidebar

Related Questions

I read many blogs, but I cannot understand the exact usage of Response.RedirectPermanent .
I have read many questions about Android, J2ME and RecordStore , but I still
I've read many posts and blogs at this point and I'm still not sure
I have read so many blogs and find out we cna use stringFormat property
I am trying understand ViewModels deeper and I have read many articles and blogs
I have read many questions about the facebook login but until not I didnt
I'm a machine learning newbie trying to understand how Adaboost works. I've read many
First of, I have read many explanations on SO and blogs about covariance and
I have read many posts and blogs about rewriting and I know there is
I try to learn Rspec , but I don't understand what is that. Let

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.