Oops Interview Questions and Answers - 2

11. What is a virtual function?

Answer:

A virtual function is a member function which is declared within base class and is re-defined (Overriden) by derived class.

You use virtual functions when you want to override a certain behavior for your derived class at run-time.

12. What is a friend function?

Answer:

If a function is defined as a friend function then the protected and private data of a class can be accessed using the function.

13. What is function overloading?

Answer:

Function overloading is a feature where two or more functions can have the same name but different parameters.

The function can perform different operations and hence eliminates the use of different function names for the same kind of operations.

Example
void Show(int& a, int& b);
void Show(double& a, double& b);
void Show(struct bob& a, struct bob& b);

14. What is operator overloading?

Answer:

It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type.

15. What is an abstract class?

Answer:

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does.

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

16. What is an interface?

Answer:

Interfaces specify what a class must do and not how. It is the blueprint of the class.

An interface can have methods and variables, but the methods declared in interface are by default abstract

It is used to achieve total abstraction.

17. Difference between overloading and overriding.

Answer:

Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.

Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism.

The idea behind method overriding is to change the implementation of given method in a subclass. In other words you “override” the implementation of the parent’s class method using the same signature of the method (name, return types, parameters), but implement different functionality inside the overridden method.

18. Difference between a class and an object.

Answer:

An instance of a class is known as an object. A template or blueprint with which objects are created is known as a Class.

Object allocates memory when it is created. Class doesn't allocate memory when it is created.

Object is a physical entity. Class is a logical entity.