suppose A is a base class and B is the derved class. Both
have a method foo which is defined as a virtual method in
the base class. You have a pointer of classs B and you
typecast it to A. Now when you call pointer->foo, which
method gets called?

The next part of the question is, how does the compiler
know which method to call?

Answers were Sorted based on User's Feedback



suppose A is a base class and B is the derved class. Both have a method foo which is defined as a ..

Answer / nm

The answer is that B->foo gets calle, and why ? I found a
good explanation of that here:

http://en.wikipedia.org/wiki/Virtual_table

Is This Answer Correct ?    3 Yes 1 No

suppose A is a base class and B is the derved class. Both have a method foo which is defined as a ..

Answer / rahul

the answer is that compiler does only the the type checking at compile time it checks only that the type of reference or the type of object is being assigned to the type is assignable or not(i.e parent child relationship or cast operation should be valid otherwise it will give you compile time error) and compiler conveys the message to the runtime that this operation is valid so no type checking is done at run time, at runtime function call depends upon the actual object being referred by the type but the existance of that function is checked in the type class if the function doesn't exist in the type class the function will not be called even though it exist in the class which is being instantiated by type(reference type)

for example A a = new B();
a.foo();
here we are calling the foo() method of class B(because object of class B is being instantiated)using class A reference now here when we compile this program the compiler checks for foo() method in class A if this method doesn't exist in class A compiler even will not compile your program

Is This Answer Correct ?    0 Yes 0 No

suppose A is a base class and B is the derved class. Both have a method foo which is defined as a ..

Answer / suchi

It depends the pointer points to the object of which class.
If the pointer points to Class A, then the method defined
in class A will be called, and if the pointer points to
class B, then the method defined in class B will be called.

Is This Answer Correct ?    2 Yes 6 No

Post New Answer

More OOPS Interview Questions

What is abstraction in oop with example?

0 Answers  


What is meant by oops concept?

0 Answers  


Can private class be inherited?

0 Answers  


How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction

0 Answers  


which feature are not hold visual basic of oop?

0 Answers   Ignou,






What makes a language oop?

0 Answers  


what is pointers

7 Answers   Exilant,


write a program to find 2 power of a 5digit number with out using big int and exponent ?

0 Answers  


should we use linear search or binary search if elements are placed in random order or mixed?in both cases? i need a little bit detail ans?thnks

2 Answers  


write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.

0 Answers   Wipro,


why reinterpret cast is considered dangerous?

0 Answers   TCS,


What is class and example?

0 Answers  


Categories