Quantcast
answersLogoWhite
notificationBell

Top Answer
User Avatar
Wiki User
Answered 2012-06-06 21:04:20

You declare a pure-virtual method in base classes that are intended to provide a common interface to two or more derived classes. Any class that declares a method as pure-virtual becomes an abstract base class (ABC), also known as an abstract data type (ADT).

You cannot instantiate abstract classes, but that's fine because the intent is to derive classes from the abstract class and define specialised implementations for the pure-virtual methods in each of the derived classes. The base class exists purely to provide a common interface to all its derivatives.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Still Have Questions?

Related Questions

What are the differences between a virtual function and a pure virtual function?

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 is difference between pure virtual function and virtual function?

A pure virtual function can not be instanced directly, only in a derived class. A normal virtual function has no such requirement.


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. };


Why use pure virtual function?

You declare a pure virtual function whenever you wish your class to be an abstract data type and require all derivatives of the class to provide an implementation for the pure virtual function. Unlike a non-pure virtual function which needn't be overridden by derivatives, pure virtual functions must be overridden. Once a derivative provides an implementation, that function becomes non-pure virtual, and can then be inherited by derivatives of that derivative.


What is a pure virtual member function in c plus plus?

When we declare a pure virtual member function, the class in which it is declared becomes an ADT (abstract data type). The base class that declared the pure-virtual function need not provide an implementation for it. Unlike a virtual function which maybe overridden by a derived class, a pure-virtual function must be overridden by a derived class, otherwise it, too, becomes an ADT. However, once overridden, a pure-virtual function becomes a normal virtual function with respect to further derivatives.


What is pure virtual function in c plus plus?

A pure vitual function is a function declared in base class that has no definition relative to base class .In that cases compiler requires each derived class either to define the function or to redeclare it as a pure virtual function.The class with pure virtual functions cannot be used to declare any objects any objects of its own. A pure virtual function is a "do-nothing function" declaration : virtual void funtionname()=0;


Can a class have both a virtual and a pure virtual function in c plus plus?

Yes. Any non-derived class that declares at least one virtual function must also have a virtual destructor. It makes no difference if the virtual function is pure-virtual or just virtual; without a virtual destructor, derived classes cannot be destroyed when base classes fall from scope. However, once a destructor is declared virtual, all derived destructors are effectively virtual through implication (the virtual keyword is optional when overriding an inherited virtual interface).The only major difference between a pure-virtual function and virtual function is that a pure-virtual function will render your class abstract. A generic implementation for a pure-virtual function is entirely optional, but derived classes cannot inherit that implementation (although they can explicitly call it) and they must provide an implementation for that function (or it will be rendered abstract as well). Once a derived class implements a pure-virtual function, that function is effectively rendered virtual and can then be inherited by subsequent derivatives (access permitting). However, only classes that either provide or inherit a complete implementation for all pure-virtual methods can be physically instantiated.When you declare a virtual function, you must provide a generic implementation, but it need not be overridden by a derivative if the generic implementation suffices for that derivative.Ultimately, any class may have any combination of non-virtual, virtual and pure-virtual functions. The purpose of the class will ultimately determine which combination you need.


Distinguish between a virtual function and pure virtual function with a suitable example?

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.


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.


Distinguish between virtual functions and pure virtual function?

Virtual functions are expected to be overridden by derived classes. Pure-virtual functions must be overridden by derived classes.


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 a Pure Virtual Function Write a C plus plus program using Pure Virtual Function?

A pure-virtual method is similar to a virtual method. A virtual method is a method that is expected to be overridden in a derived class while a pure-virtual method must be overridden in a derived class. The base class may provide a default implementation for a pure-virtual method, but it is not a requirement, whereas it must provide an implementation for a virtual method. In addition,any base class that has a pure-virtual method becomes abstract; you cannot instantiate abstract classes -- you are expectedto derive from them.#include class Abstract{public:// Pure-virtual method; note the '=0' to denote pure-virtual.virtual void Show()=0;};class Derived : public Abstract{public:virtual void Show(){// Implementation is required here!std:: cout }};void main(){// Abstract a; // Compiler error!Derived d;d.Show(); // Call pure-virtual method.return( 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.


Why does your class have a pure virtual function?

so it can call it polymorphic-ally.


What is pure virtual functions?

In C++, a pure virtual function is a function declaration that has no implementation in the class where it is declared, but is rather left to the child classes to override and define the function. This means that the parent class cannot be instantiated. In Java this is known as an abstract class or interface.


How do you declare pure virtual function?

Declare the function as you would a virtual function, but add "=0" before the semi-colon or function body. The following example demonstrates overloaded constant and volatile pure virtual functions: struct foo { virtual const int bar( void ) const = 0; virtual int bar( void ) = 0; }; Note that unlike a virtual function which is expected to be overridden, a pure virtual function must be overridden. As such, a generic implementation is optional. If a generic implementation is required, it may be defined inside or outside the class declaration as per any other function. However, as with all class methods, all implementations should ideally be defined outside of the class declaration in order to maintain separation from the interface.


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. };


What are the implecations of making a function pure virtual function?

A pure virtual function is a base class function that must be overridden by a derived class. The base class need not provide a default implementation of the function. In addition, the base class becomes an abstract base class, which means no object of that class can be instantiated other than through derivation. A derived class that does not override a pure virtual function is itself an abstract base class. Once overridden by a derived class, the pure virtual function becomes an ordinary virtual function with respect to all other derivates of that class. By contrast, a virtual function is a base class function that is expected to be overridden by derived classes. The base class must provide a default implementation and the base class can be instantiated as an object in its own right. Any class that declares at least one virtual function or at least one pure virtual function must also declare a virtual destructor. This ensures that whenever an object of the class implicitly falls from scope or is explicitly destroyed through a reference or pointer to one of its base classes, the most-derived destructor is always invoked first, even if the most-derived class is not known to the programmer. This ensures the object's class hierarchy is destroyed in the reverse order it was constructed.



What are the implications of making a function pure virtual function?

When we declare a pure virtual function, the class in which it is first declared becomes an abstract base class (an incomplete class). This means we cannot instantiate that class other than through derivation. Moreover, abstract base classes need not implement their pure virtual interfaces. A derived class that inherits a pure virtual function must override that function otherwise it, too, becomes an abstract base class. However, once overridden by a derived class, a pure virtual function reverts to a virtual function with respect to any further derivates. Derived classes need not fully implement the pure virtual interface inherited from their base classes, but they can inherit its overrides (because they are no longer pure virtual). However, only classes that inherit or implement the complete pure virtual interface can be instantiated in their own right. Abstract base classes are typically used to represent abstract concepts. For instance, a shape is an abstract concept whereas a circle and a square are concrete concepts that could derive from shape. We would not expect to be able to construct a shape in its own right (a shapeless shape is meaningless), but we would expect it to provide the common interface to all concrete shapes, and that interface has to be pure virtual because a shapeless shape cannot implement that interface in any meaningful way; the concrete shapes must override that interface in order to give it meaning.


What is virtual function in c plus plus?

A virtual function is a function defined in some class A that can be overridden by a class B that inherits from A. Example:#include // Base class.class A {public:// Constructor.A() {}// Destructor. When a class includes virtual functions,// always make the destructor virtual.virtual ~A() {}// A virtual function.virtual void function() {std::cout } // function()}; // class A// Derived class.class B : public A {public:// Constructor.B() {}// Destructor. Virtual is unnecessary here,// because it is implied if unspecified,// but it is good practice to include it.virtual ~B() {}// Overridden function. Again, virtual is unnecessary,// but good practice.virtual void function() {std::cout } // function()}; // class Bint main(int, char**) {// Instantiate new objects.A* a = new A();A* b = new B();// Call functions.a->function(); // Outputs "A::function()" because a is an A.b->function(); // Outputs "B::function()" because b is a B.// Dispose of objects.delete b;delete a;// Return success status.return 0;} // main()Note that, if class B does not provide an override for "function", then the statementb->function();produces the same output asa->function();which is to say that the default implementation provided by class A is used.If "function" is declared in A as:virtual void function() = 0;then "function" is a pure virtual function, and class A becomes an abstract base class. Class A can now no longer be instantiated. Derived classes must provide an implementation of "function" in order to be instantiable. A function can be pure virtual but still provide an implementation:virtual void A::function() = 0 {std::cout } // function()And, though class B must still define "function" because it is still pure virtual, it is allowed to explicitly call on the default implementation of "function" provided by A:virtual void B::function() {A::function();} // function()In this case, "B::function" outputs "A::function()", just as if "function" were declared plain virtual in class A and not overridden in class B.


Explain virtual function in c plus plus?

A virtual function is a function that is expected (but is not required) to be overridden by a derived class. By contrast, a pure-virtual function is a virtual function that must be overridden by a derived class. Virtual functions are used to enable polymorphic behaviour. That is, a virtual function is a generic method of a base class that is common to all its derivatives, and which allows base classes to be able to extort non-generic behaviour via calls to the generic interface, without ever needing to know the actual runtime type of its derivatives.


Distinguish between virtual function and pure virtual function?

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.


What is the abstract class in c?

C is not an object-oriented programming language and therefore has no concept of abstract classes. In C++, however, an abstract class is a base class that declares one or more pure-virtual functions. An abstract base class is also known as an abstract data type (ADT). Pure-virtual functions differ from virtual functions in that virtual functions are expected to be overridden (but needn't be) while a pure-virtual function must be overridden (but needn't be implemented by the ADT). Once overridden by a derived class, the function reverts to being a virtual function with respect to further derivatives. However, only classes that provide or inherit a complete implementation of the pure-virtual interface can be instantiated in their own right; those that do not are themselves abstract data types.


What is need of pure virtual functions in c plus plus?

To ensure the base class cannot be instantiated. Any class with a pure-virtual function becomes abstract; you are expectedto derive new classes from an abstract class and you mustprovide an implementation for the pure-virtual method, either in the derived class itself or in one of its derivatives. A derived class that does not implement a pure-virtual function inherited from its base class becomes abstract itself. Derived classes may also declare their own pure-virtual methods, which also renders them abstract.Abstract classes merely provide the basis upon which to create fully-implemented classes. The least-derived class is the least-specialised. As classes becomes more derived, they become more specialised. When a specialised class fully implements all the pure-virtual functions below it (either through inheritance or by providing the implementation itself), that class can finally be instantiated as an object.Note that although the class that declares a pure-virtual function can also provide an implementation for it, the derived class (or one of its derivatives) must override that implementation. Default pure-virtual implementations cannot be inherited by derived classes, but the overrides can call them, just as they can with non-pure virtual functions.


Still have questions?

Trending Questions
How old is Danielle cohn? Asked By Wiki User
Unanswered Questions
How thick is a rams skull? Asked By Wiki User
Is hugged a common noun? Asked By Wiki User
Who is juelz Santana baby mom? Asked By Wiki User