UrbanPro

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

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

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

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

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

Trainer

when you use virtual then we can use other class function to another class whether that function not belong to it.
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

Experienced IT Trainer/Coach, Founder Win Corporate Training, Counsellor

It is used to implement Runtime polymorphism in C++ and C#. the virtual method will be defined in the base class and will be redifined in the subclass.
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

Is C/C++ language is still important in 2019?
Of course, C language is only the heart of any processor or controller.Because of complexity in C peoples are looking to work on High-level languages like JAVA etc.
Tejas
0 0
7
What is called 'nested loops' in C++?
These are those loops in which one loop executes inside another loop. For example for loop for(int i=1,i=10,i++) { for(int j=1, j<=i,j++) { } } the second loop is the nested for loop.
Harikrishnan

What is the main difference between returning value of a function and does not return any value of a function in  C. Which is correct void main(void), int main(void), int main(int) , or void main(int) and explain the main difference between int main() and int main(void). 

A function may or may not return a value to the calling function. If the function is returning a value, then you have to mention the data type in the function definition and funtion declaration (like int,...
Mahesh Kondawar
What is the best IDE for C and C++ development?
If you're a student then Turbo C++ is best for C and C++ program + inbuilt C/C++ Graphics Function If you're a professional or working then Netbeans for C/C++ or Code:: Blocks is Best.
Balakrishnasingh

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

Ask a Question

Related Lessons

C++ Program Sample Application
//Standard Library Functions(Headers Files Used in C++) #include<iostream.h> #include<conio.h> //Main function int main() { //function for clearing the screen clrscr(); //function for...
S

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....

Necessity of Theory and Practical in Computer Science.
Upon studying a subject both theory and practical are important. Usually many schools concentrate more on theory and the marks not on the practical. Other and opposite kind of people prefer practical...

Turbo C++ Keyboard Shortcuts
S.No. Shortcuts keys Action 1. F1 For Help 2. F2 Save 3. F3 Open 4. F4 Go to cursor 5. F5 Zoom 6. F6 Next 7. F7 Trace...

Working with C/C++ applications
Inorder to learn C and C++ programming languages one can work with various editors available.To name a few are the most popular one is turbo c++, DEV C++, Eclipse, NetBeans. Here are the screen shots...
S

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
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more