Home / Tips / C++ Tips / C++: Virtual Multiple Inheritance

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C++: Virtual Multiple Inheritance


Virtual Multiple Inheritance


A class b is defined having member variable i. Suppose two classes d1 and d2 are derived from class b and a class multiple is derived from both d1 and d2. If variable i is accessed from a member function of multiple then it gives an error as 'member is ambiguous'. To avoid this error derive classes d1 and d2 with modifier virtual as shown in the following program.

#include <iostream.h> 
class b 
{ 
   public : 
      int i ; 
   public : 
      fun( ) 
      { 
         i = 0 ; 
      } 
} ; 
class d1 : virtual public b 
{ 
   public : 
      fun( ) 
      { 
         i = 1 ; 
      } 
} ; 
class d2 : virtual public b
{ 
   public : 
      fun( ) 
      { 
         i = 2 ; 
      } 
} ; 
class multiple : public d1, public d2 
{ 
   public : 
      fun( ) 
      { 
         i = 10 ; 
      } 
} ; 
void main( ) 
{ 
   multiple d ; 
   d.fun( ) ; 
   cout << d.i ; 
} 

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts