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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:50:10+00:00 2026-05-29T03:50:10+00:00

So I understand pretty much how it works, but I just can’t grasp what

  • 0

So I understand pretty much how it works, but I just can’t grasp what makes it useful. You still have to define all the separate functions, you still have to create an instance of each object, so why not just call the function from that object vs creating the object, creating a pointer to the parent object and passing the derived objects reference, just to call a function? I don’t understand the benefits of taking this extra step.

Why do this:

class Parent
{
    virtual void function(){};
};

class Derived : public Parent
{
    void function()
    {
    cout << "derived";
    }
};

int main()
{
    Derived foo;
    Parent* bar = &foo;
    bar->function();
    return -3234324;
}

vs this:

class Parent
{
    virtual void function(){};
};

class Derived : public Parent
{
    void function()
    {
    cout << "derived";
    }
};

int main()
{
    Derived foo;
    foo.function();
    return -3234324;
}

They do exactly the same thing right? Only one uses more memory and more confusion as far as I can tell.

  • 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-29T03:50:10+00:00Added an answer on May 29, 2026 at 3:50 am

    Both your examples do the same thing but in different ways.
    The first example calls function() by using Static binding while the second calls it using Dynamic Binding.

    In first case the compiler precisely knows which function to call at compilation time itself, while in second case the decision as to which function should be called is made at run-time depending on the type of object which is pointed by the Base class pointer.

    What is the advantage?
    The advantage is more generic and loosely coupled code.

    Imagine a class hierarchy as follows:

    enter image description here

    The calling code which uses these classes, will be like:

    Shape *basep[] = { &line_obj, &tri_obj,
                       &rect_obj, &cir_obj};
    for (i = 0; i < NO_PICTURES; i++)
        basep[i] -> Draw ();
    

    Where, line_obj, tri_obj etc are objects of the concrete Shape classes Line, Triangle and so on, and they are stored in a array of pointers of the type of more generalized base class Shape.

    This gives the additional flexibility and loose coupling that if you need to add another concrete shape class say Rhombus, the calling code does not have to change much, because it refers to all concrete shapes with a pointer to Base class Shape. You only have to make the Base class pointer point to the new concrete class.

    At the sametime the calling code can call appropriate methods of those classes because the Draw() method would be virtual in these classes and the method to call will be decided at run-time depending on what object the base class pointer points to.

    The above is an good example of applying Open Closed Principle of the famous SOLID design principles.

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

Sidebar

Related Questions

well, the title pretty much says it all. here's a little info: i have
I understand PCRE pretty well, but MySQL's regular expression flavor seems to get the
I'm pretty sure I understand how seeding work with respect to seeds.rb, but I
I understand this is an easy question but for some reason this just isn't
I understand mostpayment gateway just deposit all incoming payment to one account. IS it
The title pretty much says it all, so here's the code: #include <stdio.h> /*
Hi I have setup a project pretty much the same as the Restkit RKGithub
So, I am pretty new to web programming and I just understand the basic
Well the title Question pretty much sums it up, But I'd like to detail
This is pretty standard stuff here, and I cannot understand why it isn't working.

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.