answersLogoWhite

0


Best Answer

A virtual class allows children classes to inherit multiple classes that all share a common ancestor. Without this property, the child class would contain multiple copies of the same parent class. A virtual function is a function, with a body, defined in a parent class that may be overridden in the parent class.

User Avatar

Wiki User

βˆ™ 12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 15y ago

Take a look around you, find me an object which you couldn't classify. In

reality, the whole world around you is classified in some way. Life and

genes, elements and materials, businesses and products, date and time,

energy and work, etc.

The basic idea is that you can classify (no pun intended), contain, upcast,

or provide easy code-expansion for a new object based on its provided

derivative(s).

Common example

A Shape class can be an abstract class to derive a Triangle class, a

Rectangle class and a Circle class. A pure-virtual getArea() member function

in the abstract Shape class requires you to define the getArea() member

function in each of its derivatives. The Circle and the Rectangle, for

example, don't rely on the same getArea() member function to calculate its

area.

Note that this makes OO sense since:

A Triangle is_a Shape

A Rectangle is_a Shape

A Circle is_a Shape

Containers

The benefit is now you can use a container that holds any Shape (an array of

Shapes, a vector, a list, stack, queue, etc). The container needs not track

which shape element is a circle, a triangle or a rectangle. The Shape

container needs only satisfy that each element is a pointer to an existing

Shape derivative.

Shapes* shapes[4]; // a container of pointers to Shape objects

shapes[0] = new Rectangle(10, 10); // (width, length)

shapes[1] = new Circle(5); // (radius)

shapes[2] = new Triangle(7, 6) // (width, length)

shapes[3] = new Circle(22); // (radius)

The element at shapes[0] knows whether its a Circle, Rectangle or Triangle.

The same goes with every other element. The container doesn't care whether

Array[2] is a Triangle, a Rectangle or a Circle. So when you write:

int n = 0;

while ( n < 4 )

{

Array[n++]->getArea();

}

delete [] shapes;

You are calling the appropriate getArea() behaviour for each Shape-type

element in the container (using a vtable). This would not be possible

without the abstract Shape class (a container of what?). Neither would this

be possible without a pure-virtual getArea() member function in Shape (we

need to guarentee that all Shapes have such a member function available).

This ability to contain any derivative-type can be applied to Animals,

Vehicles, Employees, Airplanes, Planets or any other base class you can

think of. Note that the hierarchy need not be so shallow. A Car is a

derivative of Vehicle, a Sportscar is a derivative of Car, hence a Sportscar

can also be an element in a container of Vehicles.

Whats the benefit? All such Vehicles have an engine, a steering wheel and a

gas tank. There is no need to repeatedly insert an engine in all the derived

classes. A Vehicle base class can be composed of an engine and each derived

class needs only invoke the appropriate engine ctor to equip itself with a

particular engine. The startEngine() member function in Vehicle can now

start the engine in any Vehicle. This can save an enormous amount of code

when you consider the variety of motorized vehicle-types out there.

Should a client programmer choose to employ your classes (expand Shape to

provide a pentagon or octagon - expand Vehicle to provide a FireTruck), his

job has been made much easier for both you, the creator, and the client.

You've used a common abstract base class and pure-virtual member functions

which dictates and guarentees the implementation of the new derived entity

in the program.

A quick scan by the client of your base class permits him or her to draw up

the derived FireTruck. The client can add the water pump, the ladder, valves

and whatever he likes or needs. Yet that FireTruck can still be held in any

Vehicle container (highway, FireHouse, bridge, Ferry, Tunnel, etc) and the

original startEngine() member function still works.

The same principles of simplified code reusability can be applied in

virtually any appropriate classification hierarchy.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 10y ago

An abstract class is any base class that has at least one pure virtual function. This means the base class cannot be instantiated other than by derivation. The most-derived class must provide an implementation for the pure virtual function. The base class may provide a generic implementation, however this implementation cannot be inherited and it can only be called explicitly by a derivative. Once a derived class implements the pure virtual function, it reverts to a virtual function with regards to its derivatives and can be inherited. Only class that provide or inherit a complete implementation of all pure virtual methods can be instantiated as objects in their own right. All others are abstract.

A virtual function is a generic base class method that can be overridden by a derived class. When calling the generic method of the base class, the most-derived override is called instead. This allows programmers to work with generic objects whilst invoking specialised methods without the need to know the runtime type of an object. When you declare virtual functions, every class in the hierarchy has its own virtual table which maps the generic methods to their overrides. Although this costs a little memory, it is many times quicker than using runtime type information to determine which specialised methods to call, and reduces code maintenance substantially because the generic type does not need to know the runtime type of its derivatives. In other words, without virtual functions, every base class would need to know the exact type of all its derivatives, which means you would have to update the base class every time you derived a new object from it, and update all methods to cater for that new type, which vastly increases code size. The virtual table provides that functionality for very little cost, performs many times better, and greatly reduces the chances of programmer error.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

Neither Java nor C# provides a feature to define virtualclasses. Abstract classes may be defined in both languages.

An abstract class is a class contains (and should) at least one abstract method (and properties in c#). The idea is to establish a service contract (the name of the method, the returning type, the arguments, etc, the method signature) but provides no implementation (no method body). The implementation is deferred on purposely to be done in the derived classes. This abstract class has no knowledge what the derived classes will actually do.

Similar to abstract method, there is a virtual method in C# (in Java, every method can be a virtual method in a non-final class), it does provide one of the possibly implementation (with method body). The derived may opt to override it, inherit from that, or perform additional steps before and/or after the default implementation. A class contains at least 1 virtual method DOES NOT make that class a virtual one.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

An abstract class is an abstract data type (ADT) or, if you prefer, an abstract base class (ABC). Either way, it is a conceptual class rather than an actual class. You cannot instantiate an ADT/ABC, you can only derive from it. They exist purely to provide a common interface to the derivatives, through the use of pure-virtual methods (methods which the ADT/ABC declare but do not implement).

A virtual class is a base class that is common to two or more derived classes that may be derived through multiple inheritance. If class V is the base class for both A and B, and class D derives from both A and B, then it inherits two instance of V (one from A, the other from B). If it is known in advance that A and B can be used in multiple inheritance, then V should be declared virtual in both A and B. Thus D inherits a single instance of V, which is shared between A and B. For this to work, V should be an ADT/ABC that has a minimal interface and (preferably) no member variables.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between virtual base class and virtual function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


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

Related questions

What is the difference between abstarct class and virctulfunction in C sharp?

They are not comparable, but may have some relationship between them.An abstract class is a class, while a virtual function (or method) is a method. A method must exist within a class. Hence, a class has methods, and the methods may be defined as virtual functions.A virtual function must be defined in a class, but that class does not have to be an abstract class. However, the purpose of a virtual function in C# is to provide a default behavior/implementation, while allowing the derived class to override that default implementation, hence it makes no sense to define a virtual function in a sealed class (a leaf, that is, no class can extend from it, and it is not an abstract class)Example:public class Parent {public virtual string MostCommonPhrase() {return "You better listen to me...";}}public class Child : Parent {public override string MostCommonPhrase() {return "You never listen to me...";}}


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.


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


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 friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.


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


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


Explain virtual functions in C plus plus?

A virtual function in C++ is a function that can have multiple definitions.For example:If you have a class which contains a virtual function:class Virtual{virtual void makesomething();};That function can be implemented when you inherit that class an implement the function. So:class Inherit : public Virtual{//this is the same function, but can be implemented to do something differentvoid makesomething() { //do something else }};


What is difference between struct variable and class object?

structure variable can access the variable but class object can access the function also


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.