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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:17:25+00:00 2026-06-08T18:17:25+00:00

I was trying below code: In which I am calling opengascap() of car class

  • 0

I was trying below code:

In which I am calling opengascap() of car class with car* pointer which is pointing to a object of nuclear** class but it is giving nuclear*** function of the object that is being pointed to.
My question is why its giving output “fire” although the function name doesn’t even exist in the nuclearsubmarine class.

  #include <iostream>

  using namespace std;

  class Vehicle 
  {
      public:
      virtual ~Vehicle() { }
      virtual void startEngine() = 0;
  };

  class Car : public Vehicle 
  {
  public:
      virtual void startEngine()
      {
          cout<<"start car";
      }
      virtual void openGasCap()
      {
      cout<<"open";
      }
  };

  class NuclearSubmarine : public Vehicle 
  {
      public:
      virtual void startEngine()
      {
          cout<<"start ship";
      }
      virtual void fireNuclearMissle()
      {
          cout<<"fire";
      }
  };

 int main()
 {
     Car   car;
     Car*  carPtr = &car;
     NuclearSubmarine  sub;
     NuclearSubmarine* subPtr = &sub;
     carPtr=(Car*)subPtr;
     // This last line would have caused carPtr to point to sub !
     carPtr->openGasCap();  // This might call fireNuclearMissle()!
     return 0;
 }

Ouput: fire

  • 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-08T18:17:26+00:00Added an answer on June 8, 2026 at 6:17 pm

    You’re pointing to an object of type NuclearSubmarine with a pointer to type Car. You can’t do that, because these types are unrelated.

    This error invokes undefined behaviour, which causes your program to behave in an unpredictable way.


    If you’re interested in why this happens, have a read on how virtual functions are implemented internally: Virtual method table. This will make things clear for you.


    In reply to the comment:

    That’s right. Vehicle instances all have an internal pointer that points to a vtable which looks something like this:

    0: Vehicle::~Vehicle
    1: Vehicle::StartEngine                   // this is a null pointer
    

    Car instances have their vptr point to a vtable which looks something like this:

    0:Vehicle::~Vehicle                       // this one didn't get overridden
    1:Car::startEngine
    2:Car::openGasTrap
    

    NuclearSubmarine‘s vtable looks like this:

    0:Vehicle::~Vehicle                       // this one didn't get overridden
    1:NuclearSubmarine::startEngine
    2:NuclearEngine::fireNuclearMissile.
    

    If you have this,

    Vehicle* v = new Car();
    v->startEngine();
    

    it gets compiled into something like this (pseudocode ahead):

    Vehicle* v = new Car();
    // OK, v is a Vehicle and startEngine is a Vehicle's virtual function of index 1
    // let's call it!
    StartEngineFunc* f = v.VPTR[1]; // look up the vtable using the object's virtual pointer
    CallMethod(v, f);
    

    The virtual pointer refers the function lookup into the correct vtable w/r/t the object’s actual runtime type.

    This allows you to call methods of derived classes via pointers to base classes, since derived classes will have their virtual table (the first part of it) corresponding to its parent class.

    However, if two classes are not related by a parent-child relationship, their virtual tables will have different meaning. That’s what happens in your case.

    (Note that while the vtable mechanism is an implementation detail – it’s very common in compilers, but it isn’t enforced in any way and compilers are free to implement it differently – so you shouldn’t count on that in your programs.)

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

Sidebar

Related Questions

I have a C extension (code below) in which I'm trying to get access
In the below code, i am trying to reference an external .dll, which creates
I have a jQuery function I am calling with the below code. The function
I'm using the javascript code below trying to extract the number after gallery-entry_ in
In the below code I'm trying to loop through each child node and append
In the code below i`m trying to read a list of selected projects and
I have been trying to access the below code DECLARE N_DEPTNO DEPT.DEPTNO %TYPE :=&DEPT_NUM;
I am trying to change desktop image using below code: NSError *anError = nil;
I found the below code in the Internet, I'm trying to understand how Linux
I am trying to load a JS file using yepnope using the below code:

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.