ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage   interview questions urls   External Links  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C++  >>  C++ General
 
 


 

 
 STL interview questions  STL Interview Questions
 OOPS interview questions  OOPS Interview Questions
 C++ General interview questions  C++ General Interview Questions
Question
What is Pure Virtual Function? Why and when it is used ?
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What is Pure Virtual Function? Why and when it is used ?
Answer
# 1
The abstract class whose pure virtual method has to be 
implemented by all the classes which derive on these. 
Otherwise it would result in a compilation error.
This construct should be used when one wants to ensure that 
all the derived classes implement the method defined as 
pure virtual in base class. 
 
Is This Answer Correct ?    2 Yes 5 No
Guest
 
  Re: What is Pure Virtual Function? Why and when it is used ?
Answer
# 2
in inheritance if the base class contains a virtual 
function equating to zero, it is known also as do-nothing  
function & that base class is called as abstract base class 
as there are no instances or objects can be created by this 
base class. And this pure virtual can be filled with the 
codes in successiv derived classes accordin to the user 
requirements.
          The syntax of the pure virtual function is....
          
      class class_name
      {
         visibility mode:\\should be protected:
         virtual return_type function_name()=0;
       }
      new class_name : inheritance_type old class_name
       {
         ........  //class body
         ........
        }
 
Is This Answer Correct ?    4 Yes 0 No
Ognas(shradha-asutosh)
 
 
 
  Re: What is Pure Virtual Function? Why and when it is used ?
Answer
# 3
Virtual function vs pure virtual function :

Virtual function :-

1. Virtual function have a function body.
2. Overloaded can be done by the virtual funciton. 
(Optional)
3. It is define as : virtual int myfunction();


Pure virtual function :-

1. Pure virtual function have no function body.
2. Overloading is must in pure virtual funciton. (Must)
3. It is define as : virtual int myfunction() = 0;
4. A class is "abstract class" when it has at least one 
pure virtual function.
5. You cann't create instance of "abstract class", rather 
you have to inherit the "abstract class" and overload all 
pure virtual function.

Like :- CControlBar class is an "abstract class".
 
Is This Answer Correct ?    8 Yes 1 No
Mahfuzur Rahman
 
  Re: What is Pure Virtual Function? Why and when it is used ?
Answer
# 4
A virtual function that is initialized to zero (0) is 
referred to as pure virtual function.It has no body and 
hence also known as do-nothing or the dummy function.
Example: virtual void show()=0;

A class containing one or more pure virtual functions is 
called an Abstract class, which means an instance of such 
class can't be created (but pointer to that class can be 
created).We should use pure virtual function if we do not 
want to instantiate a class but make it act as a base class 
for all the classes that derive from it.An important thing 
to note about pure virtual functions is that these 
functions must be overridden in all the derived classes 
otherwise the compile would flag out an error.

Sample program:

class alpha
{
public:virtual void show()=0; //pure virtual function
};
class beta:public alpha
{
public:void show()   //overriding
       {
       cout<<"OOP in C++";
       }
};
void main()
{
alpha *p;
beta b;
p=&b;
p->show();
}

Output: OOP in C++
 
Is This Answer Correct ?    3 Yes 1 No
Apple Dugar
 

 
 
 
Other C++ General Interview Questions
 
  Question Asked @ Answers
 
catch(exception &e) { . . . } Referring to the sample code above, which one of the following lines of code produces a written description of the type of exception that "e" refers to? a) cout << e.type(); b) cout << e.name(); c) cout << typeid(e).name(); d) cout << e.what(); e) cout << e; Quark2
How to implement flags? Symphony1
1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 * sizeof(int)); for (i=0; i<4; i++) *a++ = 10 * i; for (i=0; i<4; i++) { printf("%d\n", *savea); savea += sizeof(int); } return 0; } 2)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 * sizeof(int)); for (i=0; i<4; i++) *a++ = 10 * i; for (i=0; i<4; i++) { printf("%d\n", *savea); savea ++; } return 0; } The output of this two programs will be different why?  2
structure contains int, char, float how it behaves for big endian and little endian? BITS1
Why do C++ compilers need name mangling? Lucent1
what is the size of this class class size { public: char data1; double d; int data2; char data3; double data4; short data5; }; please explain the padding for these double variables.  6
Write the program for fibonacci in c++?  3
What is "map" in STL?  1
Explain the ISA and HASA class relationships. How would you implement each in a class design?  2
What and all can a compiler provides by default? HP3
What are the different operators in C++? HP1
How do you test your code? Microsoft3
Difference between Top down and bottom up approaches for a given project ? HP1
What is the Difference between "vector" and "array"? TCS6
What are the basics of classifying different storage types, why? Symphony1
Why would you make a destructor virtual? Lehman-Brothers3
How to construct virtual constructor Symphony4
What is the difference between public, private, protected inheritance? Wipro4
How do you know that your class needs a virtual destructor? Lucent3
int f() { int I = 12; int &r = I; r += r / 4; int *p = &r; *p += r; return I; } Referring to the sample code above, what is the return value of the function "f()"? a) 12 b) 15 c) 24 d) 17 e) 30 Quark2
 
For more C++ General Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com