answersLogoWhite

0


Best Answer

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 */ }

};

User Avatar

Wiki User

βˆ™ 11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are some of the attributes of pure virtual function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


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;


Why inherit function is not called?

Inherit is not a function. It is a class derivation where some of the methods and attributes of the new class inherit from a parent class.


What is virtual function in c plus plus?

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class


Application of object-oriented programming?

AnswerA virtual function must be declared as a non-static member method of a class that you expect to act as a base class. Declaring a virtual function adds some overhead as a result of creating the v-table (virtual method table), but most of that overhead is paid with the first virtual function (subsequent virtual functions just add a new entry to the already-existing v-table). However, do not declare a virtual function unless you expect that function to be overridden. Bear in mind that overriding an overloaded, non-virtual function "hides" all the overloads in the base class.If a virtual function must be overridden, declare it as pure-virtual instead. You do not need to implement the method in the base class, but you will be reminded to provide an implementation in the derived class at compile time if one does not exist, even if you provide a default implementation in the base class. Bear in mind that base classes with one or more pure-virtual methods become abstract -- they cannot be instantiated.If there is any virtual function or pure-virtual function, there must also be a virtual destructor, as well as a public or protected default constructor (a constructor with no arguments). When a derived class is constructed, it calls the base class constructor, which calls its base class constructor. Derived classes are constructed in sequence, beginning with the least-derived class. Destruction is the reverse -- the most-derived class is destroyed before the base classes are destroyed.Virtual functions can be invoked just like any other class member method, both via an object reference's member operator (.), and the indirection operator (-->) for pointers to objects. It does not matter whether the reference or pointer refers to a base class or a derived class; the v-table decides which override (where one is provided) will actually execute, starting from the most-derived override and working back towards the base class, the least-derived. With appropriate use of virtual functions, dynamic casting can be avoided completely (dynamic casting should never be employed as it completely defeats the point of having a v-table in the first place).

Related questions

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.


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;


What are the attributes of a cosine function?

There are many "attributes" of a cosine function. Some examples of attributes are as follows: For, constants a, b, n, y=a*cos(nx)+b has an amplitude of a, a period of 2pi/n, a range of [-a+b,a+b], a derivative of y'=-an*sin(nx).


Why inherit function is not called?

Inherit is not a function. It is a class derivation where some of the methods and attributes of the new class inherit from a parent class.


Rules of virtual function?

AnswerA virtual function must be declared as a non-static member method of a class that you expect to act as a base class. Declaring a virtual function adds some overhead as a result of creating the v-table (virtual method table), but most of that overhead is paid with the first virtual function (subsequent virtual functions just add a new entry to the already-existing v-table). However, do not declare a virtual function unless you expect that function to be overridden. Bear in mind that overriding an overloaded, non-virtual function "hides" all the overloads in the base class.If a virtual function must be overridden, declare it as pure-virtual instead. You do not need to implement the method in the base class, but you will be reminded to provide an implementation in the derived class at compile time if one does not exist, even if you provide a default implementation in the base class. Bear in mind that base classes with one or more pure-virtual methods become abstract -- they cannot be instantiated.If there is any virtual function or pure-virtual function, there must also be a virtual destructor, as well as a public or protected default constructor (a constructor with no arguments). When a derived class is constructed, it calls the base class constructor, which calls its base class constructor. Derived classes are constructed in sequence, beginning with the least-derived class. Destruction is the reverse -- the most-derived class is destroyed before the base classes are destroyed.Virtual functions can be invoked just like any other class member method, both via an object reference's member operator (.), and the indirection operator (-->) for pointers to objects. It does not matter whether the reference or pointer refers to a base class or a derived class; the v-table decides which override (where one is provided) will actually execute, starting from the most-derived override and working back towards the base class, the least-derived. With appropriate use of virtual functions, dynamic casting can be avoided completely (dynamic casting should never be employed as it completely defeats the point of having a v-table in the first place).Answer1.the virtual function should be a member of some class 2.they cannot be a static member 3.they are accessed by using object pointers


What is virtual function in c plus plus?

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class


Application of object-oriented programming?

AnswerA virtual function must be declared as a non-static member method of a class that you expect to act as a base class. Declaring a virtual function adds some overhead as a result of creating the v-table (virtual method table), but most of that overhead is paid with the first virtual function (subsequent virtual functions just add a new entry to the already-existing v-table). However, do not declare a virtual function unless you expect that function to be overridden. Bear in mind that overriding an overloaded, non-virtual function "hides" all the overloads in the base class.If a virtual function must be overridden, declare it as pure-virtual instead. You do not need to implement the method in the base class, but you will be reminded to provide an implementation in the derived class at compile time if one does not exist, even if you provide a default implementation in the base class. Bear in mind that base classes with one or more pure-virtual methods become abstract -- they cannot be instantiated.If there is any virtual function or pure-virtual function, there must also be a virtual destructor, as well as a public or protected default constructor (a constructor with no arguments). When a derived class is constructed, it calls the base class constructor, which calls its base class constructor. Derived classes are constructed in sequence, beginning with the least-derived class. Destruction is the reverse -- the most-derived class is destroyed before the base classes are destroyed.Virtual functions can be invoked just like any other class member method, both via an object reference's member operator (.), and the indirection operator (-->) for pointers to objects. It does not matter whether the reference or pointer refers to a base class or a derived class; the v-table decides which override (where one is provided) will actually execute, starting from the most-derived override and working back towards the base class, the least-derived. With appropriate use of virtual functions, dynamic casting can be avoided completely (dynamic casting should never be employed as it completely defeats the point of having a v-table in the first place).


Why cant you use all of your systems memory to run a virtual machine?

You can't use all of your system memory to run a virtual machine because then there wouldn't be enough memory for the Operating System that's running the virtual machine to function. For instance, if you have 4 GB of system memory total, and your operating system needs at least a quarter of that (1 GB) to function, then your virtual machine maximum memory would be 3 GB.


What are some of the attributes of a computerized accounting system?

what are some attributes of computerized accounting


What is the difference between base class and derived class in c plus plus?

There is no difference other than that a derived class inherits from a base class. That is, the derived class inherits all the public and protected members of its base class, and is a more specialised form of its base class. Thus both can be treated as if they really were base classes. The derived class can also override the virtual methods of the base class, thus allowing polymorphic behaviour without the need to know the exact type of the derived class.


What type of graph shows attributes?

All graphs show attributes. Some attributes may be qualitative, others quantitative.