C++ Language Features Interview Questions

You have a class Parent with a virtual method a and a class Child that implements a. You also have a several functions:

void f1(Parent p) { p.a(); }
void f2(Parent* p) { p->a(); }
void f3(Parent& p) { p.a(); }

When called with a Child instance, which method will be invoked for f1, f2 and f3?

Explain in detail what the following C++ code does:

#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Define the following object oriented concepts:

  • Class, object (and the difference between the two)
  • Instantiation
  • Method (as opposed to, say, a C function)
  • Static methods and classes
  • Destructor/finalizer
  • Inheritance
  • Encapsulation
  • Multiple inheritance (and give an example)
  • Abstract class
  • Interface/protocol (and different from abstract class)
  • Method overriding
  • Method overloading (and difference from overriding)
  • Polymorphism (without resorting to examples)
  • Method visibility (e.g. public/private/other)