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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:11:32+00:00 2026-05-12T16:11:32+00:00

Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;

  • 0

Consider the following program.

#include<iostream>
using namespace std;

class base
{

   public:
     int _bval;

     base():_bval(0){}
};

class derived:public base
{

   public:
     int _dval;

     derived():base(),_dval(1){}
};

int main()
{     

   derived d[5];
   base *p;
   p=d;
   for(int i=0;i<5;++i,++p)
      cout<<p->_bval;

}

The output of the above program is 01010.
I thought the output would be 00000 because the value of _bval was initialized to 0(each time) by the base class constructor.

But why is the output different from 00000?
What am I missing?

  • 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-12T16:11:33+00:00Added an answer on May 12, 2026 at 4:11 pm

    Short answer: In C++, arrays of values are never polymorphic, even if their content is, and cannot be treated so. That is, you cannot treat an derived ad[N] as if it was a base ab[N].

    Long answer: The reason for this is deeply buried in C’s pointer arithmetic. If you have an int* pi and increment it ++pi, it will not simply increment to the next memory address. If it did, it wouldn’t point to the next int since this doesn’t start at the next address. So instead sizeof(int) bytes are added to the pointer. (A concrete example might help: On architectures with 8bit char types – char being, by definition what C and C++ consider the architecture’s byte size – and 32bit int types, int has the size of 4 bytes. Thus, ++pi will add 4 to the pointers address, so that it points to the next int.) The same arithmetic applies to all other pointer operations. So, for example, with int* pi2=pi+1, pi2 will point sizeof(int) bytes behind pi, although pi2-pi will yield 1.

    So, presuming you understood the last paragraph, let’s go back to arrays. If you have an array derived ad[N], the address of ad[1] is sizeof(derived) bytes greater than the address of ad[0]. (That’s disregarding alignment in order to not to further complicate the issue.) However, if you have a base* pb pointing to ad[0], incrementing it will make it point sizeof(base) behind the address of the first element – which, if (as is the case in your example) sizeof(base) < sizeof(derived), is not the address of ad[1], but somewhere in the middle of ad[0].

    The only thing you can do to treat the array content as if it was all base classes, is to iterate over the array using a derived* and cast this pointer to base* within the loop:

    derived d[5];
    derived* begin = d; 
    const derived* end = d + sizeof(d)/sizeof(d[0]); // points one beyond the last element
    while(begin != end)
    {
      base* pb = begin;
      cout<< pb->_bval;
      ++begin;
    }
    

    (Note that I’ve also changed your code to use C++’ idiomatic begin/end iterators.)

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Did you take a look at this Multi-Threaded Game Server… May 12, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer The method FormsAuthentication.SignOut does it something like: HttpCookie cookie =… May 12, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer They're not essentially the same. The second also filters out… May 12, 2026 at 8:23 pm

Related Questions

Consider the following simple C program that read a file into a buffer and
// edited by Neil Butterworth to conserve vertical space #include <stdio.h> struct A; struct
I've written a library to match strings against a set of patterns and I
With the Visual Studio 2005 C++ compiler , I get the following warning when
Consider the following program. How is the behaviour that it displays (namely that exceptions

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.