Found 7347 Articles for C++

Undefined Behaviour in C and C++

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

220 Views

Here we will see some C and C++ Codes. and try to guess the results. The codes will generate some runtime errors.1. The Divide By Zero error is undefined.Example Code#include using namespace std; int main() {    int x = 10, y = 0;    int z = x / y;    cout

What do you mean by a dynamic initialization of variables?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

7K+ Views

Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value of an object is to be provided during run time. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors. This type of initialization is required to initialize the class variables during run time.Why we need the dynamic initialization?Dynamic initialization of objects is needed asIt utilizes memory efficiently.Various initialization formats can be provided using overloaded constructors.It has the flexibility of using different formats of data at run time considering the situation.Example Code#include using namespace std; class simple_interest {   ... Read More

How to create timer using C++11?

George John
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how to make timer using C++. Here we are creating one class called later. This class has following properties.int (milliseconds to wait until to run code)bool (If this is true, it returns instantly, and run the code after specified time on another thread)The variable arguments (exactly we want to feed to std::bind)We can change the chrono::milliseconds to nanoseconds or microseconds etc. to change the precision.Example Code#include #include #include #include class later {    public:       template       later(int after, bool async, callable&& f, arguments&&... args){     ... Read More

What is Proxy Class in C++?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see what is the proxy class in C++. The Proxy class is basically the Proxy design pattern. In this pattern an object provides a modified interface for another class. Let us see one example.In this example, we want to make an array class, that can store only binary values [0, 1]. This is the first try.Example Codeclass BinArray {    int arr[10];    int & operator[](int i) {       //Put some code here    } };In this code, there is no condition checking. But we want the operator[] to complain if we put something like ... Read More

How to check if a variable is NULL in C/C++?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

22K+ Views

In C or C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.Here we will see one program. We will try to open a file in read mode, that is not present in the system. So the function will return null value. We can check it using if statement. See the code for better understanding.Example Code#include main() {    //try to open a file in read mode, which is not present    FILE *fp;    fp = fopen("hello.txt", "r");    if(fp == NULL)     ... Read More

How to use range-based for() loop with std::map?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we will see how to use the range based for loop for std::map type objects. In C++, we know that there are map type objects. That can store key value pairs. The map basically stores the pair objects. This pair object is used to store one key and corresponding value. These keys and values are implemented using templates, so we can use any type of data.To use the range based for loop, we can define for loop that can iterate through each pair of the map. Let us see the code to get the better idea.Example Code#include #include using ... Read More

IPC using Message Queues

George John
Updated on 30-Jul-2019 22:30:26

3K+ Views

Why do we need message queues when we already have the shared memory? It would be for multiple reasons, let us try to break this into multiple points for simplification −As understood, once the message is received by a process it would be no longer available for any other process. Whereas in shared memory, the data is available for multiple processes to access.If we want to communicate with small message formats.Shared memory data need to be protected with synchronization when multiple processes communicating at the same time.Frequency of writing and reading using the shared memory is high, then it would ... Read More

IPC through shared memory

Chandu yadav
Updated on 30-Jul-2019 22:30:26

3K+ Views

Shared memory is a memory shared between two or more processes. However, why do we need to share memory or some other means of communication?To reiterate, each process has its own address space, if any process wants to communicate with some information from its own address space to other processes, then it is only possible with IPC (inter process communication) techniques. As we are already aware, communication can be between related or unrelated processes.Usually, inter-related process communication is performed using Pipes or Named Pipes. Unrelated processes (say one process running in one terminal and another process in another terminal) communication ... Read More

What is the precision of floating point in C++?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

821 Views

In C++, the size of the floating point number is either 4-byte or 8-bytes. So it can store up to few decimal places. For example, the 1/3 = 0.333333…… Up to infinity. If we store it inside floating type variable, then it will store some significant digits. The default value is 6. So normally floating point numbers in C++ can display up to 6 decimal places.We can change the size of the precision using setprecision. This is present inside the iomanip header file. Let us see one example to get the idea.Example Code#include #include using namespace std; int ... Read More

Hidden features of C++

George John
Updated on 30-Jul-2019 22:30:26

391 Views

Here we will see some good features and tricks of C++ programming language that can help us in different area. Like if we want to participate in some competitive programming events, then these tricks will help us to reduce the time for writing codes. Let us see some of these examples one by one.Checking whether a number is odd or even without using % operator. This trick is simple. We can perform bitwise AND operation with the number and 1. If the result is non-zero then this is odd, otherwise this is even. The logic is too simple. All odd ... Read More

Advertisements