Welcome to EZDefinition.com
Technological Concepts, Abbreviations & Definitions
Main Menu
Main categories
  • Operating Systems
  • Computer Hardware
  • Internet
  • Programming Languages
  • Multimedia
  • Software
  • Security and Encryption
  • Communications and Networking
  • Organizations
  • Books
  • Databases
  • Games
  • E-commerce

    [an error occurred while processing this directive]

  • EZDefinition Sponsor
    Please visit our sponsor Parosoft.com
    Related Links to Pure virtual definitions
    [an error occurred while processing this directive]
    Pure virtual definitions
    [an error occurred while processing this directive]
    Computer Technologies  Programming Languages  C++ Pure virtual definitions

    Pure virtual definitions

    Pure virtual definitions

    It’s possible to provide a definition for a pure virtual function in the base class. You’re still telling the compiler not to allow objects of that abstract base class, and the pure virtual functions must still be defined in derived classes in order to create objects. However, there may be a common piece of code that you want some or all of the derived class definitions to call rather than duplicating that code in every function.

    Here’s what a pure virtual definition looks like:

    //: C15:PureVirtualDefinitions.cpp
    // Pure virtual base definitions
    #include <iostream>
    using namespace std;
     
    class Pet {
    public:
      virtual void speak() const = 0;
      virtual void eat() const = 0;
      // Inline pure virtual definitions illegal:
      //!  virtual void sleep() const = 0 {}
    };
     
    // OK, not defined inline
    void Pet::eat() const {
      cout << "Pet::eat()" << endl;
    }
     
    void Pet::speak() const { 
      cout << "Pet::speak()" << endl;
    }
     
    class Dog : public Pet {
    public:
      // Use the common Pet code:
      void speak() const { Pet::speak(); }
      void eat() const { Pet::eat(); }
    };
     
    int main() {
      Dog simba;  // Richard's dog
      simba.speak();
      simba.eat();
    } ///:~

    The slot in the Pet VTABLE is still empty, but there happens to be a function by that name that you can call in the derived class.

    The other benefit to this feature is that it allows you to change from an ordinary virtual to a pure virtual without disturbing the existing code. (This is a way for you to locate classes that don’t override that virtual function.)

     


    [an error occurred while processing this directive]

    [an error occurred while processing this directive]
     

    All Rights Reserved

    Terms of usage   Please read our privacy stetment
    Copyright © 1999-2006 EZDefinition.com

     

    [an error occurred while processing this directive]