What is virtual class in oops?

Asked by Last Modified  

Follow 0
Answer

Please enter your answer

Banker

In object-oriented programming, a virtual class is a nested inner class whose functions and member variables can be overridden and redefined by subclasses of the outer class. Virtual classes are analogous to virtual functions. Virtual classes solve the extensibility problem of extending data abstraction...
read more
In object-oriented programming, a virtual class is a nested inner class whose functions and member variables can be overridden and redefined by subclasses of the outer class. Virtual classes are analogous to virtual functions. Virtual classes solve the extensibility problem of extending data abstraction with new functions and representations. Like virtual functions, virtual classes follow the same rules of definition, overriding, and reference. When a derived class inherits from a base class, it must define or override the virtual inner classes it inherited from the base class. An object of the child class may be referred to by a reference or pointer of the parent class type or the child class type. When the reference or pointer invoke the virtual inner classes, the derived class's implementation will be called if the object is of the derived class type. The type of the outer class determines the run time of the inner virtual class. A method with an object argument has access to the object's virtual classes. The method can use the virtual classes of its arguments to create instances and declare variables. Virtual classes of different instances are not compatible. For example, a base class Machine could have a virtual class Parts. Subclass Car would implement Parts differently than the subclass Bicycle, but the programmer can call any methods in the virtual inner class Parts on any class Machine object, and get the Parts implementation of that specific derived class. class Machine { void run () {} virtual class Parts { }; }; // The inner class "Parts" of the class "Machine" may return the number of wheels the machine has. class Car: public Machine { void run() { cout << "The car is running." << endl; } class Parts { int get_Wheels () { cout << "A car has 4 wheels." << endl; return 4; } string get_Fuel_Type () { cout << "A car uses gasoline for fuel." << endl; return "gasoline"; } }; }; read less
Comments

C++ supports multiple inheritance. Suppose a class is inherited by two classes which in turn are inherited by a single class. If this last child wants to access a data member of its grandparent there would be ambiguity as there would be two copies of that grandparent in the memory. To solve this problem...
read more
C++ supports multiple inheritance. Suppose a class is inherited by two classes which in turn are inherited by a single class. If this last child wants to access a data member of its grandparent there would be ambiguity as there would be two copies of that grandparent in the memory. To solve this problem we use virtual base class concept. A virtual keyword instructs compiler to have only one instance of the class. So now there would be no ambiguity. read less
Comments

C++ supports multiple inheritance. Suppose there is a class which is inherited by two classes which inturn are inherited by a single class. Now if this last child wants to access data member of its grandparent there would be ambuigity. As there would be two copies of instances of the grandparent. To...
read more
C++ supports multiple inheritance. Suppose there is a class which is inherited by two classes which inturn are inherited by a single class. Now if this last child wants to access data member of its grandparent there would be ambuigity. As there would be two copies of instances of the grandparent. To solve this problem virtual base class concept is used. A virtual keyword instructs compiler to have only one instance of the class. There would be no ambiguity now. read less
Comments

Computer Science Faculty

virtual class is an imaginary class in oops.
Comments

A virtual class is an inner class that can be overridden by derived classes of the outer class.
Comments

Will Train the students as per their requirement.

A virtual class is the one that prevents multiple copies of a single base class
Comments

TRAINER FOR SOFTWARE COURSES & B.TECH TUTIONS

When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class
Comments

Teacher

In object-oriented programming, a virtual class is a nested inner class whose functions and member variables can be overridden and redefined by sub-classes of the outer class. Virtual classes solve the extensible problem of extending data abstraction with new functions and representations.
Comments

Virtual class is used in C++ multiple inheritance. suppose: we have a base class base1. Two classes derived1 and derived2 derive from base1. A third class derived3 derives from both derived1 and derived2. base1 has a protected variable bvar. A class derived3 method accesses this variable. Now there...
read more
Virtual class is used in C++ multiple inheritance. suppose: we have a base class base1. Two classes derived1 and derived2 derive from base1. A third class derived3 derives from both derived1 and derived2. base1 has a protected variable bvar. A class derived3 method accesses this variable. Now there are two copies of bvar available for C to use -- derived1::bvar and derived2::bvar. So the compiler gives error for ambiguity. If, however, classes derived1 and derived2 declare base1 as virtual base class this problem would be avoided. When a base class is declared as virtual only one copy of its data members exist in memory. At runtime it will be checked whether an object of the base class base1 is created in memory, if not it will be created, if yes the same copy will be referred. So there will be no ambiguity. read less
Comments

View 7 more Answers

Related Lessons


Introduction To Partnership Firms
Fundamentals of partnership: A partnership comes into existence by an agreement. This agreement can be verbal or written. When two or more individuals enter into a partnership and sign a written agreement...

Various Methods of Depreciation
Dear students,Depreciation refers to a phenomenon where the value of a fixed asset falls year on year. This change is due to obsolescence in technology, efflux of time and wear and tear of the fixed asset...

Process Costing
Introduction Process Costing is a method of Costing used in industries where the material has to pass through two or more processes for being converted into final product. It is defined as a method...

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

Recommended Articles

Swati is a renowned Hindi tutor with 7 years of experience in teaching. She conducts classes for various students ranging from class 6- class 12 and also BA students. Having pursued her education at Madras University where she did her Masters in Hindi, Swati knows her way around students. She believes that each student...

Read full article >

Urmila is a passionate teacher with over 8 years of experience in teaching. She is currently pursuing her Ph. D. She provides classes for Class 11, Class 12, MBBS and Medical tuition.  Urmila began her career in teaching long before she became a teacher. She used to provide classes for foreign national students in her college...

Read full article >

Radhe Shyam is a highly skilled accounts and finance trainer with 8 years of experience in teaching. Accounting is challenging for many students and that’s where Radhe Shyam’s expertise comes into play. He helps his students not only in understanding the subject but also advises them on how to overcome the fear of accounts...

Read full article >

Sandhya is a proactive educationalist. She conducts classes for CBSE, PUC, ICSE, I.B. and IGCSE. Having a 6-year experience in teaching, she connects with her students and provides tutoring as per their understanding. She mentors her students personally and strives them to achieve their goals with ease. Being an enthusiastic...

Read full article >

Looking for Class 12 Tuition ?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you