Posts

File handling in C++

                 Files are important feature in any programming language. File handling refers to reading and writing information in the files. Files have some formats like .txt,.csv and so on. C++ has lot of standard library files to handles file management. Eg: < fstream > header file   has ofstream , ifstream ,fstream. It also has ifstream(input file stream) and   ofstream(output file stream). Let us create objects and open the file as follows… Eg: ‘ifstream’ : ifstream filein("sample .txt "); ‘ofstream’ : ofstream fileout("sample.txt"); Where filein ,fileout are objects accordingly. If you are using ‘fstream’, it needs to open a file like this. ‘fstream fstr("sample.text", mode);’ Here, fstr is the object of fstream. “sample.txt” is the file name. Mode is   the purpose of operation going to be performed in the file. It has variety of types. Modes: ‘ ios::app ’ – append the contents to the en...

How to manage Threads in C++ programming?

          Threads are handled by using built-in functions in C++. The header file needed is ‘<thread>’. It has the following functions as follows…. ‘get_id()’ – it is used to get the id of the current thread. It is used for logging and debugging. ‘ hardware_concurrency() ’ - Returns the number of hardware threads available for use. ‘ detach() ’ – it allows the thread to run independently. ‘join()’ – it makes the calling thread to complete the execution. ‘mutex()’ -it is used to deal with shared data. ‘atomic()’ – it is useful to manage the threads in safe manner. ‘sleep_for()’- pauses the execution for specified time. ‘sleep_until()’-pauses the execution for time until the specified time. ‘ lock_guard ()’ – it locks and unlocks the thread in mutex block. ‘ condition_variable ’ -it is used to synchronize the threads. What are the problems in Multithreading ?            ...

Callables in Multithreading

              Callables are like a function and passed to a thread . It is executed simultaneously with the thread. Let us categorize the callables as follows. Categories: ·        Function ·        Function Object ·        Lambda Expression ·        Non- static or static Member Function Function:               It creates the function to be called with thread. C++ Program: #include < bits/stdc++.h > using namespace std ; void samp(int no) {     cout <<"The number is:"<< no; } int main() {     int n;     cout<<"Enter the number "<<endl;     cin>>n;     thread t(samp, n);     // Wait for thread to finish ...

Multithreading in C++ -Thread creations and basic functions in thread

Multithreading in C++               Thread is a light weight process under execution. If multiple threads are running, it need to manage memory, simultaneous threads. In c++, there is a header file < thread > which supports multithreading. How to create a thread? A thread is created by the use of std::thread class . The syntax is given below. Syntax: thread thread_name( callable ); here, thread_name is the object for the thread class. ‘callable’- it is a object for function object. Eg: ·        This c++ program includes the header file bits/stdc++.h . ·        It creates a function func() with a thread message to display. ·        Next, a main() function is created with threat t calling a function func() and an object is created for thread as t. ·        The object t calls a funct...

Exception Handling in C++

               It is a mechanism to find and rectify the errors in a standard way. It uses some mechanisms to handle the exceptions. It can be done by following ways. Try catch : it is a block which searches the exception and thrown it accordingly. Throws : it also throws the exception. Exception types: It can be two types. Based on logic errors ·        Domain_error ·        Array out of bound ·        Type mismatch Based on runtime error ·        Overflow ·        Underflow ·        Range Let us create a c++ program to perform try-catch block. Syntax: ‘try { } catch(Exception e) { }’ Code: #include <iostream> using namespace std; int main() {     int num, den;     cout << "Enter...

Namespace in C++

     If you want to create a special container to hold a set of names, then it is called namespace . It can be a variable name or a function. Generally, it is useful in avoiding name conflicts . The types of namespaces are listed below. 1. Global Namespace : It can be declared as globally. 2. Standard Namespace (std): it is a built-in one. 3. User-Defined Namespace : User can create the name space. 4. Nested Namespace : The namespace can be nested by itself or with other namespace. 5. Anonymous Namespace : it is a unusual one. 6. Inline Namespace ( C++ 11 and later): it is like a parent- child relationship. Eg: #include <iostream>   namespace Sample1 {     // Function greet inside namespace Sample1     void greetIt() {         std::cout << "Have a nice day!!" << std::endl;     } } namespace Sample2 {     // Function greet...

Types of Templates in C++

              The types of templates are listed as follows. Class Templates Function Templates Variable Templates (Since C++ 14 ) Class Templates :               It is a type of template which handles class to define the template. This template can be used for any data types . Program: This program creates a template “ Sample ” with a constructor and ‘get_Data()’ function to get the values from the user. In the main() function, various data are send to the get_Data() to print the data. Code: #include <iostream> using namespace std; template <typename T> class Sample               {                 public:          ...