0% found this document useful (0 votes)
914 views17 pages

Virtual Base Class

Virtual base classes in C++ are used to prevent multiple instances of a base class from appearing in an inheritance hierarchy with multiple inheritances. Without a virtual base class, a base class can be inherited twice and its members would appear twice in derived classes. Abstract classes contain at least one pure virtual function that is defined with = 0 and declares a function signature but provides no implementation. Derived classes must implement all pure virtual functions to not be abstract themselves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
914 views17 pages

Virtual Base Class

Virtual base classes in C++ are used to prevent multiple instances of a base class from appearing in an inheritance hierarchy with multiple inheritances. Without a virtual base class, a base class can be inherited twice and its members would appear twice in derived classes. Abstract classes contain at least one pure virtual function that is defined with = 0 and declares a function signature but provides no implementation. Derived classes must implement all pure virtual functions to not be abstract themselves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Virtual base class

Virtual base class in C++


• Virtual base classes are used in virtual
inheritance in a way of preventing multiple
“instances” of a given class appearing in an
inheritance hierarchy when using multiple
inheritances.
Need for Virtual Base Classes:
• Consider the situation where we have one
class A .This class is A is inherited by two other
classes B and C. Both these class are inherited
into another in a new class D as shown in
figure below.
• As we can see from the figure that data
members/function of class A are inherited twice to
class D. One through class B and second through
class C. When any data / function member of
class A is accessed by an object of class D,
ambiguity arises as to which data/function
member would be called? One inherited
through B or the other inherited through C. This
confuses compiler and it displays error.
To show the need of Virtual Base Class in C++
#include <iostream> class D : public B, public C {
using namespace std;
};
class A {
public:
void show() int main()
{
{
cout << "Hello form A \n";
} D object;
};
object.show();
class B : public A { }
};

class C : public A {
};
Compile Errors:
• prog.cpp: In function 'int main()':
• prog.cpp:29:9: error: request for member 'show' is
ambiguous
• object.show();
^
• prog.cpp:8:8: note: candidates are: void A::show()
• void show()
^
• prog.cpp:8:8: note: void A::show()
How to resolve this issue?
• To resolve this ambiguity when class A is inherited in both class B and
class C, it is declared as virtual base class by placing a keyword virtual as :
• Syntax for Virtual Base Classes:
Syntax 1:
class B : virtual public A
{
};

Syntax 2:
class C : public virtual A
{
};
Note:
• virtual can be written before or after the public.
• Now only one copy of data/function member will be copied
to class C and class B and class A becomes the virtual base
class.
• Virtual base classes offer a way to save space and avoid
ambiguities in class hierarchies that use multiple inheritances.
• When a base class is specified as a virtual base, it can act as
an indirect base more than once without duplication of its
data members.
• A single copy of its data members is shared by all the base
classes that use virtual base.
Example
#include <iostream>
using namespace std;
int main()
{
class A {
public: D object; // object creation of
int a; class d
A() // constructor
{ cout << "a = " << object.a << endl;
a = 10;
}
}; return 0;
class B : public virtual A { }
};

class C : public virtual A {


}; Output:
class D : public B, public C {
};
a = 10
Explanation :
• The class A has just one data member a which
is public. This class is virtually inherited in
class B and class C. Now class B and
class C becomes virtual base class and no
duplication of data member a is done.
Pure Virtual Functions and
Abstract Classes in C++
Introduction
• Sometimes implementation of all function cannot be
provided in a base class because we don’t know the
implementation. Such a class is called abstract class.
• For example, let Shape be a base class. We cannot provide
implementation of function draw() in Shape, but we know
every derived class must have implementation of draw().
• Similarly an Animal class doesn’t have implementation of
move() (assuming that all animals move), but all animals
must know how to move. We cannot create objects of
abstract classes.
• A pure virtual function (or abstract function) in
C++ is a virtual function for which we don’t
have implementation, we only declare it. A
pure virtual function is declared by assigning 0
in declaration.
• See the following example.
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;

/* Other members */
};
A complete example:
• A pure virtual function is implemented by class Derived: public Base
classes which are derived from a Abstract {
class. Following is a simple example to int y;
demonstrate the same. public:
void fun() { cout << "fun() called"; }
#include<iostream> };
using namespace std;
int main(void)
class Base
{
{
Derived d;
int x;
public:
d.fun();
virtual void fun() = 0; return 0;
int getX() { return x; } }
};
Output:
// This class inherits from Base and implements fun()
fun() called
Some Interesting Facts:
1. A class is abstract if it has at least one pure
virtual function.
2. We can have pointers and references of
abstract class type.
3. If we do not override the pure virtual function
in derived class, then derived class also
becomes abstract class.
4. An abstract class can have constructors.

You might also like