What is the use of virtual keyword in inheritance?

Asked by Last Modified  

Follow 2
Answer

Please enter your answer

MS SQL SERVER DBA Trainer

use of virtual keyword in inheritance-------> Multiple inheritance in C++ is a powerful, but tricky tool, that often leads to problems if not used carefully. This article will teach you how to use virtual inheritance to solve some of these common problems programmers run into
Comments

LearnHowToLearn, Expert In Programming Languages

The 'virtual' keyword enables polymorphism such that you actually override functions. Because enheriting any function is not overriding, in C++ 'virtual' keyword is must to implement dyanamic polymorphism. Here is C++ example: struct Base { virtual void foo() {} }; struct Derived : Base { ...
read more
The 'virtual' keyword enables polymorphism such that you actually override functions. Because enheriting any function is not overriding, in C++ 'virtual' keyword is must to implement dyanamic polymorphism. Here is C++ example: struct Base { virtual void foo() {} }; struct Derived : Base { virtual void foo() {} // * second `virtual` is optional, but clearest }; Derived d; Base& b = d; b.foo(); This invokes Derived::foo, because this now overrides Base::foo — your object is polymorphic. read less
Comments

Software Developer and Trainer with 10 years of experience

Virtual keyword is used in inheritance to avoid ambiguity that may rise especially during multiple inheritance. Let me explain it with an example. Consider a class "Point" that reads and plots (x,y) co-ordinates. A new class "Line" that draws a line, inherits the class Point as it has to deal...
read more
Virtual keyword is used in inheritance to avoid ambiguity that may rise especially during multiple inheritance. Let me explain it with an example. Consider a class "Point" that reads and plots (x,y) co-ordinates. A new class "Line" that draws a line, inherits the class Point as it has to deal with starting and ending point. Similarly another class "Circle" also inherits class Point to plot the centre. Now if you want to create a class that draw a PieChart, It has to draw both lines and circle. So the easiest method is to inherit both the Line and Circle classes. On implementing this will lead to a simple problem that the class PieChart will inherit the base class Point two times (i) through Line Class (ii) through Circle class This obviously confuse the compiler that which copy it has to execute. This can be solved by adding a Virtual keyword while inheriting the first base class "Point" . This keyword prevents the creation of multiple copies for same class. So when you derive a class from two or more classes that are already subclasses of same class, ambiguity occurs due to the creation of multiple copies of base class. To avoid this "Virtual" Keyword is used. Virtual keyword is also used to create Virtual functions that enables Run-time Polymorphism. read less
Comments

Computer wizard and yoga medication

"virtual" keyword is used in case of multiple inheritance to remove ambiguity Here at level 1, when two or more class are derived from single base class by using virtual keyword,all derived class carry same features of base class which can be reused ; now when a class is derived in 2nd level , then...
read more
"virtual" keyword is used in case of multiple inheritance to remove ambiguity Here at level 1, when two or more class are derived from single base class by using virtual keyword,all derived class carry same features of base class which can be reused ; now when a class is derived in 2nd level , then in this case above derived class i.e class in 1st level act as base class and "2nd level derived class have only single base object", irrespective of being derived from multiple class And this is how ambiguities are eliminated by virtual keyword ...... but incase virtual keyword are not used in 1st level classes then 2nd level derived class will contain more number of data sets/ base object depending on number of base class it have And this creates ambiguity ...... thus virtual key word eliminates ambiguity. For example: class A is base class, class B & C are virtually derived class in 1st level at last class D is derived from two classes B & C , still class D have one single base object . this is because of virtual keyword. read less
Comments

B.E (Computer Science & Engineering) 78%; MS (Computer Science) (USA) (GPA: 3.8/4.0)

Virtual keyword can be used with classes as well as methods. When used with classes, it avoids ambiguity in inheritance. When it is used with methods, it allows method overriding and thus supports run-time polymorphism.
Comments

Virtual keyword determines if a member function of a class can be over-ridden in its derived classes.
Comments

B.Com, M.C.A, GNIIT

Virtual is used when we want that the class functionality is override in each class it used. So that if anyone dont give the functionality of virtual class in child class it used the virtual class functionality or if he give the functionality it used that functionality. Ex. Camera in Mobile. We want...
read more
Virtual is used when we want that the class functionality is override in each class it used. So that if anyone dont give the functionality of virtual class in child class it used the virtual class functionality or if he give the functionality it used that functionality. Ex. Camera in Mobile. We want that the Camera functionality is enhanced in every class. In that case we use virtual Keyword. read less
Comments

I am a java developer and i have 10year experience in training of information technology

Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. If a derived class is handled using pointer or reference...
read more
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. If a derived class is handled using pointer or reference to the base class, a call to a overridden virtual function would invoke the behavior defined in the derived class. This behavior is suppressed if the function is selected using qualified name lookup (that is, if the function's name appears to the right of the scope resolution operator ::) read less
Comments

Trainer

when you use virtual then we can use other class function to another class whether that function not belong to it.
Comments

Computer Classes

Virtual keyword is used to avoid ambiguity in inheritance. That is to resolve the ambiguity when there is/are member function with same prototype in multiple base classes.
Comments

View 44 more Answers

Related Questions

how to use dos.h
Its simple aman just go through this link with examples explains are there .. Still have any doubt u can ask here.. http://www.programmingsimplified.com/c/dos.h
Profile Photo
Aman
Is C#, C++, Python, or JS better for game development?
All languages good for gamjng. But you can make 3d games in c# uaing unity software and it's very very interesting to work on it.
Profile Photo
Mohit
0 0
View Comments6
What is iostream.h?
It is a header file, which is needed to be included in C++ program to perform input output operations.
Profile Photo
Monisha
Which language is best, C, C++, Python or Java?
If you want to learn any languages C#, Java, ect. then must to learn C, C++ language as these are base or language to be strong in language skills. C++ is improved version of C language. C#, Java, J# and etc. which are improved version of C++.
Profile Photo
Sribaghya
0 0
View Comments6
Explain about member functions?
All the data items of that class can be accessed only by the member functions of that class.
Profile Photo
Zeeshan
0 0
View Comments5

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Compare C And C++ In Terms Of Header Files.
1."stdio.h" in c program controls input-output functionalities and operations associated with it where as in c++ program iostream.h" controls input-output functionalities and operations associated with...
Profile Photo

C++ Overview
Introduction: C++ is the first object oriented programming language used universally as an advanced version of C language. Many features such as Inheritance, Class & Object, Polymorphism(Function...
N

Namrata Y.

0 0
View Comments0

Understanding Computer Science Concepts with Images and Videos..
All Computer science concepts relating to programming and software development are only virtual. It cannot be practically shown as a hardware parts of a computer. But for better understanding it should...
Profile Photo

Importance Of Function Prototype In C
Function prototype tells compiler about number of parameters function takes, data-types of parameters and return type of function. By using this information, compiler cross checks function parameters and...
Profile Photo

Do You Know Size Of Empty Class and Reason?
Size of empty class is always 1 byte. Reason is, in order to differentiate one object to another object, the size of empty class is always 1 byte. See the below c++ code snippet. Here there are three...
Profile Photo

Recommended Articles

Introduction C++ is an excellent programming language and many of the applications are written in C++ language. It has generic, object-oriented & imperative programming features, and also provides facilities for low-level memory manipulation. Successor of C language, it is an OOP (object oriented programming) language...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Looking for C++ Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you