Code:
#include<iostream>
using namespace std;
class A {
protected:
int i;
public:
void fun(int p)
{
i=p;
cout<<"i= "<<i<<"p= "<<p<<endl;
}
};
class B: public A
{
public:
void function()
{
cout<<i<<endl;
}
};
int main()
{
A obj;
obj.fun(123);
B object;
object.function();
return 0;
}
The output of this code is:
123 123
##GARBAGE VALUE##
why is it happening? At the time when I passed 123 to p & then 'i' copied the value 123 into it, I thought it must have stored 123 into the memory space allocated to it, but after inheriting it 'function()' in class B is not printing the value. WHY?