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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:01:28+00:00 2026-06-06T08:01:28+00:00

In the Code Below there are Two Classes. One Object of type two is

  • 0

In the Code Below there are Two Classes. One Object of type two is created and then it is assigned to pointer of class one.

On Calling the out function, the out function of the class one is called.

#include<iostream>
using namespace std;

class one
{
    public :
        void  out()
        {
            cout<<"one ";
        }
};

class two
{
    public : 
        void out()
        {
            cout<<"two ";
        }
};

int main()
{ 
    two dp[3];
    one *bp = (one *)dp;
    for (int i=0; i<3;i++)
    (bp++)->out();
}    

OUTPUT

one one one

Output according to me should be two instead of one.
When we created the object of type two, the memory location of that object contained the address of the function of out of class two, then why on assignment, out of class one is called ?

EDIT –
moreover even if we change the name of function in class two, the output is not changed.

  • 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-06T08:01:30+00:00Added an answer on June 6, 2026 at 8:01 am

    It’s not uncommon for a novice to assume that all C++ member functions “belong” to an object.
    As you noticed, they don’t.

    Conceptually – the precise procedure is a compiler implementation detail – your out member functions are transformed into “free” functions that look like these:

    void one_out(one* this) { cout << "one"; }
    void two_out(two* this) { cout << "two"; }
    

    For non-member functions, that’s all that’s needed.

    When the compiler sees

      (bp++)->out();
    

    it knows that bp is a pointer to one (it doesn’t know that you lied), and so it calls

    one_out(bp++);
    

    because that’s what compilers do.

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

Sidebar

Related Questions

I have created two separate objects, one of class Order and one of class
I have two classes one and two . Both run threads. class Two is
In the code below the Move public class derives fromthe generic type Submit. Submit
I have two classes: an Object class, and an ObjectManager class. The ObjectManager class
Given the Java code below, what's the closest you could represent these two static
The code below is resulting in an error on a site in which there
Is there a way to get the code below to return null if no
in the code below at first if statements block (there will be more than
The code below is for a simple newsletter signup widget. I'm sure there's a
With the following code below, There is an error saying File /home/user/web_pro/info/views.py, line 184,

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.