class A
{
int n;
public:
virtualvoid Fun1(int no=10)
{
n = no;
cout<<"A::Fun1"<<n;
}
};
class B : public A
{
int m;
public:
virtualvoid Fun1(int no=20)
{
m = no;
cout<<"B::Fun1()"<<m;
}
};
int main()
{
B b;
A &a =b;
a.Fun1();
}
When we execute it, B::Fun1() is being executed.
But it prints value of member variable of A class. Why??
Hi,
When you call the function only on the objec as line 2 .
1 2 3 4
B b;
b.Func1() ;
A &a =b;
a.Fun1();
This is default value is complied at the complied time and you will get the value 20 .
Where in dynamic binding the default value of the of the derived class is lost and the value of the base class is taken