answersLogoWhite

0


Best Answer
Answer

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

User Avatar

Wiki User

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

Wiki User

13y ago

The ability to create large programs while maximising code reuse and allowing design and reasoning about a problem to be fully contained on different levels of abstraction.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Application of object-oriented programming
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is application of c?

Programming.


What are the application domains of programming languages?

various area of application in computer application in business


What software communicates with application software and the user?

Application programming interface software


Is COBOL an application program?

No, COBOL is a programming language.


Application and uses of trees in programming?

do it urself pajious


What do you mean by ABAP?

advanced business application programming


What is the significance of programming languages to a business?

Programming languages play a vital role in business application. COBOL language, for example, is basically designed for business purpose application.


What are the different examples in computer application in science?

Programming and design are examples in computer application in science.


What is meant by sap and what is use of it?

It stands for system application and programming.


What is application of linear programming?

It is used in many optimization problems.


Where is the API?

API = Application Programming Interfacing Now, where is API of what?


What are the applications using C programming?

One application that uses c programming is Microsoft Visual Studio 2008.