“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Actually, if access is NOT specified, it deafults to private derivation. In private derivation, binding is static. So, whether foo is declared virtual or not it still defaults to static binding. So, A->foo() is called.
However, If a public derivation is specified from C <– B and B <– A, then if foo() is virtual C->foo() is called; if foo() is non virtual A->foo() is called.
The following statement “In private derivation, binding is static.” in Venu’s answer is incorrect. The binding depends upon whether the function is declared virtual or not. I think it has nothing to do the type of derivation.
I tested following piece of code:
#include
class A
{
public:
virtual void foo()
{
std::cout foo();
int i;
std::cin >> i;
}
yeah what ever Nitin said is true.. it does not depend on the public or private derivatio or either public or private function . It depends only on virtual or not ..
” I cast C to A “- derived class pointer can not point to base class. So casting C to A itself is impossible.
If somebody feels otherwise, please prove with code.
I interpret this question like this, we have class a which derives both b and c
Class b {
public:
int foo();
}
Class c {
public:
int foo();
}
Class a: public b, public c {
public:
int foo();
}
c *c1 = new c();
a *a1;
/* we do a cast of c to a */
a1 = c1;
/* So guys we have two interpretations, C is downcasted to a. C already has foo function. The function foo which is active in memory is that of c->foo().
So no matter you call a->foo(); you would get result of implemetation done by c->foo() and not the implementation of a->foo();
foo() for class C will be called.
it depends. if in A foo is defined as virtual function. then call C’s foo(),
if it doesn’t defined virtual, then
call A’s foo()
Actually, if access is NOT specified, it deafults to private derivation. In private derivation, binding is static. So, whether foo is declared virtual or not it still defaults to static binding. So, A->foo() is called.
However, If a public derivation is specified from C <– B and B <– A, then if foo() is virtual C->foo() is called; if foo() is non virtual A->foo() is called.
foo is not virtual than class c’s function will be called other wise as it searched top to bottom class A’s function will be called
The question is deeply ambiguous, with answer 4 ( venu ) above, being the correct one:
I have interpreted it as follows,
the correct answer in this interpretation being A
class a
{
private:
int a;
public:
int foo( int i );
};
class b : public a
{
private:
int b;
public:
int foo( int i );
};
class c : public b
{
private:
int c;
public:
int foo( int i );
};
int a :: foo( int i )
{
i ++ ;
}
int b :: foo( int i )
{
i ++ ;
}
int c :: foo( int i )
{
i ++ ;
}
void my_application :: a_test( void )
{
a A;
b B;
c * cptr = new c;
(( a * )cptr )->foo( 1 );
delete( cptr );
}
The following statement “In private derivation, binding is static.” in Venu’s answer is incorrect. The binding depends upon whether the function is declared virtual or not. I think it has nothing to do the type of derivation.
I tested following piece of code:
#include
class A
{
public:
virtual void foo()
{
std::cout foo();
int i;
std::cin >> i;
}
and it prints “In B”
Here is the correct program
#include
class A
{
public:
virtual void foo()
{
std::cout foo();
int i;
std::cin >> i;
}
yeah what ever Nitin said is true.. it does not depend on the public or private derivatio or either public or private function . It depends only on virtual or not ..
” I cast C to A “- derived class pointer can not point to base class. So casting C to A itself is impossible.
If somebody feels otherwise, please prove with code.
When you cast a C to a A slicing occurs… you loose C and B s A.foo () is called
Bill Campbell is correct.
When you cast C to A, object slicing occurs - meaning the “object” is treated of type A, so it looses all of the derived class details.
So, irrespective of foo() being virtual or not, A.foo() would be called.
In general, Virtual functions can only be called through pointers, where a base class pointer is refering to a derived class’s object.
I interpret this question like this, we have class a which derives both b and c
Class b {
public:
int foo();
}
Class c {
public:
int foo();
}
Class a: public b, public c {
public:
int foo();
}
c *c1 = new c();
a *a1;
/* we do a cast of c to a */
a1 = c1;
/* So guys we have two interpretations, C is downcasted to a. C already has foo function. The function foo which is active in memory is that of c->foo().
So no matter you call a->foo(); you would get result of implemetation done by c->foo() and not the implementation of a->foo();
Let me know if i am wrong.
Leave an Answer/Comment