Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why pure virtual function is needed

why pure virtual function is needed

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aloktambi
    wrote on last edited by
    #1

    sir, i am confused about pure virtual function .why it is needed in abstract base class. in which cases it is use. plz tell me about pure virtual function & abstract base class

    C R M V 4 Replies Last reply
    0
    • A aloktambi

      sir, i am confused about pure virtual function .why it is needed in abstract base class. in which cases it is use. plz tell me about pure virtual function & abstract base class

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Pure virtual functions can be used to force other developper who will create classes inherited of your class to define this specific function. If this developper implemented a class that inherits yours and didn't implement the function, whenever you try to instantiate this class, the compiler will generate an error saying that the function has not been define in the inherited class. It's a way to ensure that all child classes will implement their own virtual function.

      R 1 Reply Last reply
      0
      • A aloktambi

        sir, i am confused about pure virtual function .why it is needed in abstract base class. in which cases it is use. plz tell me about pure virtual function & abstract base class

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        Here's an example of something I did with an abstract base class. This class was a thread class that would start up the thread. It was its own class because that would allow derived instances of it to have their own set of properties and capabilities in addition to those supplied by the base class. The pure virtual methd of the class is the thread procedure itself. One makes their own thread objects by deriving from this base class and implementing the thread procedure method. Other methods are available to create the thread, to initialize it, and to clean up after it. Each of those mentioned are virtual to allow them to be overridden but they also have default implementations. In the case of the thread procedure method a thread is useless without one so making it a pure virtual method requires all derived class to provide an implementation of it. Does this makes sense ? BTW - about this thread class, normally class methods can't be used as thread procedures but these threads use a static member method as the thread procedure and its argument is the this pointer of the thread object. The static member method calls the derived class' thread procedure method.

        1 Reply Last reply
        0
        • A aloktambi

          sir, i am confused about pure virtual function .why it is needed in abstract base class. in which cases it is use. plz tell me about pure virtual function & abstract base class

          M Offline
          M Offline
          MailtoGops
          wrote on last edited by
          #4

          cedric moonen is right... Here Why you need Abstract class.. Consider the basic senario of having shape classes like Line, Rectangle, etc. In the real world, there is nothing called Shape... you cannot define a Shape,, The object could be regular, irregular... But you cannot not define Shape anywhere... Line is a Shape Circle is a Shape Rectangle is a Shape But Shape is not Shape... (U might confused more..if it is i am really sorry :) ) But if you look at Line, Circle, Rectangle they are measarable..ie you can find area, they will have line length, width, color, style etc.. So considering these features we have, all are put in to a Shape class in order to resuse the existing implementation... But as I told Shape is not Shape... You cannot draw a Shape.. So you need to protect the user to create Shape object...For that you need to make that class as abstract ie it must to have a pure virtual function, Pure virtual function doesn't has implementation (In c++ context) " Action without vision is only passing time, Vision without action is merely day dreaming, But vision with action can change the world " - Words from Nelson Mandela Thanks & Regards, Gopalakrishnan

          1 Reply Last reply
          0
          • C Cedric Moonen

            Pure virtual functions can be used to force other developper who will create classes inherited of your class to define this specific function. If this developper implemented a class that inherits yours and didn't implement the function, whenever you try to instantiate this class, the compiler will generate an error saying that the function has not been define in the inherited class. It's a way to ensure that all child classes will implement their own virtual function.

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #5

            You may think of an abstract base class as an interface. The interface defines how inherited classes are utilized. The base class is abstract and hence cannot be created. The same goes for interfaces: there's no point in create an interface unless there's an object behind it. This is also how COM interfaces are defined in C++. aloktambi wrote: why it is needed in abstract base class. Without a pure virtual function the class is not abstract.

            1 Reply Last reply
            0
            • A aloktambi

              sir, i am confused about pure virtual function .why it is needed in abstract base class. in which cases it is use. plz tell me about pure virtual function & abstract base class

              V Offline
              V Offline
              vikas amin
              wrote on last edited by
              #6

              Its quiet simple It forces the programer to define the function in the dervied class that is a pure virtual fun in the base class. Vikas Amin Embin Technology Bombay vikas.amin@embin.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups