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

What is the syntex error?
A syntax error is a violation of the syntax, or grammatical rules, of a natural language or a programming language. ... A syntax error is called a fatal compilation error, because the compiler cannot translate...
Pranik
Describe about private access specifier?
Private keyword, means that no one can access the class members declared private outside that class. By default class variables and member functions are private.
Prasad
1 1
8
Is it possible to write big safe programs in C++?
yes. C++ is generally used to write system software. For example windows operating system are writeen in C++. Most of the operating system, device driver and antivirus programs are written by using C++.
Sapna
0 0
6
what is method over-riding and how it is used ? if possible send with code with output or screen photo ?
Method over-riding : it is a technique in which more then one function with same name and same signature( similar type parameter) can present in program. in overriding a subclass method overrides the definition...
Sayyad
0 0
5
Is C++ is next version of C?
This feature looks pretty sweet. This would be great because you wouldn't need getters any more. You could make member variables public; later, if you change your mind, you can turn them into property.
Subham

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

Ask a Question

Related Lessons

Introduction to Programming Languages
What is a Programming Language? A programming language is a formal computer language or constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages...

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

Programing Languages Learning Tricks
You want to learn that new language or library or framework as soon as possible, right? That’s understandable. Fortunately, there are a handful of tips that can help you to better retain all of that...

Harshal G.

0 0
0

Lets learn why OOPS is popular?
1. Supports reusability- It means that once a class is designed it works just like a template which can be inherited. 2. Provides better management- It means that abstract classes and interfaces guide...
R

C++ Program-Factorial
//Header files #include<iostream.h>#include<conio.h> //Main function void main(){ int num,i; long factorial=1; //Function for clearing screen clrscr(); cout<< "Enter a number:";...
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 >

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