J
Jordan Taylor
Hi, can anyone suggest how the compiler will decide which bar() to
invoke when we call d.dio()? I am getting the following output:
B foo
B bar
but is there a situation when the same code can print
B foo
D bar
?
#include<iostream>
using namespace std;
class Base
{
public:
void foo()
{
cout<<"B foo"<<endl;
bar();
}
void bar()
{
cout<<"B bar"<<endl;
}
};
class Derived : public Base {
public:
void dio()
{
foo();
}
void bar()
{
cout<<"D bar"<<endl;
}
};
int main()
{
Derived d;
d.dio();
return 0;
}
invoke when we call d.dio()? I am getting the following output:
B foo
B bar
but is there a situation when the same code can print
B foo
D bar
?
#include<iostream>
using namespace std;
class Base
{
public:
void foo()
{
cout<<"B foo"<<endl;
bar();
}
void bar()
{
cout<<"B bar"<<endl;
}
};
class Derived : public Base {
public:
void dio()
{
foo();
}
void bar()
{
cout<<"D bar"<<endl;
}
};
int main()
{
Derived d;
d.dio();
return 0;
}