Posts

Encapsulation in C++

                Encapsulation is one of the object oriented programming concepts which binds the data and functions together. It makes the whole thing into a single unit. Generally, this single unit is considered as a class . Encapsulation is achieved by using access specifiers as follows.. ‘ public ’: member variables can be accessible for anywhere in the class. ‘ private ’: this type of variable accessed by current class only. ‘ protected ’: it can be accessible by class and derived class. Let us create a c++ program to display the student details. C++ program: Here, ‘ Student ’ is the class. It has set() method to display the data. The get() method gets the data. Using the object, we call the set() and get() function. Code: #include <iostream> using namespace std; class Student { private: string s_name; string s_id; int s_age; public: // Constructor Student(string s_name,string s_id ,int s_age) { ...

Abstraction in C++

              It is one of the Object oriented programming concepts which is the basic one. It deals with data. Abstraction shows the essential information to the user and hides the internal details. For e.g., when you drive a motor bike, you know about bike moving, but the functionality of engine is not known. This is the abstraction. Types of abstraction:               Abstraction is classified into two types as follows… ·        Data abstraction : it shows only necessary data. ·        Control abstraction : it shows only necessary information. Let us create the c++ program to implement abstraction. Code: #include <iostream> using namespace std; // Abstract class class ShapeEg { public:     // It is a virtual function     virtual void find_area() = 0;  ...

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