C++: why pure virtual function has definition?Please look into sample code here
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: C++: why pure virtual function has definition?Please look into sample code here

  1. #1
    Join Date
    Feb 2005
    Location
    Tamilnadu, India
    Posts
    62

    Question C++: why pure virtual function has definition?Please look into sample code here

    Actually pure virtual funtion should n't have implementation (viod Fun( ) = 0). Also this function cannot be called in derived class. Please look at the sample code.Here the class Base has pure virutual function, But it is allowing to have definition, even it can be called from drived class. There is no compilation error and Run time error. why? Anyone Can please give details about it?
    Is there any special purpose to allow this?

    #include <iostream.h>
    class Base
    {
    public:
    virtual void Fun( )=0
    {
    cout<<"\n I am in Pure virtual function\n";
    }
    };
    class Derived:Public Base
    {
    public:
    void Fun()
    {
    cout<<"\n I am in the Derived class";
    Base::Fun( );
    }
    };
    void main()
    {
    Derived d;
    Base *b = &d;
    b->Fun();
    }
    Regards,
    Rameshkanth
    BORN TO WIN

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: C++: why pure virtual function has definition?Please look into sample code here

    A pure virtual function can have a body. Why? Because the purpose of the "pure virtual" is to force instantiable children classes to implement it.
    Har Har

  3. #3
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: C++: why pure virtual function has definition?Please look into sample code here

    If I remember well, somewhere in "Effective C++" Meyers mentions a reason for a pure virtual function to have a body:
    Derived classes that implement this pure virtual function may call this implementation smwhere in their code.
    If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy, even if the function should be pure virtual.
    Extreme situations require extreme measures

  4. #4
    Join Date
    Feb 2005
    Location
    Tamilnadu, India
    Posts
    62

    Question Re: C++: why pure virtual function has definition?Please look into sample code here

    Thanks for your quick response. I agree with your point that the pure virtual function purpose is to force the derived class to override it.

    But my doubt if we are able to call that function from the derived classes, why are we not allowed to create an object for that class and call that function with that object.
    Regards,
    Rameshkanth
    BORN TO WIN

  5. #5
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: C++: why pure virtual function has definition?Please look into sample code here

    Quote Originally Posted by ksrameshkanth
    But my doubt if we are able to call that function from the derived classes, why are we not allowed to create an object for that class and call that function with that object.
    Using implementation code from a base class has nothing to do with allowing creation of objects of the base class. These are totally unrelated.
    When designing ask yourself:
    - Do I need to be able to instantiate objects of the base class? If not the base class should be abstract (contains at least one pure virtual function).
    - Is there common code that derived classes may want to use? Then consider providing this in the body of a pure virtual function.
    Extreme situations require extreme measures

  6. #6
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Thumbs up Re: C++: why pure virtual function has definition?Please look into sample code here

    Purpose of a Pure Virtual Function is to make a Class to Abstract Class.
    Often in a design, you want the base class to present only an interface for
    its derived classes. That is, you donít want anyone to actually create an
    object of the base class, only to upcast to it so that its interface can be
    used. This is accomplished by making that class abstract, which happens if
    you give it at least one pure virtual function. You can recognize a pure
    virtual function because it uses the virtual keyword and is followed by = 0.
    If anyone tries to make an object of an abstract class, the compiler
    prevents them. This is a tool that allows you to enforce a particular design.

    When an abstract class is inherited, all pure virtual functions must be
    implemented, or the inherited class becomes abstract as well. Creating a
    pure virtual function allows you to put a member function in an interface
    without being forced to provide a possibly meaningless body of code for
    that member function. At the same time, a pure virtual function forces
    inherited classes to provide a definition for it.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: C++: why pure virtual function has definition?Please look into sample code here

    If you have a "normal" virtual function, its implementation (body) becomes a sort of "default" implementation. That is, derived classes will inherit that implementation and use it if they don't override the function. This may or may not be a problem if the author of a derived class forgets to override a particular virtual function.

    By making the original function pure (but still with a body), you are saying to the author of a derived class "there's a default implementation of this function; if you want to use, you will have to be explicit about it".
    Code:
    class base
    {
    public:
    	virtual void f() = 0;
    };
    
    void base::f()
    {
    	// implementation
    }
    
    class derived : public base
    {
    public:
    	virtual void f()
    	{
    		base::f();  // explicit use of default implementation
    	}
    };
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: C++: why pure virtual function has definition?Please look into sample code here

    Well, the point has already been made. But some additions and a question at the end. There is a case where it becomes a necessity to provide the implementation for a pure virtual functions and that would be a pure virtual destructor.
    ISO C++ 12.4 (7):
    A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined. If a class has a base class with a virtual destructor, its destructor (whether user or implicitly declared) is virtual.
    Also, its not that since it has a body you need to call it explicitly. Its the other way round. If it needs to be called, then its body need to be implemented. The standard states it clearly:
    ISO C++ 10.4 (2):
    A pure virtual function need be defined only if explicitly called with the qualified id syntax (5.1).
    And hence when the call from the derived destructor is done while polymorphic destruction you need to have a body of the base pure virtual destructor implemented for it to be executed while Base's destruction.

    And now the question - Danny Kalev - here says that the body of the pure virtual destructor should always be empty. I could not get why does he say so? Comeau allows this. VC++ allows this. And standard doesnt have any mention to it. Is this something wrong that he has stated? Regards.
    Last edited by exterminator; September 9th, 2005 at 08:18 AM.

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: C++: why pure virtual function has definition?Please look into sample code here

    I would be happy with the statement if it's specified that he never puts data in an abstract class. Some people stick to this rule - I don't see the point of being that restrictive.

    It comes down to whether you only ever use abstract classes to mimic "interfaces", or whether you see them as part of a broader picture, one that sees a use for abstract classes with state information.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: C++: why pure virtual function has definition?Please look into sample code here

    I already had abstract classes with a few fields, used by non-virtual methods who called other virtual methods.
    I think that it is a restriction of the language to never put data in abstract classes, and it may need some duplicate code and data member... what is not good.

  11. #11
    Join Date
    Sep 2013
    Posts
    1

    Re: C++: why pure virtual function has definition?Please look into sample code here

    According to this link:

    Pure Virtual function

    "Having that implementation may be desirable when you think that classes which derive from the base class may need some sort of default behavior for the pure virtual function"

    Also, an interesting piece of information:

    "Pure virtual functions can not have a definition inside the function declaration", with the exception of Microsoft's visual C++.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center