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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:49:40+00:00 2026-05-28T20:49:40+00:00

Is the Wikipedia example at this article about method overiding in C++ correct? Please

  • 0

Is the Wikipedia example at this article about method overiding in C++ correct?

Please see the code below where I have mentioned a comment //INCORRECT

Slightly confused about overiding in C++ and run time polymorphism. What should this cast do?

#include <iostream>

class Rectangle {
public:
    explicit Rectangle(double l, double w) : length(l), width(w) {}
    virtual void print() const;

private:
    double length;
    double width;
};

void Rectangle::print() const { // print() method of base class
   std::cout << "Length = " << this->length << "; Width = " << this->width;
}

class Box : public Rectangle {
public:
    explicit Box(double l, double w, double h) : Rectangle(l, w), height(h) {}
    virtual void print() const; // virtual is optional here, but it is a good practice to remind it to the developer

private:
    double height;
};

void Box::print() const { // print() method of derived class
   Rectangle::print();  // Invoke parent print() method.
   std::cout << "; Height= " << this->height;
}

int main(int argc, char** argv) {
   Rectangle rectangle(5.0, 3.0);   rectangle.print();
   // outputs:
   // Length = 5.0; Width = 3.0

   Box box(6.0, 5.0, 4.0);
   // the pointer to the most overridden method in the vtable in on Box::print
   //box.print(); // but this call does not illustrate overriding

   static_cast<Rectangle&>(box).print(); // this one does   

   // outputs:
   // Length = 5.0; Width = 3.0; Height= 4 // INCORRECT  

   //But it actually outputs Length = 6; Width = 5; Height = 4

   getchar();
   return 0;

}
  • 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-28T20:49:41+00:00Added an answer on May 28, 2026 at 8:49 pm

    this line: static_cast<Rectangle&>(box).print(); has same effect as this code would have:

    Rectangle & r = box;
    r.print();
    

    and also same effect as this code would have:

    Rectangle * r = &box;
    r->print();
    

    It means that Box object was created (Box box(6.0, 5.0, 4.0); = in runtime you see Box) and it doesn’t matter what is the type of pointer / reference where you store this object. The fact that you store box as a Rectangle* is what you see in compile time.

    Most important fact about overriding virtual methods is that about “which method will be called?” is decided in runtime.

    Hope this helps.

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

Sidebar

Related Questions

Below is the statement written from Wikipedia's Isolation article about REPEATABLE READS In this
Judging from this Wikipedia article on cipher modes and other things I've heard about
What is this SWX data format that I keep hearing about? The Wikipedia article
At the end of this wikipedia article, in a section A simple example in
I'm talking mostly about functional programming languages. For example, the wikipedia article for map
How can I create pdf documents with 2D barcodes (for example QR code http://en.wikipedia.org/wiki/QR_Code
Having issues displaying some websites within iframes. Live example of code This one works.
In the Wikipedia Article on Block Cipher Modes they have a neat little diagram
This question about zip bombs naturally led me to the Wikipedia page on the
I had spend some time on this Bridge pattern example from wikipedia , however,

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.