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?
Answer Posted / 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 |
Post New Answer View All Answers
What does no cap mean?
What is constructor overloading in oop?
What is polymorphism programming?
write knight tour problem which is present in datastructure
write a program using c++ to implement single contiguous memory mangement techniques.display the content of the main memory after yhe allocation of jobs and percentage of the wastage of the main memory
What is for loop and its syntax?
What is difference between pop and oop?
What is encapsulation c#?
What is object and example?
Why is abstraction used?
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
How can you overcome the diamond problem in inheritance?
How oops is better than procedural?
What is new keyword in oops?
Question: Write a program that prints a paycheck. Ask the program user for the name of the employee, the hourly rate, and the number of hours worked. If the number of hours exceeds 40, the employee is paid “time and a half”, that is, 150 percent of the hourly rate on the hours exceeding 40. Be sure to use stepwi se refine ment and break your solution into several functions. Use the int_name function to print the dollar amount of the check.