C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 27)
27.
Which of the following cannot be used with the keyword virtual?
Discussion:
20 comments Page 1 of 2.
Rekha said:
1 decade ago
At runtime looking at the type of object referred by a pointer or reference it is decided which destructor to call -- the destructor of the base class or the destructor of the derived class.
For example, suppose B is a base class and D is a class derived from B and suppose both classes have declared their destrcutor as virtual. Suppose a pointer B *ptr is initialized as follows:
B *ptr = new D();
Now the ptr is of type B* but points to an object of D. So when this object is freed or goes out of scope D's destructor will be called since the destructors have been declared as virtual.
For example, suppose B is a base class and D is a class derived from B and suppose both classes have declared their destrcutor as virtual. Suppose a pointer B *ptr is initialized as follows:
B *ptr = new D();
Now the ptr is of type B* but points to an object of D. So when this object is freed or goes out of scope D's destructor will be called since the destructors have been declared as virtual.
Rahul said:
1 decade ago
As simple as that..we made the model (virtual ) of those things which are difficult to understand and those which can perform more than one task..
From above questions a class can have multiple variables n member functions, access specifiers..
On the other hand , MEMBER FUNCTIONS can also perform many tasks .Therefore their model should also be made..
Constructor just need to do one thing..that is INITIALIZATION by ASSIGNMENT..n that's it..not any task..
Desrtuctors also performed many tasks..therefore their virtual also exist..
From above questions a class can have multiple variables n member functions, access specifiers..
On the other hand , MEMBER FUNCTIONS can also perform many tasks .Therefore their model should also be made..
Constructor just need to do one thing..that is INITIALIZATION by ASSIGNMENT..n that's it..not any task..
Desrtuctors also performed many tasks..therefore their virtual also exist..
Gelu Menumorut said:
9 years ago
There is no such a thing as a "virtual class", but only the expression "virtual base class" that is just an euphemism that has sense only in the context of inheritance, meaning the base class involved in virtual inheritance, here the word "virtual" is actually an attribute of the inheritance not of the class. Is like "C++ slang", but altering the expression and it's context and making it official by saying it's a correct answer is weird, to say the least.
(1)
Biraj Borah said:
1 decade ago
A constructor cannot be virtual.
The reason behind this: is that a virtual keyword makes a virtual table to map function at run-time with help of Vptr (virtual pointer).
Now this virtual pointer Vptr is the first 4 bytes of the object.
So if the constructor has been called then the Vptr has to be present in the object allocation.
But wait a minute. To create an object we need constructor.
The reason behind this: is that a virtual keyword makes a virtual table to map function at run-time with help of Vptr (virtual pointer).
Now this virtual pointer Vptr is the first 4 bytes of the object.
So if the constructor has been called then the Vptr has to be present in the object allocation.
But wait a minute. To create an object we need constructor.
Arnab Pratihar said:
4 weeks ago
The "virtual" keyword is used to prevent early binding which checks which function is called at runtime, known as "late binding".
But the constructor is called at runtime, that's why no late binding problem occurs with the constructor. That is the actual reason virtual keyword is not used or can't be used before the constructor.
But the constructor is called at runtime, that's why no late binding problem occurs with the constructor. That is the actual reason virtual keyword is not used or can't be used before the constructor.
Sushil said:
1 decade ago
There is nothing like Virtual Constructor. The Constructor can't be virtual as the constructor is a code which is responsible for creating an instance of a class and it can't be delegated to any other object by virtual keyword means.
(1)
Rabz said:
5 years ago
Constructor cannot be virtual because the time when constructor is called, there is no virtual table in the memory yet. But destructor can be virtual.
(2)
Pooja Singh said:
1 decade ago
Yes, we can declare class as a virtual. It removes ambiguity of same function name used in different classes.
Example - hybrid inheritance.
Example - hybrid inheritance.
Pankaj said:
9 years ago
Constructors can be virtual and static. So right answer is destructor D option.
Prosenjit Santra said:
1 decade ago
I think we cannot create virtual constructor as well as virtual destructor.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers