Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Virtual base class in c++
Scaler Topics profile imageAmod Nazirkar
Amod Nazirkar forScaler Topics

Posted on

     

Virtual base class in c++

Introduction

Let’s start with an example of the hybrid inheritance, in which there is a base class, let's say A, and it has two derived classes, let's say class B and class C. Now as there is hybrid inheritance so there may be another class, let’s say D which inherits properties of both class B and class C.

As a child class can access the member functions or properties of the parent class or even the properties of the parent class’s parents which implies D can access the member functions of its parents B and C, also there parent A. But the problem is A is the parent of both B and C so there are two ways for objects of class D to access the properties of A either via class B or via class C. When a function call is made from object of class D to the function of class A then the compiler will get confused about the path to approach class A this leads to an error.

Let’s see the code of above defined scenario

#include <bits/stdc++.h>usingnamespacestd;// defining class AclassA{public:voidhello_A(){cout<<"This is class A"<<endl;}};// defining class B// class B inherits from class AclassB:publicA{public:voidhello_B(){cout<<"This is class B"<<endl;}};// defining class C// class C inherits from class AclassC:publicA{public:voidhello_C(){cout<<"This is class C"<<endl;}};// defining class D// class D inherits from both class B and CclassD:publicB,publicC{public:voidhello_D(){cout<<"This is class D"<<endl;}};intmain(){// creating the object of class DDobj;// calling the function of the class A using object of class Dobj.hello_A();}
Enter fullscreen modeExit fullscreen mode

Error

HelloWorld.cpp:Infunction'intmain()':HelloWorld.cpp:52:9:error:requestformember'hello_A'isambiguousobj.hello_A();^~~~~~~HelloWorld.cpp:7:10:note:candidatesare:voidA::hello_A()voidhello_A()^~~~~~~
Enter fullscreen modeExit fullscreen mode

We get an ambiguous error here as the compiler has no idea which path it has to choose to reach class A. This problem is known as diamond problem, when there is diamond-like shape in our hybrid inheritance then there will be ambiguous error and to remove this error we define the parent class as the virtual class with the help of virtual keyword which means virtual base class is a way of defining virtual inheritance to prevent the multiple instances of a class.

How to declare virtual base class in C++?

Virtual base class is declared with the help of the virtual keyword.

Syntax

Syntax1:Addingvirtualkeywordbeforepublickeywordclasschild_class:virtualpublicparent_class{};Syntax2:Addingvirtualkeywordafterpublickeywordclasschid_class:publicvirtualparent_class{};
Enter fullscreen modeExit fullscreen mode

In the above syntax we defined a child class which inherits from the parent class public members and to make it virtual base class we just added virtual keyword by two ways either before public keyword or before access specifier or we can define it after access specifier as we have done in second syntax.

Let’s implement our above code by declaring class A as virtual parent class

#include <bits/stdc++.h>usingnamespacestd;// defining class AclassA{public:voidhello_A(){cout<<"This is class A"<<endl;}};// defining class B// class B inherits from class AclassB:virtualpublicA{public:voidhello_B(){cout<<"This is class B"<<endl;}};// defining class C// class C inherits from class AclassC:publicvirtualA{public:voidhello_C(){cout<<"This is class C"<<endl;}};// defining class D// class D inherits from both class B and CclassD:publicB,publicC{public:voidhello_D(){cout<<"This is class D"<<endl;}};intmain(){// creating the object of class DDobj;// calling the function of the class A using object of class Dobj.hello_A();}
Enter fullscreen modeExit fullscreen mode

Output:

ThisisclassA
Enter fullscreen modeExit fullscreen mode

We rewrite the code explained in the above section, only change is we define the base class (class A) as a virtual class using the virtual keyword while inheriting it in class B and class C that makes our code work properly.

Conclusion

  1. Virtual base class is a way of defining virtual inheritance to prevent the multiple instances of a class.
  2. When there is diamond-like shape in hybrid-inheritance, then there is a chance to get errors which can be prevented by declaring base class as virtual.
  3. To declare a virtual class, a virtual keyword is used while inheriting it in child class.
  4. To discover the virtual-base-class topic covered in detail please refer to thisScaler link.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Technopedia for Your Mastermind

More fromScaler Topics

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp