Menu

Virtual class derivatives

2009-04-26
2012-09-26
  • Monkey Magic

    Monkey Magic - 2009-04-26

    Hello. I wonder if you guys could clear up some confusion for me!

    In the introductary C++ book that I am reading it mentions that if you declare a function within a class to be virtual, i.e:

    include <iostream>

    class Greeting
    {
    public: virtual void Hello() = 0;
    };

    then the entire class will be marked as being abstract, and you cannot compile any classes derived from that base class that do not include this virtual function.

    However, if I try and compile a new source file which includes this class, I can derive a class from it without any problems, i.e:

    include <iostream>

    include "test.cpp"

    class Hey: public Greeting
    {
    public: virtual void SomeFunction()
    {
    std::cout << "I never used the Hello() function!";
    }
    };

    Compiles fine. The book I am reading says that this file should not compile because "test.cpp" would be abstract and it should also tell me that it can't define "Hello()". With Dev-C++ this compiles without error.

    Is this a problem? Why does this compile without error in this compiler but not others? Is it something to do with new C++ standards? Or what? It's nothing serious, but I'm just curious why this won't compile in other compilers.

    Thanks,
    MonkeyMagics

     
    • Monkey Magic

      Monkey Magic - 2009-04-27

      The book is Learn to Program with C++ by John Smiley.

      http://www.amazon.co.uk/Learn-Program-C-John-Smiley/dp/0072225351

      It is actually a very good book compared with some others I flipped through - it was really only this matter of abstract classes that wasn't very clear, but to be honest that might just have been me not reading clearly. I found it explained all the other concepts quite clearly. It's aimed at the absolute beginner, though!

       
    • BoogieGLX

      BoogieGLX - 2009-04-26

      Try and create an object of the derived class, think, and you will get your answer.

       
    • cpns

      cpns - 2009-04-26

      > In the introductary C++ book that I am reading

      Always reference the book by title and author so we know what you are talking about. If you don't tell us that much telling us about the book at all waste waste of typing.

      > you cannot compile any classes derived from that base
      > class that do not include this virtual function.

      The books language is a little imprecise (or perhaps yours if you merely paraphrased it). In this case the statement is true only because your example is of a "pure virtual'. The = 0 at the end of teh declaration means "no default implementation", so a derived class cannot be instantiated unless it provides an implementation.

      > I can derive a class from it without any problems,

      Deriving from it is not the problem. You will not be able to instantiate an object of type Hey. Hey remains abstract. It could be further derived and once a subclass provides an implementation it is concrete and can be instantiated.

      > Why does this compile without error in this compiler but not others?

      Have you actually tried it in others? Have you tried instantiating Hey? For example:

      int main()
      {
      Hey test ;
      }

      should fail. It is instantiation of abstract classes that is an error, not definition of them. If you had to have an implementation in order to compile and abstract class you would not for example be able to include abstract classes in libraries, or even have teh abstract class and the implementing subclass in separate compilation units. It obviate teh usefulness of abstract classes almost entirely.

      > should not compile because "test.cpp" would be abstract

      A source file cannot be abstract, only the classes defined within the source file.

      Clifford

       
    • Monkey Magic

      Monkey Magic - 2009-04-27

      Thanks for clearing that up. The book implied that the entire source file would be declared abstract, which led to my confusion!

       
      • cpns

        cpns - 2009-04-27

        OK, but still you did not say which book! In this case that is probably important as "community knowledge". People sometimes ask for book recommendations, and if this one is confusing or imprecise it would be good to know.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.