Posts

OOPS Concepts in C++

                OOP – Object Oriented Programming . It is a programming approach which deals with objects and classes . It creates reusable code to achieve modularity . Why OOPs concepts? It makes the code modular. The code can be reusable any times. It is scalable. It gives you security from unauthorized access. OOPs concepts: The concepts are listed below. Class Object Abstraction Encapsulation Polymorphism Inheritance Each one of the concepts are explained with code as follows… 1.Class:               It is a prototype from which the object are created. It has a collection of variables and methods.you can create multiple objects for single class. The class has following components. Class Components: Class name: Name of the class. Access specifiers: private, public and protected Methods: it defines the functionalities. Variables: it deals with val...

User defined data types in c++(Structure and Union)

                 Basically, the data types are categorised into two types. One is the primitive data types supported by programming language. Another one is user defined datatypes which is created by the user. Some the user defined datatypes in c++ are given below. ·        Structures ·        Unions Let us implement each one of this. Structures:               It is one of the user defined data types in c++, which has collection of members of different datatypes. Syntax: Creating a structure:   Structure structure_name {                 Data type data_member1;                 Data type data_member2;        ...

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...