C++ Virtual Functions

  Home  C++ Programming  C++ Virtual Functions


“C++ Virtual Functions frequently Asked Questions in various C++ virtual functions job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting”



30 C++ Virtual Functions Questions And Answers

21⟩ What is problem with overriding functions?

Overriding of functions occurs in Inheritance. A derived class may override a base class member function. In overriding, the function names and parameter list are same in both the functions.

 184 views

22⟩ Do you know what is overriding?

Defining a function in the derived class with same name as in the parent class is called overriding. In C++, the base class member can be overridden by the derived.

 189 views

23⟩ Explain object slicing in C++?

When a Derived Class object is assigned to Base class, the base class' contents in the derived object are copied to the base class leaving behind the derived class specific contents. This is referred as Object Slicing. That is, the base class object can access only the base class members. This also implies the separation of base class members from derived class members has happened.

class base

{

public:

int i, j;

};

class derived : public base

{

public:

int k;

};

int main()

{

base b;

derived d;

b=d;

return 0;

}

here b contains i and j where as d contains i, j& k. On assignment only i and j of the d get copied into i and j of b. k does not get copied. on the effect object d got sliced.

 176 views

26⟩ Tell me can a pure virtual function have an implementation?

The quick answer to that question is yes! A pure virtual function can have an implementation in C++ - which is something that even many veteran C++ developers do not know. So, using the SomeClass class from our example above, we can have the following code:

class SomeClass {

public:

virtual void pure_virtual() = 0; // a pure virtual function

// note that there is no function body

};

/*This is an implementation of the pure_virtual function

which is declared as a pure virtual function.

This is perfectly legal:

*/

void SomeClass::pure_virtual() {

cout<<"This is a test"<

}< /endl;

 181 views

27⟩ Explain virtual destructor?

If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.

 167 views

28⟩ Explain data encapsulation?

The wrapping up of data and functions into a single unit (Class) is known as Encapsulation. By encapsulating the data inside the class; data is not accessible to the outside world.

 191 views

29⟩ Explain polymorphism?

Polymorphism means the ability to take more than one form. An operation may exhibit different behavior in different instances. The behavior depends on the types of data used in the operation.

 169 views

30⟩ What is the use of Vtable?

Vtables are used for virtual functions. Its a shortform for Virtual Function Table.

It's a static table created by the compiler. Compiler creates a static table per class and the data consists on pointers to the virtual function definitions. They are automatically initialised by the compiler's constructor code.

 185 views