© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

11. What is Pure Virtual Function? Why and when it is used?

Ans:

The abstract class whose pure virtual method has to be implemented by all the   classes which derive on these. Otherwise it would result in a compilation error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

Ex - 1: What is the output?

 #define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}

Ans:
64

Explanation:
the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64

Ex - 2: What is the output?

main()
{
 show();
}
void show()
{
 printf("I'm the greatest");
}

Ans:
Compiler error: Type mismatch in redeclaration of show.

Explanation:

When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error.
The solutions are as follows:
1. declare void show() in main() .
2. define show() before main().
3. declare extern void show() before the use of show().


SLogix Student Projects
bottom