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

how to use dos.h
First include dos.h header file then use their function. Before use function, you must have knowledge of dos functions and their charactistics
Aman
What is iostream.h?
It is a header file, which is needed to be included in C++ program to perform input output operations.
Monisha
Explain about protected internal access specifier?
protected access specifier is used in inheritance. Base class members are made accessible to its sub classes through protected access specifier. This access specifier works with in inheritance.
Sahiba
0 0
7
What is meaning of C++?
C++ is an advancement of C language.It is the that language which consists the concepts of OOP or lets say OBJECT ORIENTED PROGRAMMING( OOP ).The benefits of C language as it more secure in programming,...
Moinuddin
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

Callback using Function Pointer (in C), interface (in Java) and EventListener pattern
Note : Read the original document at http://som-itsolutions.blogspot.in/2017/01/blog-post_24.html “In computer programming, a callback is a reference to executable code, or a piece of executable...

File Handling in C++ : Lesson 1 Data Persistence
Data persistence refers to the existence of data in the program. In other words, it means the life of data. Data is the most significant part of a program and it must be stored properly and easily retrieved...
J

C, C++, JAVA Tutor
* Program to swap two numbers using a temporary variable with each statement explained with comments * #include // headerfile for cout statement using namespace std; //...

C Programming Language Basics - Easy way to learn
Computer - OverviewTodays world is an information-rich world and it has become a necessity for everyone to know about computers. A computer is an electronic data processing device, which accepts and stores...

Polymorphism and Virtual Functions
Polymorphism and Virtual Functions Compile Time Polymorphism or Early Binding or Static Binding An object is bound to its function call at compile time. Implemented using overloaded functions and...

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 >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

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 >

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