Posts

Showing posts from October, 2025

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

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

Templates in C++

                 It is a concept which develops generic classes and functions. Using this, you can create a code that helps the user to use the code for any data type without changing the base code. How to create templates? Here, is the syntax. template < typename A , typename B , ...> eg: ‘template <typename T> T myfunction(T a, T b,……, T n);’ Why templates?               Templates are useful for the following functionality. ·        Reusability. ·        Data type safety. ·        It makes the implementation easy for vectors, maps and sorting. ·        Code duplication is avoided. The types of templates are given below… Class Templates Function Templates Variable Templates (Since C++ 14) Let ...

Dynamic Memory Allocation in C++

                 If you want to create the memory at run time, this concept is used. This allocation is done by ‘new’ and ‘delete’ operators. This happens in heap memory’s portion. Let us create a c++ program to display the memory location. C++ Program:               This program includes the header files <iostream> and <memory>. ‘main()’ function is included with pointer variable (*ptr) declaration and assignment of 10   Integer values. The value and address is printed. Code: #include <iostream> #include <memory> using namespace std; int main() {     int *ptr;     ptr = new int(10);     cout << "The value is:"<<*ptr << endl;     cout << "The address is:"<<ptr;     return 0; } Output: The value is:10 The add...