Quantcast
answersLogoWhite
notificationBell

Top Answer
User Avatar
Wiki User
Answered 2012-08-17 11:17:42

Virtual functions must be implemented in the base class, but they need not be implemented in their derived classes. Derived classes will automatically inherit the base class implementations of all virtual functions, but can override those functions by providing their own implementations. The base class methods can still be called explicitly to provide common functionality. A function that is declared virtual is expected to be implemented by derived classes, but it is not a requirement.

Pure-virtual functions need not be implemented in the base class, but they must be implemented in the derived classes. Any class that declares a pure-virtual function is automatically rendered an ADT (abstract data type) and cannot be instantiated other than through derivation. Derived classes do not inherit any of the abstract base class implementations, but they can call them explicitly. Derived classes that do not implement all the pure-virtual functions inherited from their abstract base classes are automatically rendered abstract themselves, however any implementations they do provide are subsequently treated as virtual methods (and are therefore inherited by derivatives). A concrete class is a class that inherits or implements the sum total of all pure-virtual methods inherited from all abstract base classes. Only concrete classes can be instantiated without being derived from.

[Edit: original answer was incomplete and misleading.]

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

Your Answer

Still Have Questions?

Related Questions

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.


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.


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


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.


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.


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;


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.


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


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.


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


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


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

A virtual function is a member method that is expected to be overridden by derived classes. A pure-virtual function is one that must be overridden in derived classes. A virtual base class is a base class that is declared virtual in its derivatives. This is commonly used when a derivative inherits from two or more base classes that each inherit from a common base class. Without virtual inheritance, the most-derived class would indirectly inherit multiple instances of the common base class. By declaring the common base class to be virtual in its derivatives, the most-derived class directly inherits the common base class, and the derivatives themselves then share that same instance.


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.



What is the difference between pure and impure function in computer language?

A pure function is one function that has no side effects or output and doesn't depend on any state beyond its local state's means it can be replaced by any other pure function which returns same result given the same inputs.This property is often referred as referential transparency


Still have questions?

Trending Questions
How old is Danielle cohn? Asked By Wiki User
Credit Repair Comapny? Asked By Wiki User
Unanswered Questions
Is E635 halal? Asked By Wiki User
Why we require Microsoft paint? Asked By Wiki User