why pure virtual function is needed
-
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
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.
-
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
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.
-
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
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
-
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.
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.
-
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
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