Google News
CPP Interview Questions
In C++ there are the following access specifiers :
* Public : All data members and member functions are accessible outside the class.
* Protected : All data members and member functions are accessible inside the class and to the derived class.
* Private : All data members and member functions are not accessible outside the class.
If there are two or more functions with the same name defined in different libraries then how will the compiler know which one to refer to? Thus namespace came to picture. A namespace defines a scope and differentiates functions, classes, variables etc. with the same name available in different libraries. The namespace starts with the keyword “namespace”.

The syntax for the same is as follows :
namespace namespace_name {
   // code declarations
Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
A destructor is a member function with the same name as its class prefixed by a ~ (tilde).

For example :
class X{
  // Constructor for class X
  // Destructor for class X
Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is different from the function myfuncn(float a, int b) parameter list (float, int). Function overloading is a compile-time polymorphism.
Now that we know what is parameter list lets see the rules of overloading: we can have following functions in the same scope.
sum(int num1, int num2)
sum(int num1, int num2, int num3)
sum(int num1, double num2)

Function overloading example program : 

#include <iostream>
using namespace std;
class Addition {
    int sum(int num1,int num2) {
        return num1+num2;
    int sum(int num1,int num2, int num3) {
       return num1+num2+num3;
int main(void) {
    Addition obj;
    cout<<obj.sum(27, 18)<<endl;
    cout<<obj.sum(81, 108, 9);
    return 0;

Output : 

A string is a sequence of characters. In C++, string is a data type as well as a header file. This header file consists of powerful functions of string manipulation. A variable of string is declared as follows :
string str= "Hello"; 
And to use string one needs to include the header file.
// Include the string library
#include <string>
// Create a string variable
string str= "Hello";​
In call by value method, we pass a copy of the parameter is passed to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the main function.
In call by reference method, we pass the address of the variable and the address is used to access the actual argument used in the function call. So changes made in the parameter alter the passing argument.
A Copy constructor is an overloaded constructor used to declare and initialize an object from another object.
Copy Constructor is of two types :

Default Copy constructor : The compiler defines the default copy constructor. If the user defines no copy constructor, compiler supplies its constructor.
User Defined constructor : The programmer defines the user-defined constructor.
Syntax Of User-defined Copy Constructor :
Class_name(const class_name &old_object);  

Consider the following situation :
class A  
    A(A &x) //  copy constructor.  
       // copyconstructor.  
There are two important distinctions between a class and a structure in C++. These are :
* When deriving a structure from a class or some other structure, the default access specifier for the base class or structure is public. On the contrary, default access specifier is private when deriving a class.

* While the members of a structure are public by default, the members of a class are private by default
Denoted by the static keyword, a static member is allocated storage, in the static storage area, only once during the program lifetime. Some important facts pertaining to the static members are :
* Any static member function can’t be virtual
* Static member functions don’t have ‘this’ pointer
* The const, const volatile, and volatile declaration aren’t available for static member functions
The reference variable in C++ is the name given to the existing variables. The variable name and reference variable point share the same memory location in C++, which helps in updating the original variable using the reference variable. The code can be displayed in the following example.
using namespace std;
int main()
  int x = 10;
  // ref is a reference to x.
  int& ref = x;
  // Value of x is now changed to 20
  ref = 20;
  cout << "x = " << x << endl ;
  // Value of x is now changed to 30
  x = 30;
  cout << "ref = " << ref << endl ;
  return 0;

Output :
x = 20
ref = 30