Inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class A
{
	int n;
public:
	virtual void Fun1(int no=10)
	{
		n = no;
		cout<<"A::Fun1"<<n;
	}
};

class B : public A
{
	int m;
public:
	virtual void 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??
Because it reads the default argument from A::Fun1, that information is not stored in the virtual table
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

Thanks All
Topic archived. No new replies allowed.