Thread: Information Regarding Pure Virtual Functions

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Information Regarding Pure Virtual Functions


    The main difference between a pure virtual function and a regular virtual function is _______.
    A. the return type
    B. the inheritance properties
    C. a pure virtual function cannot have an implementation.
    D. the location in the class

    Posted by Mister C
    Sometime before this question was posted in this board and we had a lengthy discussion.

    Many would pick 'C'.

    Reality:

    The only difference between a pure virtual function and regular virtual function is that pure virtual function makes a class "Abstract" and regular virtual function does not make the class "Abstract"

    Polymorphic OOP made the below mentioned statement
    Pure virtual destructors MUST have a body

    Added Information:
    Whether a virtual function is pure or regular, it CAN have a body.

    Hope this is of some help to your folks
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Whether a virtual function is pure or regular, it CAN have a body.

    Please show me example code of a pure virtual function WITH a body.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Why did you make a new thread about this? You should've bumped the old one if you wanted to contribute more to it.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    We already made this point, there is no reason for you to bring it up again...

    Anyways, here's an example Davros:

    Code:
    class A
    {
        virtual void Blah() = 0;
    };
    
    void A::Blah()
    {
    }

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    Question

    I was just going thru a web site,
    I encountered a statement saying that

    "( pure )virtual functions can be private or public."

    now what's the using of keeping a ( pure ) virtual function private.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    They can be protected as well, not just public and private.

    The primary reason I can think of that one would make a pure virtual function private are for affecting the way that the object can be dealt with through pointers and/or references.

    It all stems primarily from the fact that a virtual function can be called from a member function in the base, so the member function can be called from within the base class member functions, just not externally.

    Also, remember that you can change the access of a member function in derived versions of the class. What's private in the base class isn't necissarily private in a child.

    Let's say you have a pure virtual function in a class "Base," but you only want that function to be called through pointers to its children. In other words, you want to be able to call the function through a pointer to a child or an object of a child type, but never through a pointer to its base.

    One solution would be to just not put the function in the Base class, but the problem with this is that it doesn't hold the logic that all of the children should have that member function. You want to keep that logic but make it so you can't explicitly call it through a pointer to a base object.

    If, instead, you make the member function pure virtual and private, you're ensuring that people MUST make their own versions of that virtual function in the Base type's children if they want to instantiate those types.

    Then, if necissary, the virtual function can be made protected, public, or left private, to take advantage of that access specifier.

    Example:

    Code:
    #include<iostream>
    
    class Base
    {
    public:
        void DoStuff();
    private:
        virtual void Func() = 0;
    };
    
    class Child
        : public Base
    {
    public:
        virtual void Func();
    };
    
    class BadChild
        : public Base
    {
    };
    
    void Base::DoStuff()
    {
        /* Stuff Goes Here */
    
        Func(); // Perfectly valid
    }
    
    void Child::Func()
    {
        std::cout << "B\n";
    }
    
    int main()
    {
        Child ChildObject;
        //BadChild BadChildObject; //Invalid, Func was not defined
    
        Base& BaseObjectReference = ChildObject;
    
        ChildObject.Func(); // This works!!!
    
        BaseObjectReference.DoStuff(); // This works!!!
    
        //BaseObjectReference.Func(); //Invalid! Func is private!!!
    
        return 0;
    }
    Oh the many wonders of OOP

    EDIT: Disabled those smilies
    Last edited by Polymorphic OOP; 01-29-2003 at 05:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  2. Replies: 2
    Last Post: 10-02-2005, 05:04 PM
  3. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 09:08 AM
  4. runtime error R6025 - pure virtual function call
    By nvoigt in forum Windows Programming
    Replies: 3
    Last Post: 07-11-2004, 12:50 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM