answersLogoWhite

0


Best Answer

A pure-virtual method may provide an implementation, but it is not a requirement. If you find that all your derived classes have the exact same implementation (either in whole or in part), then it makes sense to place that implementation within the abstract class, thus ensuring consistency across all derivatives (and thus reducing maintenance). Each derived class can then explicitly call the base class method and optionally augment it with a more specific implementation where required, much as you would with a non-pure virtual method. The only real difference is that you are expected to provide an override for a pure-virtual method, whereas overriding a non-pure virtual method is always optional.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Does pure virtual function contain any body?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


What is the difference between virtual function and function overriding?

Virtual Functions and Pure Virtual Functions are relevant in the context of class inheritance.Unlike Virtual Functions, Pure Virtual Functions do not require a body. This implies that when a base class defining such a function is inherited, the derived class must implement that function. Furthermore, the base class becomes abstract; meaning you cannot create an instance of the base class even if a body is implemented for the function. You are expected to derive from abstract classes; only the derived classes that implement all the inherited Pure Virtual functions can be instantiated.Here are some examples of Virtual and Pure Virtual function signatures:- Virtual Function: E.g. virtual void myFunction();- Pure Virtual Function: E.g. virtual void myFunction() = 0;


Can you have inline virtual functions in a class?

No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.


How do you represent a C plus plus non-pure virtual function in UML?

All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.


When do we make a virtual function pure?

We make a virtual function pure whenever we wish to make our class an abstract base class (an abstract data type). Unlike a virtual function, pure virtual functions must be overridden by a derived class or by one of its derivatives (the function remains pure virtual until it is overridden, at which point it becomes virtual). Derived classes that do not provide a complete implementation for all the pure virtual functions it inherits become abstract themselves. You cannot instantiate an abstract base class other than through derivation.

Related questions

A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


How do virtual functions differ from pure virtual functions?

Virtual functions is a function that can be overridden in inheriting class with the same signature (function name, parameters number, parameters types and return type);Pure virtual function is function that does not have implementation and if class has pure virtual function is called abstract. It is not possible to instantiate that class. Some other class must inherit it and define the body for it (implement). In other words class only have function prototype/declaration(signature) and no definition(implementation).


What is the difference between virtual function and function overriding?

Virtual Functions and Pure Virtual Functions are relevant in the context of class inheritance.Unlike Virtual Functions, Pure Virtual Functions do not require a body. This implies that when a base class defining such a function is inherited, the derived class must implement that function. Furthermore, the base class becomes abstract; meaning you cannot create an instance of the base class even if a body is implemented for the function. You are expected to derive from abstract classes; only the derived classes that implement all the inherited Pure Virtual functions can be instantiated.Here are some examples of Virtual and Pure Virtual function signatures:- Virtual Function: E.g. virtual void myFunction();- Pure Virtual Function: E.g. virtual void myFunction() = 0;


Can you have inline virtual functions in a class?

No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.


How do you represent a C plus plus non-pure virtual function in UML?

All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.


When do we make a virtual function pure?

We make a virtual function pure whenever we wish to make our class an abstract base class (an abstract data type). Unlike a virtual function, pure virtual functions must be overridden by a derived class or by one of its derivatives (the function remains pure virtual until it is overridden, at which point it becomes virtual). Derived classes that do not provide a complete implementation for all the pure virtual functions it inherits become abstract themselves. You cannot instantiate an abstract base class other than through derivation.


Write abstract class in c plus plus?

An abstract class is any class definition that contains at least one pure-virtual function. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


Pure Virtual Function Call Error On Roller Coaster Tycoon 3?

same thing at me


What are the merits and demerits of defining and declaring a pure virtual function in a program?

Merits of defining a pure virtual function: It enforces derived classes to implement the function, ensuring polymorphic behavior. It enables abstract classes to define a common interface. Demerits: It can hinder flexibility as derived classes must implement the function. It may also increase code complexity.


How dynamic binding acheived in c plus plus?

Dynamic binding is achieved via virtual functions and the virtual table that is associated with every class that declares or inherits a virtual function. The virtual table (or v-table) maps every virtual function (including pure-virtual functions) to a function pointer that points to the most-derived overload. This makes it possible to invoke specific behaviour even when the runtime type of the object is unknown to the caller.


What are some of the attributes of pure virtual function?

A pure-virtual function is not unlike a virtual function, except that you needn't provide a default implementation like you must do with a virtual function. But whether a generic implementation is provided or not, the class is rendered abstract, which means you cannot instantiate the class directly -- you must derive a new class. Indeed, the only reason for declaring a pure-virtual function is to render the class abstract. Why would you design a class that cannot be instantiated other than by derivation? There are many reasons, but ultimately the class is intended to represent a conceptual object rather than an actual object. A conceptual object is a generic object that encapsulates everything that is common to all its derivatives, but one that cannot provide a complete implementation of its interface. For instance, circles and squares are actual objects that share a common concept: they are both types of shape. Thus circles and squares (and all other shapes) may be derived from a generic shape object. The shape object can encapsulate everything that is common to all shapes, such as colour attributes, line width and style, and so on. It can also provide common interfaces such as draw(), rotate() and so on. But it cannot implement these interfaces without knowing what type of shape it actually is. While you could use expensive runtime type information within the base class to determine the type of shape and write reams of base class code with switch statements to cater for all possible shapes, this makes no sense (and would be a maintenance nightmare) when the derivatives have all the information required to implement these interfaces. Thus the abstract shape class exists to provide a common interface while each specific shape fills in the details and provides the actual implementation. The derivative must provide an implementation for the pure-virtual method (augmenting or replacing any generic implementation, where one exists) otherwise it becomes abstract itself. But once a derivative implements a pure-virtual function, the function becomes a normal virtual function with respect to any of its derivatives. That is, derived implementations can be inherited, but the base class implementation (if one exists) cannot be inherited. A pure-virtual function is declared like any other virtual function, by prefixing the function with the virtual keyword. The difference is that you must append =0 after the function signature: class abstract { public: virtual void my_function()=0; }; If a generic function body is require, it can be declared externally: void abstract::my_function() { // ... function body goes here } Or it can be declared inline: class abstract { public: virtual void my_function()=0{ /* ... function body goes here */ } };


What should be structure of class when it has to be a base for other classes?

If a class is intended to be used purely as a base class then it must have one or more pure-virtual functions (a function that may or may not have an implementation). Such a class is regarded as being an abstract base class because no instances of the class can be instantiated other than through derivation, and the derivative must provide an implementation or it too becomes an abstract base class. The abstract base class need not declare a pure-virtual method if it inherits one or more pure-virtual methods from another base class but does not provide a complete implementation. Only classes that provide a complete implementation can actually be instantiated. Implementations may be inherited from lower base classes other than the one that declared the function pure-virtual in the first place. Once a derivative implements a pure-virtual function, that implementation may be inherited by subsequent derivatives. If the base class may be instantiated in its own right then it must not declare any pure-virtual methods, nor must it inherit any pure-virtual methods that have no implementations. In this case the base class may be derived from, but doesn't have to be derived from, whereas an abstract base class must be derived from.