Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C++ Interview Questions and Answers Part 4

    • 0
    • 3
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 175
    Comment on it

    In the previous parts C++ Interview Questions and Answers Part 1, Part2 and Part 3, I have explained some basic questions of C++. This part is in continuation with the previous C++ Interview Questions and Answers tutorial. In this part of the tutorial, we are proceeding with more questions based on further concepts of C++ and trying to guide you with the best possible explanations.

    1. What are virtual functions and what is its use?

      [This question is frequently asked in every OOP language interviews. Virtual functions are used to implement run time polymorphism in c++.]

      Virtual functions are those member functions of class which are declared using keyword 'virtual'. A Parent class reference is initialized using object of derived class and the virtual function that must be overrided in the derived class is invoked using the parent class reference, the method in derived class object will get invoked.

      class Parent
      {    
          int a;     
          public:        Parent()       
           {            a = 1;        
          }        
          virtual void method()        
          {            cout << a;        
          }
      }; 
      class Derived: public Parent
      {  
            int b;    
          public:         Derived()       
               {           
                  b = 2;      
               }       
               virtual void method()        
              {             cout << b;      
               }
      };
      int main()
      { 
          Parent *pBase;     
          Derived oChild;    
          pBase = &oChild;    
          pBase->method();   
          return 0;
      }
      

      In the above example, even though the method in invoked on parent class reference, method of the derived will get invoked since its declared as virtual.

    1. What do you mean by pure virtual functions in C++? Give an example?

      Pure virtual function is a function which doesn't have an implementation, for which we dont have implementation, we only declare it and that function needs to be implemented by the the next immediate non-abstract class. This function is also known as abstract function.(A class will become an abstract class if there is any single pure virtual function and pure virtual functions are used to create interfaces in c++).

      • How to create a pure virtual function?

      A function is made as pure virtual function by using keyword virtual before function name and assigning 0 in its declaration.

          class Shape
           {    
              public:              virtual void draw() = 0;
          };
      

    1. Why pure virtual functions are used if they don't have implementation / When does a pure virtual function become useful?

    2. Pure virtual functions are used when it doesn't make sense to provide definition of a virtual function in the parent class or a proper definition does not exists in the context of parent class. Consider the above example, class Shape is used as parent class for shapes with symmetric structure (circle, square, equilateral triangle, etc). In this case, there exists no proper definition for function draw()in the parent class Shape instead the derived classes of Shape (circle, square, etc) can implement this draw() method and draw corresponding shapes.

    1. What is virtual destructor? Why they are used?

    2. Destructor in the Base class can be Virtual. When an object of derived class has to be removed, which is referenced by a parent class pointer, in this case only destructor of parent class will get executed. If the destructor is preceded by virtual keyword, both the destructor ( of parent and derived class ) will get invoked.

    1. What you mean by early binding and late binding? How it is related to dynamic binding?

    2. Early binding (also called static binding) means the compiler is able to directly associate the identifier name (such as a function or variable name) with a machine address.

      In some programs, it's not possible to know which function will be called until runtime. This is known as late binding (or dynamic binding). In C++, one way to get late binding is to use function pointers or the other way is the use of virtual functions in inheritance. e.g., using function pointer.

    1. What is meant by reference variable in C++?

    2. In C++, A reference variable is an alias, that is, another name for an already existing variable. A reference variable can be used to access the original data. We can access the variable either with the variable name or the reference name, once a reference is initialized with a variable.

             int x = 5;
                  int& y = x;
      

      The first statement initializes an integer variable x. Second statement creates **an integer reference initialized to variable x** Below is an example to see how reference variables work.

       int main ()
          {   
               int   x; 
      int& y = x; 
      x = 10;
      cout << "Value of x : " << a << endl;
      cout << "Value of x reference (y) : " << y << endl; 
      y = 20;
      cout << "Value of x : " << x << endl;
      cout << "Value of x reference (y) : " << y << endl; 
      return 0;
      }

      Above code creates following output.

      Value of x : 10
      Value of x reference (y) : 10
      Value of x : 20
      Value of x reference (y) : 20
    1. What are the differences between reference variables and pointers in C++?

    2. Pointers Reference variables
      Pointers can be assigned to NULL. References cannot be assigned NULL.
      Pointers can be pointed to another object at any time. Once a reference is initialized to an object, it cannot be changed to refer to another object. 
      Pointers can be initialized at any time. A reference must be initialized when it is created.
      Pointer has own memory address and location on stack Reference variables has location on stack, but shares the same memory location with the object it refer to.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: