what is C++ Language ? and explain it.

What is C++ ? Explain it.

Basic concepts:

πŸ‘‰C++ is an object oriented programming language. It is a concept of  C language and it's extend version of C language. It was developed by Bjarne Stroustrup at AT&T bell lab in New Jersey, USA in the early 1980's.

πŸ‘‰In C++ language program first write header files like as iostream.h ,conio.h , etc....as per requirement of program. After header file write class declaration or definition as per your planning. After class , define all member functions which are not define but declare inside the class. In last write main function without main function program execution is not possible. 

this type program:
Example:
          #include <iostream>  //Header file
          using namespace std;
          class trial                    //Class
          {
                    int a;
               public;
                    void getdata()
                    {
                          a=10;
                     }
                    void putdata();
          };
          void trial::putdata()    //Member Function of outside class
          {
                     cout<<"The value of a= "<<a;
           }
           int main()                  //Main Function
          {
                     trial T;
                     T.getdata();
                     T.putdata();
           }
     
           output:
           The value of a = 10


Following  tearms:

πŸ‘‰Namespace:
                           It defines a scope for the identifiers that are used in a program. For using identifiers defined in namespace using directive is used as using namespace std; . std is the namespace where defined of ANSI C++ standard class library and this type program is directive. This will bring all the identifiers defined in std to the current global scope.

πŸ‘‰Keywords:
                           They are explicitly reserved identifiers and cannot be used as names for the program variables or other user-defined program elements.
Ex: int, class , void etc....

πŸ‘‰Identifiers:
                           They refer  to the name of variables, functions, arrays ,classes etc., created by the programmer. Each language has its own rules for naming these identifiers.
*Following are common rules for naming these identifiers.
 1] only alphabetic characters , digits, and underscores are permitted.
 2] The name cannot start with a digit.
 3] Uppercase and lowercase letters are distinct.
 4] A declared keyword cannot be used as a variable name.

πŸ‘‰Constants:
                           Like variables, constants are data storage locations. But variables can vary, constants do not change. You must initialize a constant when you create it, and you can not assign new value later , after constant is initialized. Define constant using #difine: . #define is a preprocessor directive that declares symbolic constant. For example syntax: #define PI 3.14 .Every time the preprocessor sees the word PI, it puts 3.14 in the text.
Example:
               #include<iostream>
               using namespace std;
               #define PI 3.14

               int main()
              {
                         int r, area;
                         cout<<"enter Radius :";
                         cin>>r;
                         area=PI*r*r;
                         cout<<"Area of Circle = "<<area;
                         return 0;
               }
               
               Output:
               Enter Radius :5
               Area of Circle = 78.5

☺Define constant  using const keyword: 
☺'const' keyword is used to declare constant variable of any type.
☺We can not change its value during execution of program.
☺Syntax: const Data Type Variables_Name=value;
    Ex: const int a=2;
☺Now 'a' is a integer type constant.

Various Data types:

πŸ‘‰Three part of Data types used in C++
      1]Primary data type
      2]Derived data type
      3]User defined data type
         
πŸ‘‰Primary Data types:
   *The primary data type of C++ is as follow.
    

πŸ‘‰Derived Data types:
  *Following derived data types.
  1] Arrays 
  2] Function
  3] Pointers

Arrays: An array is a fixed-size sequenced collection of elements      of the same data type.
Pointer: Pointer is a special variable which contains address of     another variable.
Function: A group of statements combined in one block for some      special purpose.

πŸ‘‰User Defined Data types:
   ☺We have following type of user defined data type in C++     language.
   ☺The user defined data type is defined by programmer as per       his/her requirement.
   ☺Structure: Structure is a collection of logically related data     items of different data types grouped together and known by a   single name.
   ☺Union: Union is like a structure, except that each element       shares the common memory.
   ☺Class: A class is a template that specifies the fields and methods    of things or objects. A class is a prototype from which objects are      created.
Enum: Enum is a user-defined type consisting of a set of named      constants called enumerator.
   ~In other words enum is also used to assign numeric constants to      strings.
   ~Syntax of enumeration: enum enum_tag {list of variables};
   ~Example of enumeration: enum day-of-week
                                              {mon=1,tue,wed,thu,fri,sat,sun};

Various operators:

πŸ‘‰Eleven operators use in the C++ language.
    1] Arithmetic Operators
    2] Relational Operators
    3] Logical Operators
    4] Assignment Operators
    5] Increment and Decrement Operators
    6] Conditional Operator
    7] Bitwise Operators
    8] Special Operators
    9] Extraction Operator (>>)
  10] Insertion Operator(>>)
  11] Scope resolution operator (::)

πŸ‘‰ Arithmetic Operators:
☺Arithmetic operators are used for mathematical calculation.
☺C++ supports following arithmetic operators:
 

 πŸ‘‰Relational Operators:
☺Relational Operators are used to compare two numbers and     taking decisions based on their relation.
☺Relation expression are used in decision statements such as if, for,   while, etc....
 
πŸ‘‰Logical Operators:
 ☺Logical operators are used to test more than one condition and 
     make decisions.
      

πŸ‘‰Assignment Operator:
Assignment operators are used to assign the result of an                      expression to a variable . C++ also supports shorthand assignment      operators which simplify operation with assignment.
    

πŸ‘‰Increment and Decrement Operators:
☺These are special operators in C++ which are generally not found
    in other languages.

πŸ‘‰Conditional Operator:
 A ternary operator is known as condition operator.
     exp1?exp2:exp3 if exp1 is true then execute exp2 otherwise exp3

     Example: x=(a>b)?a:b; which is same as
                 if(a>b)
                     x=a;
                 else
                     x=b;

πŸ‘‰Bitwise operators:
Bitwise operators are used to perform operation bit by bit. Bitwise      operators may not be applied to float or double.
     
πŸ‘‰Special Operators:

πŸ‘‰Extraction Operator(>>):
Extraction Operator (>>) is used with cin to input data from  
    keyword.

πŸ‘‰Insertion Operator(<<):
☺Insertion operator (<<) is used with cout to output data from 
    keyword.

πŸ‘‰Scope resolution operator (::) :-
Scope resolution operator (::) is used to define the already 
    declared member functions of the class.


Memory Management operators of C++ :
☺For dynamic memory management, C++ provides two unary       operators 'new' and 'delete'.
☺An object can be created by using new, and destroy by using     delete, as and when required .
☺Dynamic  allocation of memory using new
☺Syntax of new :
                              pointer_variable = new data_type;
☺Here pointer_variable is a pointer of any data type.
☺The new operator allocates sufficient memory to hold a data     objects.
☺The pointer_variable holds the address of the memory space   allocated.
☺For example:
                         p=new int;
                         q=new float;
☺Type of 'p' is integer and type of 'q' is float.
☺We can combine declaration and initialization .
                         int *p=new int;
                         float *q=new float;
☺We can dynamic allocate space for array, structure and classes by      new.
                          int *p=new int[10];
☺Allocates a memory space for an array of size 10.
☺p[0] will refer location of p[1] and p[1] will refer location of [2] 
    and so on.

☺Release memory using Delete:
☺When a data object is no longer needed, it is destroyed to release        the memory space for reuse.
☺Syntax of Delete:
                         delete pointer_variable;
☺The pointer_variable is the pointer that points to a data object 
    created with new.
☺For example:
                         delete p;
                         delete q;
☺To free a dynamically allocated array
                         delete [size] pointer_variable;
                         delete [10]p;

Reference variable and it's major use :

                          A reference variable provides an alias(alternative name) for a previously defined variable. This mechanism is use in object oriented programming because it's permits the manipulation of object by reference , and eliminates the copying of object parameters back and forth. It is also important note that references can be created not only built-in data types but also user-defined data types.
☺Syntex of delete: Data_type & reference_name = variable_name.
Example: 
                    int a=100;
                    int &b=a;    //Now both a and b will give same value.

 Use of Scope resolution operator (::) with example:

                         The scope resolution operator is used to resolve or extend the scope of variable. C++ is block structured language. we know that the same variable name can be used to have different meaning in different block. The scope resolution operator will refer value of global variable from anywhere. without scope resolution operator all variable will refer local value.

Example:
#include<iostream.h>
int m=10;
int main()
{
          int m=20;
         {
                    int k=m;
                    int m=30;
                    cout<<"we are in inner block\n';
                    cout<<"k="<<k<<"\n";
                    cout<<"m="<<m<<"\n";
          }
          cout<<"we are in outer block\n";
          cout<<"m="<<m<<"\n";         
          cout<<"::m="<<::m<<"\n";
          return 0;
}

Output:
we are in inner blolck
k=20
m=30
we are in outer block 
m=20
::m=20

Member dereferencing operator:

                          C++ provides three pointers to member operators to access the class member through pointer.


Implicit and Explicit type conversion :

πŸ‘‰Implicit conversion:
                          Wheneer data types are mixed in an expression , C++ performs the conversions automatically. This process is known as implicit or automatic conversion. ex:m=5+2.5;...For a binary operator, if the operands type differs, the compiler converts one of them to match with the other. Using the rule that the smaller type is converted to wider type. For example if one operand is integer is converted to float because float is wider than integer. In above example answer means m, will be in float.

πŸ‘‰Explicit conversion:
                          C++ permits explicit type conversion of variables or expressions using the type cast operator.
☺Syntex:type_name(expression)
☺Exanple : avaerage = sum/float(i);
                          Alternatively we can use typedef to create an identifier of the required type and use it in the functional notation.
☺Example:
                    typedef int * int_ptr;
                    p = int_ptr(q);

Example:
#include<iostream>
using namespace std;

int main()
{  
          int intvar = 5;
          float floatvar = 3.5;

          cout<<"intvar = "<<intvar;
          cout<<"\nfloatvar = "<<floatvar;
          cout<<"\nfloat(intvar) = "<<float(intvar);
          cout<<"\nint(floatvar) = "<<int(floatvar);
}

Output:
intvar = 5
floatvar = 3.5
float(intvar) = 5
int(floatvar) = 3



          






 


   



                    























                      




Comments