0
class Base {  
public:  
  Base(){ }  
  virtual void Bfun1();  
  virtual void Bfun2();  
};  

class Derv : public Base {  
public:  
  Derv(){ }  
  void Dfun1();  
};  

Is there a difference between above definitions and the below ones ? Are they same ? if not how both are the different functionally ?

class Base { 
public:   
  Base(){ }  
  void Bfun1();  
  void Bfun2();  
};  

class Derv : public virtual Base {  
public:  
  Derv(){ }  
  void Dfun1();  
};  
Xeo
  • 129,499
  • 52
  • 291
  • 397
irappa
  • 749
  • 4
  • 11
  • typo.. read constructors as public. class Base { public: Base(){ } virtual void Bfun1(); virtual void Bfun2(); }; class Derv : public Base { Derv(){ } void Dfun1(); }; Is there a difference between above definitions and the below ones ? Are they same ? if not how both are the different functionally ? class Base { Base(){ } public: void Bfun1(); void Bfun2(); }; class Derv : public virtual Base { Derv(){ } public: void Dfun1(); }; – irappa May 28 '11 at 23:14
  • Short answer: Yes, they're different. Long answer: Read your book ;) – Lightness Races in Orbit May 28 '11 at 23:17
  • Reading suggestion : [What is the "dreaded diamond"?](http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8) ... and [Where in a hierarchy should I use virtual inheritance?](http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.9) – Nawaz May 28 '11 at 23:26
  • 1
    `virtual` methods and `virtual` inheritance are quite different concepts. You are essentially asking two different questions. The same keyword is reused. You should try to understand each separately before trying to understand them together. – Aaron McDaid May 28 '11 at 23:27
  • typo.. read constructors as public. class Base { public: Base(){ } virtual void Bfun1(); virtual void Bfun2(); }; class Derv : public Base { public: Derv() { } void Dfun1(); }; Is there a difference between above definitions and the below ones ? Are they same ? if not how both are the different functionally ? class Base { public: Base() { } void Bfun1(); void Bfun2(); }; class Derv : public virtual Base { public: Derv() { } void Dfun1(); }; – irappa May 28 '11 at 23:30
  • @irappa: You can fix typos yourself using the 'edit' link at the bottom of your question. – johnsyweb May 29 '11 at 01:16
  • possible duplicate of [In C++ virtual base class?](http://stackoverflow.com/questions/21558/in-c-virtual-base-class) – Bo Persson May 29 '11 at 06:06

1 Answers1

2

They are completely different. The first set defines Bfun1 and Bfun2 as virtual function, that allows overriding them in the derived class and call those in the derived class through a base class pointer:

// assume you've overridden the functions in Derived
Base* base_ptr = new Derived;
base_ptr->Bfun1(); // will call function in derived

The second set, however, they're just normal functions. Instead, you declared the base class to be virtual, which has many implications you best read about in a good C++ book or search through the SO questions, I think we have one on that topic.

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397