none
deriving abstract classes RRS feed

  • Question

  • I have written a simple program the demonstrates abstract classes.

    //Abstract.h

    #include<iostream>
     class Abstract_class
    {

    public:
    void virtual printFunc() const;
    };


     class myclass : public Abstract_class
     {
     public:
    void printFunc(){
    std::cout << "myclass print function" << std::endl;
    }

     };

    //main.cpp

    #include"Abstract.h"

    int main()
    {

    myclass mc;
    mc.printFunc();
    return 0;
    }

    The program throws the error 

    Kindly help me resolve this!

    Friday, May 8, 2015 10:27 PM

Answers

  • Abstract_class::pureFunc() is not purely virtual, so it needs a definition.  If you want to make it purely virtual, use '= 0' after its declaration, like this:

    class Abstract_class
    {
    
    public:
       void virtual printFunc() const = 0;
    };
    
    

    • Marked as answer by Shu 2017 Tuesday, May 19, 2015 2:03 AM
    Friday, May 8, 2015 10:39 PM

All replies

  • Abstract_class::pureFunc() is not purely virtual, so it needs a definition.  If you want to make it purely virtual, use '= 0' after its declaration, like this:

    class Abstract_class
    {
    
    public:
       void virtual printFunc() const = 0;
    };
    
    

    • Marked as answer by Shu 2017 Tuesday, May 19, 2015 2:03 AM
    Friday, May 8, 2015 10:39 PM
  • Migh be you will be also interested to know that You also can use following syntax as well. Where PURE is defined inside the objbase.h . 

    class Abstract_class
    {
    
    public:
       void virtual printFunc()  PURE;
    };
    Thanks

    Rupesh Shukla


    Sunday, May 10, 2015 1:45 AM