Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C++
#include<iostream.h>
#include<conio.h>

class base
{
public:
    int bval;
    base()
    {
        bval=0;
    }
};

class deri:public base
{
public:
    int dval;
    deri()
    {
        dval=1;
    }
};

void SomeFunc(base *arr,int size)
{
    for(iont i=0;i<size;i++,arr++)
        cout<<arr->bval;
    cout<<endl;
}

int main()
{
    base BaseArr[5];
    SomeFunc(BaseArr,5);
    deri DeriArr[5];
    SomeFunc(DeriArr,5);
}


What I have tried:

this program is giving output as :
00000
01010
Posted
Updated 17-Aug-17 13:56pm
v2

That is because your SomeFunc() function uses a pointer to the base class.

Within the loop the used address of the passed argument is incremented by sizeof(base). But for the deri class it should be incremented by sizeof(deri) to get a valid pointer to the next item. Because base has a size of 4 bytes / sizeof(int) and deri twice the size, the pointer will run out of sync.

The memory layout of deri is:
C++
int bval;
int dval;
The values effectively accessed are then:
Iteration  base      deri
0          bval[0]   bval[0] = 0
1          bval[1]   dval[0] = 1
2          bval[2]   bval[1] = 0
3          bval[3]   dval[1] = 1
4          bval[4]   bval[2] = 0
 
Share this answer
 
Simple: that code doesn't compile. So what you are running is the output from a different set of code. oint is not a known type for starters.
But if it did, it wouldn't do what you think it should, and the reason is this:
void SomeFunc(base *arr,int size)
{
    for(iont i=0;i<size;i++,arr++)
        cout<<arr->bval;

deri DeriArr[5];
SomeFunc(DeriArr,5);
When you pass DeriArr to SomeFunc, it arrives as a Base* - which is perfectly valid, as the name of an array is a pointer to the first element in the array, and Deri is derived from Base, so a pointer to a Deri is a valid pointer to a Base.
But then you increment the pointer: arr++ as part of the for loop.
As far as SomeFunc is concerned, arr is a pointer to a Base, so ++ adds the size of a Base in bytes to the pointer. But Deri is bigger than Base - it has an extra integer. So incrementing the pointer doesn't point to the next Deri element, it points to the next Base element and ends up half way through the Deri instance. And lo: you get the result you see.
 
Share this answer
 
Quote:
How this program is giving output in C++ please explain

See by yourself with debugger.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900