0% found this document useful (0 votes)
24 views

Short Answer

A virtual function allows a function to be redefined in a derived class. When calling a virtual function using a base class pointer, the version from the most derived class will be called. In the example, class B derives from class A and redefines the virtual function show(). When an object of class B is created and accessed using a base class pointer, calling show() will print the value from class B (b=2) rather than class A (a=1). Virtual base classes allow multiple inheritance without ambiguities. Pure virtual functions define an interface that derived classes must implement by providing function definitions.

Uploaded by

Ali Hassan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Short Answer

A virtual function allows a function to be redefined in a derived class. When calling a virtual function using a base class pointer, the version from the most derived class will be called. In the example, class B derives from class A and redefines the virtual function show(). When an object of class B is created and accessed using a base class pointer, calling show() will print the value from class B (b=2) rather than class A (a=1). Virtual base classes allow multiple inheritance without ambiguities. Pure virtual functions define an interface that derived classes must implement by providing function definitions.

Uploaded by

Ali Hassan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem Statement Q1:What is virtual function? Explain with an example.

A virtual function is that function in the class which can be declared in the base class and if we have to redefine it again so we make use of the drive class and again redefine it .So by making a function virtual we can make it value being changed. Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class.

Example:
Class A { int a; public: A() { a = 1; } virtual void show() { cout <<a; } }; Class B: public A { int b; public: B() { b = 2; } virtual void show() { cout <<b; } };

The main of the code will be as following int main() { A *pA; B oB; pA = &oB; pA->show(); return 0; }

Output :
2

Q2- What is a virtual base class? Explain with an example. Why we need them?
A virtual base class is one class abc { virtual void print() =0 } you cannot instantiate a virtual base class for you to create an object you will have to over ride pure virtual methods in your inherited class.

Example:
Virtual Class A { Void show{} } Class B:public class A{ Void show() } Class C:public class A{ Void show() } Class D : public class c, public class b{}

3-What are pure virtual functions? Explain with an example?


A pure virtual class is one in which we have no function definition and it is being equal to zero .So we can make its object in the inherited class and make use of it. It is the concept of dynamic polymorphism .We can achieve it by using pure virtual function.

Example:
Virtual void show=0 { }

You might also like