Programming in C++ (Basics of C++)

Programming in C++ 

C++ is a general-purpose programming language which is used for developing Games, GUI based applications, Image editing Applications, Web browser, Compiler, OS, etc. It also supports Object-Oriented Programming.

Writing the first program in C++

Here, we will understand and write our first program in C++. We will write the program which prints 'Hello World' as output. So, it is also called the 'Hello World' program in C++. Let's see it

Example: Hello World program in c++



#include<iostream>
using namespace std;
int main()
 {
  cout<<"Hello World..! \n"

  return 0;
 }

Output



Hello World..! 


#include<iostream>: This statement tells the compiler to include this header file named iostream so that we can use the predefined input-output functions in the program.

using namespace std: This is used to identify whether cin or cout is user built function or standard identifiers. here, std represents standard identifiers (cout and cin). 

If you do not include using namespace std in a program and make use of cin and cout then it will throw an error. However, this error can be solved either by making use of using namespace std in the program or by using std::cout<< and std::cin>> in the program.

Generally we add single using namespace std in the program rather than writing all cout and cin statements like std::cout and std::cin in the program. It makes it easier for us to program.

int main(): This is the main function of our program. The program execution begins with this function. Here, int is the return type that means it return some integer value at last and that is the reason behind writing return 0 at last in the program.

cout<<"Hello World..!": The cout work is to display whatever is written between double-quotes. It is the standard identifier belonging to the iostream file. This can also display the value of any variables written with it. We will see it later.

return 0: This means that the execution of the main function is now successful.

Variables in C++

Variable is simply a name that is given so that any value can be entered into it or associated with it. It can be changed or varied. for example int n=10; here n is a variable that is associated with value 10 which can be changed in the program according to the need.

Syntax

dataType variable_name1 = value, variable_name2 = value;

for example:

int num1 = 15, num2 = 26;

It can also be written as:


int i, j;

i = 10;

j = 20;


The scope of variables can be broadly classified as:

  1. Local Variables.
  2. Global Variables.

    The variable that is introduced inside a function or block is called a local variable. It can only be used by statements specified inside a function or block of code. They are not recognized by the outsider functions. for example:

    Example: local variable in c++


    #include<iostream>
    int gone(int,int);
    int main()
     {
      int b;                              // Local variable
    
      int s = 6, u = 8;                   // Local variable
    
      b = gone (s, u);
    
      cout << "\n The Output is: " << b;
     }
    
    int gone(int x, int y)
     {
      int z;
    
      z = x+y;
    
      return z;
     }


    Global variable as specified by the name itself, can be used by the functions or by any other external functions. for example:

    Example: global variable in c++

          
    #include<iostream>
    int x, y, z;                      // Global Variable
    float m, n, f;                    // Global Variable
    
    void main()
     {
      int s, u;                      // Local Variable
      float w, j;                    // Local Variable 
      ....
      ...
      ..
      .
     }




    Data types in C++

    Data-Types-C-Plus-Plus























    Data types define what type of data the variable is going to hold. for example, int defines that the variable holds all integer value and char tells that variable can only hold character value.

    The above picture shows all data types that are used in C++ programming language.


    Operators in C++

    Operators in C++ are the same as compared to C programming language. The following are the different operators used in C and C++.

    Operators are the symbol that is used to perform some operations. In C++, operators are described in two ways.

    C++ operators on the basis of the type of operation they show:
    1. Arithmetic or Mathematical
    2. Relational
    3. Logical
    4. Assignment
    5. Bitwise
    6. Size of
    7. Comma
    8. Ternary

    Arithmetic or Mathematical operators in C++

    1. +  Addition
    2. -   Subtraction
    3. *  Multiplication
    4. /   Division
    5. Modulus
    These are the arithmetic operators. They are also called binary operators because they need two operands for performing an operation.

    Example: arithmetic operator in c++


    #include<iostream>
    using namespace std;
    int main()
     {
       int num1 = 10, num2 = 7;
    
       cout << "sum is: " << (num1+num2) << endl;        
       cout << "minus is: " << (num1-num2) << endl;       
       cout << "product is: " << (num1*num2) << endl;     
       cout << "division is: " << (num1/num2) << endl;  
       cout << "modulo is: " << (num1%num2) << endl;     
     }
    

    Output


    sum is: 17
    minus is: 3
    product is: 70
    division is: 1
    modulo is: 3



    Explanation:
    1. When we add 10 and 7, we get 17.
    2. When we subtract 7 from 10, we get 3.
    3. When we multiply 7 and 10, we get 70.
    4. When we divide 10 and 7, we get 1 as quotient and 3 as remainder.
    5. The symbol ' ' gives us quotient. So, the answer is 1.
    When we divide 10 and 7, we get 1 as quotient and 3 as remainder.
    The symbol ' ' gives us the remainder. So, the answer is 3.

      Relational operators in C++

      1. <      less than 
      2. >      more than
      3. < =    less than equal to
      4. > =    more than equal to
      5. = =    equals to
      6. ! =     not equals to
      These are called relational operators because they all represent some relationships between the operands. They are also binary operators as they require 2 operands for performing operations.

      Logical operators in C++

      1.  &&    AND
      2.  | |        OR
      3.  !         NOT
      Logical operators are used to connect two or more than two relations. An expression including a logical operator replies 0 or 1. The reply totally depends upon whether the expression results in true (1) or false(0). It is used for statements that are decision-makers.

      && - Logical AND. True only if all operands are True otherwise False.
      || - Logical OR. True if one operand is True otherwise False.
      ! - Logical NOT. True if the operand is False otherwise False.


      Assignment operators in C++

      Assignment operators those operators that manipulate the value of any variables. Assignment operators are classified as: 

      1.  Constructive assignment operators
      2.  Destructive assignment operators
      Destructive assignment operators overwrites the new value of the variable in the place of the older one whereas constructive assignment operators update the old value of the variable with the newer value. 

      There can be addition, subtraction, multiplication, etc depends on the operators that are being used.

      Destructive assignment operator is: " "
      Constructive assignment operators are: += , -= *= , /= , %= , >>= , <<=



      Bitwise operator in C++

      1.  & Bitwise AND
      2.  |   Bitwise OR
      3.  ^  XOR (Exclusive OR)
      4.  ~ 1's Complement.
      5.  >> Right shift
      6.  << Left shift
      Bitwise operators operate on the binary value of an operand.


      Sizeof operator in C++ [sizeof ()]

      The sizeof operator in c is used to determine the memory consumption of any variable and data types. 

      Example: sizeof operator in c++

      #include<iostream>
      using namespace std;
      void main()
       {
        int a;
      
        cout << sizeof(a) << endl;              // prints size of a.
        cout << sizeof(int);                    // prints size taken by int datatype.
       }
      

      Output


      4
      4
      




      Comma operator in C++

      It is used as a separator between similar types of declaration. When the programmer wants to declare multiple variables of the same data types. Then, they make use of the comma operator as a separator between the declared variables.

      Example: comma operator in c++

      #include<iostream>
      void main()
       {
        int a, b, c, e, f;                // comma operator 
      
        char m, n, k, g;                  // comma operator 
      
        float x, y, s;                    // comma operator 
       }
      



      Conditional or Ternary operator in C++

      The conditional operator is the only operator in C language which requires three operands to complete its operations. 

      It is a replacement of if-else statement i.e., the programs that are made in multiple lines using an if-else statement can be made in a simple one-line using conditional operator.

      Conditional operator is formed by the combination of two symbols ( ? and :).

      It works exactly like an if-else statement where it tests for some condition first and after its evaluation executes either expression 1 or expression 2 as shown below in the syntax.


      Syntax:


      (True Condition) ? expression1 : expression2;

      Example: conditional or ternary operator in c++


      #include<iostream>
      using namespace std;
      void main()
       {
        int a, b, c;
      
        cout << "Enter two values \n";
        cin >> a >> b;
      
        c = (a>b) ? a : b;
      
        cout << " \nGreatest number is : " << c;
       }
      

      Output


      Enter two values 
      65
      101
      Greatest number is : 101
      



      Example: conditional or ternary operator in c++



      /*     programs to find the greatest among three numbers 
             using the conditional operator or conditional statement       */
      
      #include<iostream>
      using namespace std;
      int main()
       {
        int a, b, c, big;
      
        cout << "Enter three values \n";
        cin >> a >> b >> c;
      
        big = a  > b ? (a  > c ? a : c) : (b  > c ? b : c) ;
      
        cout << "\nGreatest number is : " << big;
       }
      

      Output


      Enter three values 
      56
      25
      111
      Greatest number is : 111
      



            Previous
            Next Post »

            Comment Below: ConversionConversion EmoticonEmoticon