answersLogoWhite

0


Best Answer

There are no pure virtual functions, but there are pure virtual function calls.

In C++, classes can define abstract functions. Those are virtual functions for which no implementation is supplied in the class that defines it. Abstract functions are typically used to define a contract. For example, a base class for any vehicle might guarantee that a move() method exists, but only classes derived from vehicle implement this method according to the properties of the specific vehicle: a bike, a car, a skateboard, etc. Classes containing abstract functions are sometimes called abstract classes.

The C++ compiler does not allow the creation of an object of type vehicle in this example, because the vehicle class contains an abstract function.

Classes derived from vehicle (derived in first or higher degree) must supply an implementation of this method before the compiler allows the creation of an object of this type.

Therefore, any valid object descended from vehicle must have an implementation of the move method. However, since it is possible to create pointers to abstract classes (classes that contain abstract functions which have not yet been given an implementation), pure virtual function calls can occur at runtime.

Consider this example:

class vehicle {

public:

virtual void move(void) = 0; // abstract function

};

class motorizedVehicle : vehicle {

...

};

class car : motorizedVehicle {

public:

virtual void move(void) {

... // implementation

}

};

An application cannot create objects of type vehicle or motorizedVehicle, but it can create pointers to those. Those might give access to classes that have no implementation for the move method, and calling it results in a run-time diagnostic known as a pure virtual function call.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

A virtual function is a function that is expected to be overridden by derived classes, but need not be overridden if the generic base class implementation is sufficient. Typically a derived class will simply augment the generic implementation to provide a more specific implementation, but it can also replace the generic implementation outright.

A pure-virtual function is a function that must be overridden by derived classes. The base class need not provide any implementation for its pure-virtual functions, but even those it does provide cannot be inherited. Only the interface is inherited, but a derived class may (optionally) call a generic implementation if one is provided.

A class with one or more pure-virtual functions automatically becomes an abstract data type, and cannot be instantiated directly; it must be derived from because its implementation is incomplete whether a generic implementation exists or not. Only derivatives can instantiate their abstract base classes, but if they do not provide a complete implementation they, too, become abstract. However, any implementations they do provide can subsequently be inherited by a lower derivative because the function then becomes virtual rather than pure-virtual at that point. Only the base class implementations cannot be inherited, but all pure-virtual functions must be implemented or inherited from higher derivatives in order to instantiate objects of that class.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The main difference is that virtual functions must be implemented but may be overridden, while pure virtual functions may be implemented but must be overridden.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain a pure virtual function with an example?
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. };


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.


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;


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).


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.


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;


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 purevirtual functions?

A pure virtual function is a virtual function initialized to zero in the base class. It is used because sometimes derived classes need to override a base class virtual function. A pure virtual function must be overrided in all derived classes. It has to be defined in all derived classes failing which leads to a compile-time error. I will give you an example to substantiate my answer. #include <iostream> using namespace std; class number { protected: int val; public: void setval(int i) { val = i; } // show() is a pure virtual function virtual void show() = 0; }; class hextype : public number { public: void show() { cout << hex << val << "\n"; } }; class dectype : public number { public: void show() { cout << val << "\n"; } }; class octtype : public number { public: void show() { cout << oct << val << "\n"; } }; int main() { dectype d; hextype h; octtype o; d.setval(20); d.show(); // displays 20 - decimal h.setval(20); h.show(); // displays 14 - hexadecimal o.setval(20); o.show(); // displays 24 - octal return 0; }


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.