Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Virtual Functions

By Sripriya R | on September 25, 2007 |
C++ Tutorials

What are Virtual Functions?

Virtual, as the name implies, is something that exists in effect but not in reality. The concept of virtual function is the same as a function, but it does not really exist although it appears in needed places in a program. The object-oriented programming language C++ implements the concept of virtual function as a simple member function, like all member functions of the class.

The functionality of virtual functions can be overridden in its derived classes. The programmer must pay attention not to confuse this concept with function overloading. Function overloading is a different concept and will be explained in later sections of this tutorial. Virtual function is a mechanism to implement the concept of polymorphism (the ability to give different meanings to one function).

Need for Virtual Function:

The vital reason for having a virtual function is to implement a different functionality in the derived class.

For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A class called FourWheeler, derived or inherited from Vehicle, may have to use a blue background and 4 tires as wheels. For this scenario, the Make function for FourWheeler should now have a different functionality from the one at the class called Vehicle. This concept is called Virtual Function.

Properties of Virtual Functions:

  • Dynamic Binding Property:

Virtual Functions are resolved during run-time or dynamic binding. Virtual functions are also simple member functions. The main difference between a non-virtual C++ member function and a virtual member function is in the way they are both resolved. A non-virtual C++ member function is resolved during compile time or static binding. Virtual Functions are resolved during run-time or dynamic binding

  • Virtual functions are member functions of a class.
  • Virtual functions are declared with the keyword virtual, detailed in an example below.
  • Virtual function takes a different functionality in the derived class.

Declaration of Virtual Function:

Virtual functions are member functions declared with the keyword virtual.

For example, the general syntax to declare a Virtual Function uses:

  1. class class_name //This denotes the base class of C++ virtual function
  2. { 
  3. public: 
  4. virtual void member_function_name() //This denotes the C++ virtual function
  5. { 
  6. ... 
  7. ... 
  8. } 
  9. };

Referring back to the Vehicle example, the declaration of Virtual function would take the shape below:

  1. class Vehicle //This denotes the base class of C++ virtual function
  2. { 
  3. public: 
  4. virtual void Make() //This denotes the C++ virtual function
  5. { 
  6. cout << "Member function of Base Class Vehicle Accessed" << endl; 
  7. } 
  8. };

After the virtual function is declared, the derived class is defined. In this derived class, the new definition of the virtual function takes place.

When the class FourWheeler is derived or inherited from Vehicle and defined by the virtual function in the class FourWheeler, it is written as:

  1.  #include <iostream>
  2. using namespace std; 
  3. class Vehicle   //This denotes the base class of C++ virtual function
  4. { 
  5. public: 
  6. 	virtual void Make()   //This denotes the C++ virtual function
  7. 	{ 
  8. 		cout << "Member function of Base Class Vehicle Accessed" << endl; 
  9. 	} 
  10. }; 
  11.  
  12. class FourWheeler : public Vehicle 
  13. { 
  14. public: 
  15. 	void Make() 
  16. 	{ 
  17. 		cout << "Virtual Member function of Derived class FourWheeler Accessed" << endl; 
  18. 	} 
  19. }; 
  20.  
  21. void main() 
  22. { 
  23. 	Vehicle *a, *b; 
  24. 	a = new Vehicle(); 
  25. 	a->Make(); 
  26. 	b = new FourWheeler(); 
  27. 	b->Make(); 
  28. }

In the above example, it is evidenced that after declaring the member functions Make() as virtual inside the base class Vehicle, class FourWheeler is derived from the base class Vehicle. In this derived class, the new implementation for virtual function Make() is placed.

Output:

The programmer might be surprised to see the function call differs and the output is then printed as above. If the member function has not been declared as virtual, the base class member function is always called because linking takes place during compile time and is therefore static.

In this example, the member function is declared virtual and the address is bounded only during run time, making it dynamic binding and thus the derived class member function is called.

To achieve the concept of dynamic binding in C++, the compiler creates a v-table each time a virtual function is declared. This v-table contains classes and pointers to the functions from each of the objects of the derived class. This is used by the compiler whenever a virtual function is needed
« « C++ Inline Functions
SOA Disadvantages » »

Author Description

Avatar

Free Training

RSSSubscribe 392 Followers
  • Popular
  • Recent
  • Elements of Object Oriented Programming

    June 10, 2006 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Type Conversions

    August 13, 2006 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment
  • Basic concepts of OOPS and Structure of C++ program

    July 26, 2006 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Class Constructors and destructors

    August 17, 2006 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment
  • C++ Variables and Data types

    April 26, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT