C++ Interview Questions and Answers for Freshers, Experienced

    174 Votes

19. What are virtual functions and what is its use?

[This is a sure question in not just C++ interviews, but any other OOP language interviews. Virtual functions are the important concepts in any object oriented language, not just from interview perspective. Virtual functions are used to implement run time polymorphism in c++.]
Virtual functions are member functions of class which is declared using keyword 'virtual'. When a base class type reference is initialized using object of sub class type and an overridden method which is declared as virtual is invoked using the base reference, the method in child class object will get invoked.
class Base
{
    int a; 
    public:
        Base()
        {
            a = 1;
        }
        virtual void method()
        {
            cout << a;
        }
};

class Child: public Base
{
    int b;
    public: 
        Child()
        {
            b = 2; 
        }
        virtual void method()
        { 
            cout << b;
        }
};

int main()
{
    Base *pBase; 
    Child oChild;
    pBase = &oChild;
    pBase->method(); 
    return 0;
}
In the above example even though the method in invoked on Base class reference, method of the child will get invoked since its declared as virtual.

20. What do you mean by pure virtual functions in C++? Give an example?

Pure virtual function is a function which doesn't have an implementation and the same needs to be implemented by the the next immediate non-abstract class. (A class will become an abstract class if there is at-least a single pure virtual function and thus pure virtual functions are used to create interfaces in c++).

How to create a pure virtual function?
A function is made as pure virtual function by the using a specific signature, " = 0" appended to the function declaration as given below,
class SymmetricShape {
    public:
        // draw() is a pure virtual function.
        virtual void draw() = 0;
};

21. Why pure virtual functions are used if they don't have implementation / When does a pure virtual function become useful?

Pure virtual functions are used when it doesn't make sense to provide definition of a virtual function in the base class or a proper definition does not exists in the context of base class. Consider the above example, class SymmetricShape is used as base class for shapes with symmetric structure(Circle, square, equilateral triangle etc). In this case, there exists no proper definition for function draw() in the base class SymmetricShape instead the child classes of SymmetricShape (Cirlce, Square etc) can implement this method and draw proper shape.

22. What is virtual destructors? Why they are used?

[This c++ interview question is in a way related to polymorphism.]
Virtual destructors are used for the same purpose as virtual functions. When you remove an object of subclass, which is referenced by a parent class pointer, only destructor of base class will get executed. But if the destructor is defined using virtual keyword, both the destructors [ of parent and sub class ] will get invoked.

23. What you mean by early binding and late binding? How it is related to dynamic binding?

[This c++ interview question is related to question about virtual functions ]
Binding is the process of linking actual address of functions or identifiers to their reference. This happens mainly two times.
  • During compilation : This is called early binding
For all the direct function references compiler will replace the reference with actual address of the method.
  • At runtime : This is called late binding.
In case of virtual function calls using a Base reference, as in shown in the example of question no: 2, compiler does not know which method will get called at run time. In this case compiler will replace the reference with code to get the address of function at runtime.

Dynamic binding is another name for late binding.

24. What is meant by reference variable in C++?

In C++, reference variable allows you create an alias (second name) for an already existing variable. A reference variable can be used to access (read/write) the original data. That means, both the variable and reference variable are attached to same memory location. In effect, if you change the value of a variable using reference variable, both will get changed (because both are attached to same memory location).

How to create a reference variable in C++
Appending an ampersand (&) to the end of datatype makes a variable eligible to use as reference variable.

int a = 20;
int& b = a;
			

The first statement initializes a an integer variable a. Second statement creates an integer reference initialized to variable a
Take a look at the below example to see how reference variables work.

int main ()
{
	int   a;
	int&  b = a;

	a = 10;
	cout << "Value of a : " << a << endl;
	cout << "Value of a reference (b) : " << b  << endl;

	b = 20;
	cout << "Value of a : " << a << endl;
	cout << "Value of a reference (b) : " << b  << endl;

	return 0;
}

Above code creates following output.

Value of a : 10
Value of a reference (b) : 10
Value of a : 20
Value of a reference (b) : 20

25. What are the difference between reference variables and pointers in C++?

[This question is usually asked in a twisted way during c++ interviews. Sometimes the interviewer might use examples and ask you to find the error.]
PointersReference Variables
Pointers can be assigned to NULL References cannot be assigned NULL. It should always be associated with actual memory, not NULL.
Pointers can be (re)pointed to any object, at any time, any number of times during the execution. Reference variables should be initialized with an object when they are created and they cannot be reinitialized to refer to another object
Pointer has own memory address and location on stack Reference variables has location on stack, but shares the same memory location with the object it refer to.
Page 4 of 4

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Advanced C Interview Questions and Answers for Experienced
Embedded C Interview Questions and Answers
C Interview Questions and Answers