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

Founder Win Corporate Training IT Corporate Trainer/Coach, 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 actual size of INT in the C language, 2 or 4 bytes?
The actual size to int is determined by the compiler as the program runs. But theoretically the size is 2 bytes. You can increase the size by adding keyword long infront of it to make the size 4 bytes. Eg int a; // 2 bytes Long int b; // 4 bytes
Kunal
i am a student of 11th class and i want to make my future in C++ is this possible i am take arts inn 11th class
Definitely.. but you should be flexible moving to java/ other languages as well.. requirement/ demand keep changing in software. i can do the needful.. with 18+ yrs exp in working on live projects...
Rajneesh
Is there any person who can teach me C++ without Money Fees?
Good question :) Free training is available on various sites online. But, if you would want to learn from an expert directly, there's a very slim chance that you might find some one who will teach for...
Kishan
What are the toughest topics in C language?
1. pointer 2. dynamic memory allocation 3. File handling
Lokayya
0 0
5
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.
Mohit
0 0
6

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

Ask a Question

Related Lessons

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Polymorphism In C++
Basically polymorphism represents poly means many and morph means forms that many forms. In which we are passing same message to different objets but every object will work for that message in their own...

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...

C++ Program-Working with constant using const keyword
//Header files #include<iostream.h>#include<conio.h> //Main function void main(){ //using const keyword to declare constants with a specific type const int len=10; const int br=5; const...

Memory Layout of C Programs
A typical memory representation of C program consists of following sections. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object...

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