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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:15:14+00:00 2026-05-10T17:15:14+00:00

Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base

  • 0

Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability):

// abstract base class #include <iostream> using namespace std;  class Polygon {     protected:         int width;         int height;     public:         void set_values(int a, int b) { width = a; height = b; }         virtual int area(void) =0; };  class Rectangle: public Polygon {     public:         int area(void) { return width * height; } };  class Triangle: public Polygon {     public:         int area(void) { return width * height / 2; } };  int main () {     Rectangle rect;     Triangle trgl;     Polygon * ppoly1 = &rect;     Polygon * ppoly2 = &trgl;     ppoly1->set_values (4,5);     ppoly2->set_values (4,5);     cout << ppoly1->area() << endl; // outputs 20     cout << ppoly2->area() << endl; // outputs 10     return 0; } 

My question is how does the compiler know that ppoly1 is a Rectangle and that ppoly2 is a Triangle, so that it can call the correct area() function? It could find that out by looking at the ‘Polygon * ppoly1 = ▭’ line and knowing that rect is a Rectangle, but that wouldn’t work in all cases, would it? What if you did something like this?

cout << ((Polygon *)0x12345678)->area() << endl; 

Assuming that you’re allowed to access that random area of memory.

I would test this out but I can’t on the computer I’m on at the moment.

(I hope I’m not missing something obvious…)

  • 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. 2026-05-10T17:15:15+00:00Added an answer on May 10, 2026 at 5:15 pm

    Each object (that belongs to a class with at least one virtual function) has a pointer, called a vptr. It points to the vtbl of its actual class (which each class with virtual functions has at least one of; possibly more than one for some multiple-inheritance scenarios).

    The vtbl contains a bunch of pointers, one for each virtual function. So at runtime, the code just uses the object’s vptr to locate the vtbl, and from there the address of the actual overridden function.

    In your specific case, Polygon, Rectangle, and Triangle each has a vtbl, each with one entry pointing to its relevant area method. Your ppoly1 will have a vptr pointing to Rectangle‘s vtbl, and ppoly2 similarly with Triangle‘s vtbl. Hope this helps!

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

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 60k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Why is the session growing? Are you doing this within… May 11, 2026 at 9:34 am
  • added an answer The answer is simple. Read the produced xml files. They… May 11, 2026 at 9:34 am
  • added an answer Sorry, misunderstood your question. According to Javascript - capturing onsubmit… May 11, 2026 at 9:34 am

Related Questions

Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
Here is an example of what I've got going on: CREATE TABLE Parent (id
Anybody have good examples of usability disasters? Here's an example. Hector is a manager
Here is an example: <h:outputText value=#{myBean.myMoney}> <f:convertNumber type=currency currencySymbol=$ /> </h:outputText> Given that I
Here is an example that a forum poster gave, I can't tell if this
My question is pretty vague :o) - But here is an example : When
Here's a very simple Prototype example. All it does is, on window load, an
Here is an interesting piece of code that my fellow team members were just
What did I do wrong? Here is an excerpt from my code: public void
I'm a little confused by some PHP syntax I've come across. Here is an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.