Wednesday 25 September 2019

C++ PROGRAMMING !





What is C++?
C++ PROGRAMMING.


C++ language is a direct descendant of C programming language with additional features such as type checking, object oriented programming, exception handling etc.


 You can call it a “better C”. It was developed by Bjarne  Stroustrup.

C++ is a general purpose language language, when I say general purpose it simply means that it is designed to be used for developing applications in a wide variety of domains.








C++ is a general-purpose programming language.
It was created by Bjarne Stroustrup at Bell Labs circa 1980.
C++ is very similar to C (invented by Dennis Ritchie in the early 1970s).
 C++ is so much compatible with C that it will probably compile over 99% of C programs without changing a line of source code.
Though, C++ is a lot well-structured and safer language than C as it OOPs based.

Some computer languages are written for a specific purpose. Like, Java was initially devised to control toasters and some other electronics.
 C was developed for programming OS. Pascal was conceptualized to teach proper programming techniques.
 But C++ is a general-purpose language. It well deserves the widely acknowledged nickname "Swiss Pocket Knife of Languages."





 Who uses C++?



C++.
 
Some of today's most visible used systems have their critical parts written in C++.

Examples are Amadeus (airline ticketing)

    Bloomberg (financial formation),
    Amazon (Web commerce), Google (Web search)
    Facebook (social media)5 Basic Concepts of C++












C++ Variables-:





 

    Variables are the backbone of any programming language.
    A variable is merely a way to store some information for later use. We can retrieve this value or data by referring to a "word" that will describe this information.
    Once declared and defined they may be used many times within the scope in which they were declared.








C++ Programming basics  -:




Output using cout. Directives -:



To output information in C++ we will use a variable called cout (pronounced "see-out"). The identifier cout stands for common output. cout is a predefined variable in C++ that indicates you are going to output a stream of characters to an output device. 

cout uses an operator called the insertion operator (<<).


Input with cin -:





The cin object in C++ is an object of class istream. It is used to accept the input from the standard input device i.e. keyboard.



Type bool -:







In C++, the data type bool has been introduced to hold a boolean value, true or false.The values true or false have been added as keywords in the C++ language. Important Points: The default numeric value of true is 1 and false is 0.





Type Conversion in C++ -:





Done by the compiler on its own, without any external trigger from the user.

Generally takes place when in an expression more than one data type is present. ...
All the data types of the variables are upgraded to the data type of the variable with largest data type.





Functions  -:


function -:




A function is a group of statements that together perform a task. ... A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C++ standard library provides numerous built-in functions that your program can call.




Returning values from functions -:

A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string.


Reference arguments -:



C++ function call by reference. ... Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by reference, argument reference is passed to the functions just like any other value.



overloaded function -:



Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++.




Inline function -:



Inline function in C++ Programming. Inline function is a function which when invoked requests the compiler to replace the calling statement with its body. A keyword inline is added before the function name to make it inline. ... Member functions of a class are inline by default even if the keyword inline is not used.



Default arguments -:






A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn't provide a value for the argument with a default value. Following is a simple C++ example to demonstrate the use of default arguments.



Returning by reference -:



A C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.




Object and Classes  -:




C++ Classes and Objects -:






Class: The building block of C++ that leads to Object Oriented programming is a Class. It is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.

A Class is a user defined data-type which has data members and member functions.

Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc and member functions can be apply brakes, increase speed etc.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.


Encapsulation in C++ -:






In normal terms Encapsulation is defined as wrapping up of data and information under a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulates them. ... Encapsulation also lead to data abstraction or hiding.



Abstraction in C++ -:






 Data abstraction is one of the most essential and important feature of object oriented programming in C++. Abstraction means displaying only essential information and hiding the details.


polymorphism in c++ -:







The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.



association in c++ -:







In C/C++ domain modeling class diagrams, an association is a structural relationship that indicates that objects of one classifier, such as a class and interface, are connected and can navigate to objects of another classifier. ... Associations can help you make design decisions about the structure of your data.




C++ Interfaces -:





 An interface is a description of what member functions must a class, which inherits this interface, implement. In other words, an interface describes behavior of the class. You can imagine an interface as a list of functions that must be implemented by a class.



implementation of class in c++ -:



An implementation file is used in C++ programming when creating a class definition to split the interface from the implementation. The header file would declare all the member functions (methods) and data methods (fields) that the class has.


constructor in c++ -:









A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.


objects as function arguments in c++ -:




Objects as Function Arguments in c++ The objects of a class can be passed as arguments to member functions as well as nonmember functions either by value or by reference. When an object is passed by value, a copy of the actual object is created inside the function. This copy is destroyed when the function terminates.




default copy constructor c++ -:







C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy.





Const member functions in C++ -:


Like member functions and member function arguments, the objects of a class can also be declared as const. ... A const object can be created by prefixing the const keyword to the object declaration. Any attempt to change the data member of const objects results in a compile-time error.







Arrays and string arrays fundamentals -:


Arrays in C++ -:





array is a collection of similar items stored in contiguous memory locations. 

In programming, sometimes a simple variable is not enough to hold all the data. 

For example -:



lets say we want to store the marks of 500 students, having 500 different variables for this task is not feasible, we can define an array with size 500 that can hold the marks of all students.




Declaring an array in C++ -:






There are couple of ways to declare an array.



Method 1 -:


int arr[5];

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;


Method 2 -:


int arr[] = {10, 20, 30, 40, 50};



Method 3 -:


int arr[5] = {10, 20, 30, 40, 50};




Accessing Array Elements -:



Array index starts with 0, which means the first array element is at index 0, second is at index 1 and so on. 

We can use this information to display the array elements. 

See the code below -:




#include <iostream>

using namespace std;

int main(){

   int arr[] = {11, 22, 33, 44, 55};
   cout<<arr[0]<<endl;
   cout<<arr[1]<<endl;
   cout<<arr[2]<<endl;
   cout<<arr[3]<<endl;
   cout<<arr[4]<<endl;
   return 0;
}


Output -:




11

22
33
44
55



Multidimensional Arrays in C++ -:


Multidimensional arrays are also known as array of arrays. The data in multidimensional array is stored in a tabular form, as shown in the diagram below  -:







A two dimensional array -:





int arr[2][3];

This array has total 2*3 = 6 elements.



A three dimensional array -:





int arr[2][2][2];

This array has total 2*2*2 = 8 elements.

Two dimensional array -:



Lets see how to declare, initialize and access Two Dimensional Array elements.



int myarray[2][3];



Initialization -:



We can initialize the array in many ways -:



Method 1 -:


int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22};



Method 2 -:


This way of initializing is preferred as you can visualize the rows and columns here.


int arr[2][3] = {{10, 11 ,12} , {20 ,21 , 22}};



Accessing array elements -:


arr[0][0] – first element

arr[0][1] – second element
arr[0][2] – third element
arr[1][0] – fourth element
arr[1][1] – fifth element
arr[1][2] – sixth element

Example: Two dimensional array in C++ -:




#include <iostream>

using namespace std;

int main(){

   int arr[2][3] = {{11, 22, 33}, {44, 55, 66}};
   for(int i=0; i<2;i++){
      for(int j=0; j<3; j++){
        cout<<"arr["<<i<<"]["<<j<<"]: "<<arr[i][j]<<endl;
      }
   }
   return 0;
}


Output -:




arr[0][0]: 11

arr[0][1]: 22
arr[0][2]: 33
arr[1][0]: 44
arr[1][1]: 55
arr[1][2]: 66


Three dimensional array -:



Lets see how to declare, initialize and access Three Dimensional Array elements.


Declaring a three dimensional array -:




int myarray[2][3][2]; 



Initialization -:



We can initialize the array in many ways -:



Method 1 -:


int arr[2][3][2] = {1, -1 ,2 ,-2 , 3 , -3, 4, -4, 5, -5, 6, -6};



Method 2 -:


This way of initializing is preferred as you can visualize the rows and columns here.


int arr[2][3][2] = {

     { {1,-1}, {2, -2}, {3, -3}},
     { {4, -4}, {5, -5}, {6, -6}}
}


Three dimensional array example -:



#include <iostream>

using namespace std;

int main(){

   

   int arr[2][3][2] = {

      { {1,-1}, {2,-2}, {3,-3} },
      { {4,-4}, {5,-5}, {6,-6} }
   };
   

   for (int x = 0; x < 2; x++) {

     for (int y = 0; y < 3; y++) {
       for (int z = 0; z < 2; z++) {
         cout<<arr[x][y][z]<<" ";
       }
     }
   }
   return 0;
}



Output -:



1 -1 2 -2 3 -3 4 -4 5 -5 6 -6








Overloading in c ++ -:






Function Overloading in C++ -:






Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.
Function overloading can be considered as an example of polymorphism feature in C++.

Following is a simple C++ example to demonstrate function overloading -:






#include <iostream> 
using namespace std; 
  
void print(int i) { 
  cout << " Here is int " << i << endl; 

void print(double  f) { 
  cout << " Here is float " << f << endl; 

void print(char const *c) { 
  cout << " Here is char* " << c << endl; 

  
int main() { 
  print(10); 
  print(10.10); 
  print("ten"); 
  return 0; 
}



Output -:



Here is int 10 
Here is float 10.1 
Here is char* ten 








Operator overloading  -:






Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. 
Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.

In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +.
Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional Number, Big Integer, etc.

A simple and complete example -:



#include<iostream> 
using namespace std; 
  
class Complex { 
private: 
    int real, imag; 
public: 
Complex(int r = 0, int i =0)  {real = r;   imag = i;} 
Complex operator + (Complex const &obj) { 
         Complex res; 
         res.real = real + obj.real; 
         res.imag = imag + obj.imag; 
         return res; 
    } 
    void print() { cout << real << " + i" << imag << endl; } 
}; 
  
int main() 

    Complex c1(10, 5), c2(2, 4); 
    Complex c3 = c1 + c2; // An example call to "operator+" 
    c3.print(); 





Output -:




12 + i9