Author: saqibkhan

  • Preprocessor

    The preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts.

    All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;).

    You already have seen a #include directive in all the examples. This macro is used to include a header file into the source file.

    There are number of preprocessor directives supported by C++ like #include, #define, #if, #else, #line, etc. Let us see important directives −

    The #define Preprocessor

    The #define preprocessor directive creates symbolic constants. The symbolic constant is called a macro and the general form of the directive is −

    #define macro-name replacement-text 
    

    When this line appears in a file, all subsequent occurrences of macro in that file will be replaced by replacement-text before the program is compiled. For example −

    #include <iostream>
    using namespace std;
    
    #define PI 3.14159
    
    int main () {
       cout << "Value of PI :" << PI << endl; 
    
       return 0;
    }

    Now, let us do the preprocessing of this code to see the result assuming we have the source code file. So let us compile it with -E option and redirect the result to test.p. Now, if you check test.p, it will have lots of information and at the bottom, you will find the value replaced as follows −

    $gcc -E test.cpp > test.p
    
    ...
    int main () {
       cout << "Value of PI :" << 3.14159 << endl; 
       return 0;
    }
    

    Function-Like Macros

    You can use #define to define a macro which will take argument as follows −

    #include <iostream>
    using namespace std;
    
    #define MIN(a,b) (((a)<(b)) ? a : b)
    
    int main () {
       int i, j;
       
       i = 100;
       j = 30;
       
       cout <<"The minimum is " << MIN(i, j) << endl;
    
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    The minimum is 30
    

    Conditional Compilation

    There are several directives, which can be used to compile selective portions of your program’s source code. This process is called conditional compilation.

    The conditional preprocessor construct is much like the ‘if’ selection structure. Consider the following preprocessor code −

    #ifndef NULL
       #define NULL 0
    #endif
    

    You can compile a program for debugging purpose. You can also turn on or off the debugging using a single macro as follows −

    #ifdef DEBUG
       cerr <<"Variable x = " << x << endl;
    #endif
    

    This causes the cerr statement to be compiled in the program if the symbolic constant DEBUG has been defined before directive #ifdef DEBUG. You can use #if 0 statment to comment out a portion of the program as follows −

    #if 0
       code prevented from compiling
    #endif
    

    Let us try the following example −

    #include <iostream>
    using namespace std;
    #define DEBUG
    
    #define MIN(a,b) (((a)<(b)) ? a : b)
    
    int main () {
       int i, j;
       
       i = 100;
       j = 30;
    
    #ifdef DEBUG
       cerr <<"Trace: Inside main function" << endl;
    #endif
    
    #if 0
       /* This is commented part */
       cout << MKSTR(HELLO C++) << endl;
    #endif
    
       cout <<"The minimum is " << MIN(i, j) << endl;
    
    #ifdef DEBUG
       cerr <<"Trace: Coming out of main function" << endl;
    #endif
    
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    The minimum is 30
    Trace: Inside main function
    Trace: Coming out of main function
    

    The # and ## Operators

    The # and ## preprocessor operators are available in C++ and ANSI/ISO C. The # operator causes a replacement-text token to be converted to a string surrounded by quotes.

    Consider the following macro definition −

    #include <iostream>
    using namespace std;
    
    #define MKSTR( x ) #x
    
    int main () {
    
       cout << MKSTR(HELLO C++) << endl;
    
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    HELLO C++
    

    Let us see how it worked. It is simple to understand that the C++ preprocessor turns the line −

    cout << MKSTR(HELLO C++) << endl;
    

    Above line will be turned into the following line −

    cout << "HELLO C++" << endl;
    

    The ## operator is used to concatenate two tokens. Here is an example −

    #define CONCAT( x, y )  x ## y
    

    When CONCAT appears in the program, its arguments are concatenated and used to replace the macro. For example, CONCAT(HELLO, C++) is replaced by “HELLO C++” in the program as follows.

    #include <iostream>
    using namespace std;
    
    #define concat(a, b) a ## b
    int main() {
       int xy = 100;
       
       cout << concat(x, y);
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    100
    

    Let us see how it worked. It is simple to understand that the C++ preprocessor transforms −

    cout << concat(x, y);
    

    Above line will be transformed into the following line −

    cout << xy;
    

    Predefined C++ Macros

    C++ provides a number of predefined macros mentioned below −

    Sr.NoMacro & Description
    1__LINE__This contains the current line number of the program when it is being compiled.
    2__FILE__This contains the current file name of the program when it is being compiled.
    3__DATE__This contains a string of the form month/day/year that is the date of the translation of the source file into object code.
    4__TIME__This contains a string of the form hour:minute:second that is the time at which the program was compiled.

    Let us see an example for all the above macros −

    #include <iostream>
    using namespace std;
    
    int main () {
       cout << "Value of __LINE__ : " << __LINE__ << endl;
       cout << "Value of __FILE__ : " << __FILE__ << endl;
       cout << "Value of __DATE__ : " << __DATE__ << endl;
       cout << "Value of __TIME__ : " << __TIME__ << endl;
    
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    Value of __LINE__ : 6
    Value of __FILE__ : test.cpp
    Value of __DATE__ : Feb 28 2011
    Value of __TIME__ : 18:52:48
    
  •  Write a Program to Check Palindrome

    C++// C++ program to check if a // number is Palindrome or not #include <iostream> using namespace std; // Function to check Palindrome bool checkPalindrome(int n) { int ans = 0; int temp = n; while (temp != 0) { ans = (ans * 10) + (temp % 10); temp = temp / 10; } return (ans == n); } int main() { int n = 12321; if (checkPalindrome(n) == 1) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }

    Output

    Yes
  • Templates

    Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

    A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

    There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>.

    You can use templates to define functions as well as classes, let us see how they work −

    Function Template

    The general form of a template function definition is shown here −

    template <class type> ret-type func-name(parameter list) {
       // body of function
    } 
    

    Here, type is a placeholder name for a data type used by the function. This name can be used within the function definition.

    The following is the example of a function template that returns the maximum of two values −

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template <typename T>
    inline T const& Max (T const& a, T const& b) { 
       return a < b ? b:a; 
    }
    
    int main () {
       int i = 39;
       int j = 20;
       cout << "Max(i, j): " << Max(i, j) << endl; 
    
       double f1 = 13.5; 
       double f2 = 20.7; 
       cout << "Max(f1, f2): " << Max(f1, f2) << endl; 
    
       string s1 = "Hello"; 
       string s2 = "World"; 
       cout << "Max(s1, s2): " << Max(s1, s2) << endl; 
    
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    Max(i, j): 39
    Max(f1, f2): 20.7
    Max(s1, s2): World
    

    Class Template

    Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here −

    template <class type> class class-name {
       .
       .
       .
    }
    

    Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more than one generic data type by using a comma-separated list.

    Following is the example to define class Stack<> and implement generic methods to push and pop the elements from the stack −

    #include <iostream>
    #include <vector>
    #include <cstdlib>
    #include <string>
    #include <stdexcept>
    
    using namespace std;
    
    template <class T>
    class Stack { 
       private: 
    
      vector&lt;T&gt; elems;    // elements 
    public:
      void push(T const&amp;);  // push element 
      void pop();               // pop element 
      T top() const;            // return top element 
      
      bool empty() const {      // return true if empty.
         return elems.empty(); 
      } 
    }; template <class T> void Stack<T>::push (T const& elem) { // append copy of passed element elems.push_back(elem); } template <class T> void Stack<T>::pop () { if (elems.empty()) {
      throw out_of_range("Stack&lt;&gt;::pop(): empty stack"); 
    } // remove last element elems.pop_back(); } template <class T> T Stack<T>::top () const { if (elems.empty()) {
      throw out_of_range("Stack&lt;&gt;::top(): empty stack"); 
    } // return copy of last element return elems.back(); } int main() { try {
      Stack&lt;int&gt;         intStack;  // stack of ints 
      Stack&lt;string&gt; stringStack;    // stack of strings 
      // manipulate int stack 
      intStack.push(7); 
      cout &lt;&lt; intStack.top() &lt;&lt;endl; 
      // manipulate string stack 
      stringStack.push("hello"); 
      cout &lt;&lt; stringStack.top() &lt;&lt; std::endl; 
      stringStack.pop(); 
      stringStack.pop(); 
    } catch (exception const& ex) {
      cerr &lt;&lt; "Exception: " &lt;&lt; ex.what() &lt;&lt;endl; 
      return -1;
    } }

    If we compile and run above code, this would produce the following result −

    7
    hello
    Exception: Stack<>::pop(): empty stack
    
  • Namespaces in C++

    Consider a situation, when we have two persons with the same name, Zara, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother’s or father’s name, etc.

    Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

    namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.

    Defining a Namespace

    A namespace definition begins with the keyword namespace followed by the namespace name as follows −

    namespace namespace_name {
       // code declarations
    }
    

    To call the namespace-enabled version of either function or variable, prepend (::) the namespace name as follows −

    name::code;  // code could be variable or function.
    

    Let us see how namespace scope the entities including variable and functions −

    #include <iostream>
    using namespace std;
    
    // first name space
    namespace first_space {
       void func() {
    
      cout &lt;&lt; "Inside first_space" &lt;&lt; endl;
    } } // second name space namespace second_space { void func() {
      cout &lt;&lt; "Inside second_space" &lt;&lt; endl;
    } } int main () { // Calls function from first name space. first_space::func(); // Calls function from second name space. second_space::func(); return 0; }

    If we compile and run above code, this would produce the following result −

    Inside first_space
    Inside second_space
    

    The using directive

    You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. The namespace is thus implied for the following code −

    #include <iostream>
    using namespace std;
    
    // first name space
    namespace first_space {
       void func() {
    
      cout &lt;&lt; "Inside first_space" &lt;&lt; endl;
    } } // second name space namespace second_space { void func() {
      cout &lt;&lt; "Inside second_space" &lt;&lt; endl;
    } } using namespace first_space; int main () { // This calls function from first name space. func(); return 0; }

    If we compile and run above code, this would produce the following result −

    Inside first_space
    

    The ‘using’ directive can also be used to refer to a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows −

    using std::cout;
    

    Subsequent code can refer to cout without prepending the namespace, but other items in the std namespace will still need to be explicit as follows −

    #include <iostream>
    using std::cout;
    
    int main () {
       cout << "std::endl is used with std!" << std::endl;
       
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    std::endl is used with std!
    

    Names introduced in a using directive obey normal scope rules. The name is visible from the point of the using directive to the end of the scope in which the directive is found. Entities with the same name defined in an outer scope are hidden.

    Discontiguous Namespaces

    A namespace can be defined in several parts and so a namespace is made up of the sum of its separately defined parts. The separate parts of a namespace can be spread over multiple files.

    So, if one part of the namespace requires a name defined in another file, that name must still be declared. Writing a following namespace definition either defines a new namespace or adds new elements to an existing one −

    namespace namespace_name {
       // code declarations
    }
    

    Nested Namespaces

    Namespaces can be nested where you can define one namespace inside another name space as follows −

    namespace namespace_name1 {
       // code declarations
       namespace namespace_name2 {
    
      // code declarations
    } }

    You can access members of nested namespace by using resolution operators as follows −

    // to access members of namespace_name2
    using namespace namespace_name1::namespace_name2;
    
    // to access members of namespace:name1
    using namespace namespace_name1;
    

    In the above statements if you are using namespace_name1, then it will make elements of namespace_name2 available in the scope as follows −

    #include <iostream>
    using namespace std;
    
    // first name space
    namespace first_space {
       void func() {
    
      cout &lt;&lt; "Inside first_space" &lt;&lt; endl;
    } // second name space namespace second_space {
      void func() {
         cout &lt;&lt; "Inside second_space" &lt;&lt; endl;
      }
    } } using namespace first_space::second_space; int main () { // This calls function from second name space. func(); return 0; }

    If we compile and run above code, this would produce the following result −

    Inside second_space
    
  • Write a Program to Check the Prime Number

    C++// C++ program to check if a // Number is prime #include <iostream> using namespace std; bool isPrime(int n) { // base condition if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int main() { isPrime(21) ? cout << " true\n" : cout << " false\n"; isPrime(17) ? cout << " true\n" : cout << " false\n"; return 0; }

    Output

     false
     true
  • Write a Program to Find a Leap Year or Not

    C++// C++ program to check if a given // year is leap year or not #include <iostream> using namespace std; bool checkYear(int year) { // leap year if (year % 400 == 0) return true; // Not leap year if (year % 100 == 0) return false; // leap year if (year % 4 == 0) return true; // Not leap year return false; } int main() { int year = 2000; if (checkYear(year)) cout << "Leap Year"; else cout << "Not a Leap Year"; return 0; }

    Output

    Leap Year
  • Dynamic Memory

    A good understanding of how dynamic memory really works in C++ is essential to becoming a good C++ programmer. Memory in your C++ program is divided into two parts −

    • The stack − All variables declared inside the function will take up memory from the stack.
    • The heap − This is unused memory of the program and can be used to allocate the memory dynamically when program runs.

    Many times, you are not aware in advance how much memory you will need to store particular information in a defined variable and the size of required memory can be determined at run time.

    You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator.

    If you are not in need of dynamically allocated memory anymore, you can use delete operator, which de-allocates memory that was previously allocated by new operator.

    new and delete Operators

    There is following generic syntax to use new operator to allocate memory dynamically for any data-type.

    new data-type;
    

    Here, data-type could be any built-in data type including an array or any user defined data types include class or structure. Let us start with built-in data types. For example we can define a pointer to type double and then request that the memory be allocated at execution time. We can do this using the new operator with the following statements −

    double* pvalue  = NULL; // Pointer initialized with null
    pvalue  = new double;   // Request memory for the variable
    

    The memory may not have been allocated successfully, if the free store had been used up. So it is good practice to check if new operator is returning NULL pointer and take appropriate action as below −

    double* pvalue  = NULL;
    if( !(pvalue  = new double )) {
       cout << "Error: out of memory." <<endl;
       exit(1);
    }
    

    The malloc() function from C, still exists in C++, but it is recommended to avoid using malloc() function. The main advantage of new over malloc() is that new doesn’t just allocate memory, it constructs objects which is prime purpose of C++.

    At any point, when you feel a variable that has been dynamically allocated is not anymore required, you can free up the memory that it occupies in the free store with the ‘delete’ operator as follows −

    delete pvalue;        // Release memory pointed to by pvalue
    

    Let us put above concepts and form the following example to show how ‘new’ and ‘delete’ work −

    #include <iostream>
    using namespace std;
    
    int main () {
       double* pvalue  = NULL; // Pointer initialized with null
       pvalue  = new double;   // Request memory for the variable
     
       *pvalue = 29494.99;     // Store value at allocated address
       cout << "Value of pvalue : " << *pvalue << endl;
    
       delete pvalue;         // free up the memory.
    
       return 0;
    }

    If we compile and run above code, this would produce the following result −

    Value of pvalue : 29495
    

    Dynamic Memory Allocation for Arrays

    Consider you want to allocate memory for an array of characters, i.e., string of 20 characters. Using the same syntax what we have used above we can allocate memory dynamically as shown below.

    char* pvalue  = NULL;         // Pointer initialized with null
    pvalue  = new char[20];       // Request memory for the variable
    

    To remove the array that we have just created the statement would look like this −

    delete [] pvalue;             // Delete array pointed to by pvalue
    

    Following the similar generic syntax of new operator, you can allocate for a multi-dimensional array as follows −

    double** pvalue  = NULL;      // Pointer initialized with null 
    pvalue  = new double [3][4];  // Allocate memory for a 3x4 array 
    

    However, the syntax to release the memory for multi-dimensional array will still remain same as above −

    delete [] pvalue;            // Delete array pointed to by pvalue
    

    Dynamic Memory Allocation for Objects

    Objects are no different from simple data types. For example, consider the following code where we are going to use an array of objects to clarify the concept −

    #include <iostream>
    using namespace std;
    
    class Box {
       public:
    
      Box() { 
         cout &lt;&lt; "Constructor called!" &lt;&lt;endl; 
      }
      ~Box() { 
         cout &lt;&lt; "Destructor called!" &lt;&lt;endl; 
      }
    }; int main() { Box* myBoxArray = new Box[4]; delete [] myBoxArray; // Delete array return 0; }

    If you were to allocate an array of four Box objects, the Simple constructor would be called four times and similarly while deleting these objects, destructor will also be called same number of times.

    If we compile and run above code, this would produce the following result −

    Constructor called!
    Constructor called!
    Constructor called!
    Constructor called!
    Destructor called!
    Destructor called!
    Destructor called!
    Destructor called!
    
  • Write a Program to Find the Factorial of a Number Using Loops

    C++// C++ program to find factorial using loops #include <bits/stdc++.h> using namespace std; // function to find factorial int factorial(int n) { int fact = 1; while (n > 1) { fact *= n; n--; } return fact; } // driver code int main() { int num = 5; cout << factorial(num); return 0; }

    Output

    120
  • Exception Handling

    An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

    Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

    • throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
    • catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
    • try − A try block identifies a block of code for which particular exceptions will be activated. It’s followed by one or more catch blocks.

    Assuming a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch as follows −

    try{// protected code}catch( ExceptionName e1 ){// catch block}catch( ExceptionName e2 ){// catch block}catch( ExceptionName eN ){// catch block}

    You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations.

    Throwing Exceptions

    Exceptions can be thrown anywhere within a code block using throw statement. The operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.

    Example

    Following is an example of throwing an exception when dividing by zero condition occurs −

    doubledivision(int a,int b){if( b ==0){throw"Division by zero condition!";}return(a/b);}

    Catching Exceptions

    The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch.

    try{// protected code}catch( ExceptionName e ){// code to handle ExceptionName exception}

    Above code will catch an exception of ExceptionName type. If you want to specify that a catch block should handle any type of exception that is thrown in a try block, you must put an ellipsis, …, between the parentheses enclosing the exception declaration as follows −

    try{// protected code}catch(...){// code to handle any exception}

    Example

    The following is an example, which throws a division by zero exception and we catch it in catch block.

    Open Compiler

    #include <iostream>usingnamespace std;doubledivision(int a,int b){if( b ==0){throw"Division by zero condition!";}return(a/b);}intmain(){int x =50;int y =0;double z =0;try{
    
      z =division(x, y);
      cout &lt;&lt; z &lt;&lt; endl;}catch(constchar* msg){
     cerr &lt;&lt; msg &lt;&lt; endl;}return0;}</code></pre>

    Because we are raising an exception of type const char*, so while catching this exception, we have to use const char* in catch block. If we compile and run above code, this would produce the following result −

    Division by zero condition!


    C++ Standard Exceptions

    C++ provides a list of standard exceptions defined in <exception> which we can use in our programs. These are arranged in a parent-child class hierarchy shown below −

    C++ Exceptions Hierarchy

    Here is the small description of each exception mentioned in the above hierarchy −

    Sr.NoException & Description
    1std::exceptionAn exception and parent class of all the standard C++ exceptions.
    2std::bad_allocThis can be thrown by new.
    3std::bad_castThis can be thrown by dynamic_cast.
    4std::bad_exceptionThis is useful device to handle unexpected exceptions in a C++ program.
    5std::bad_typeidThis can be thrown by typeid.
    6std::logic_errorAn exception that theoretically can be detected by reading the code.
    7std::domain_errorThis is an exception thrown when a mathematically invalid domain is used.
    8std::invalid_argumentThis is thrown due to invalid arguments.
    9std::length_errorThis is thrown when a too big std::string is created.
    10std::out_of_rangeThis can be thrown by the 'at' method, for example a std::vector and std::bitset<>::operator[]().
    11std::runtime_errorAn exception that theoretically cannot be detected by reading the code.
    12std::overflow_errorThis is thrown if a mathematical overflow occurs.
    13std::range_errorThis is occurred when you try to store a value which is out of range.
    14std::underflow_errorThis is thrown if a mathematical underflow occurs.

    Define New Exceptions

    You can define your own exceptions by inheriting and overriding exception class functionality.

    Example

    Following is the example, which shows how you can use std::exception class to implement your own exception in standard way −

    Open Compiler

    #include <iostream>#include <exception>usingnamespace std;structMyException:public exception{constchar*what()constthrow(){return"C++ Exception";}};intmain(){try{throwMyException();}catch(MyException& e){
    
      std::cout &lt;&lt;"MyException caught"&lt;&lt; std::endl;
      std::cout &lt;&lt; e.what()&lt;&lt; std::endl;}catch(std::exception&amp; e){//Other errors}}</code></pre>

    This would produce the following result −

    MyException caught
    C++ Exception
    

    Here, what() is a public method provided by exception class and it has been overridden by all the child exception classes. This returns the cause of an exception.

  • Write a Program to Find the Sum of the First N Natural Numbers

    C++// C++ program to find // Sum of first // n natural numbers. #include <iostream> using namespace std; // Function to find sum int findSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + i; return sum; } int main() { int n = 7; cout << findSum(n); return 0; }

    Output

    28