Found 7346 Articles for C++

RTTI (Run-time type Information) in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

462 Views

In this section we will see what is the RTTI (Runtime Type Information) in C++. In C++ the RTTI is a mechanism, that exposes information about an object’s datatype during runtime. This feature can be available only when the class has at least one virtual function. It allows the type of an object to be determined when the program is executing.In the following example the first code will not work. It will generate an error like “cannot dynamic_cast base_ptr (of type Base*) to type ‘class Derived*’ (Source type is not polymorphic)”. This error comes because there is no virtual function ... Read More

Calculation in parent and child process using fork() in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this section we will see how to use the fork() to make child process in C++. We also do some calculation into each process. So in our parent process we will find sum of all even numbers of an array, and inside the child process we will count the odd sum from array elements.When fork() is called, it returns a value. If the value is greater than 0, then currently it is in parent process, otherwise it is in child process. So using this we can distinguish between the processes.Example Code#include #include using namespace std; int main() ... Read More

Program to print hollow pyramid and diamond pattern in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

5K+ Views

Here we will see how to generate hollow pyramid and diamond patterns using C++. We can generate solid Pyramid patterns very easily. To make it hollow, we have to add some few tricks.Hollow PyramidFor the pyramid at the first line it will print one star, and at the last line it will print n number of stars. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts.Example Code#include using namespace std; int main() {    int n, i, j;    cout ... Read More

reference_wrapper in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

228 Views

In C++ the reference_wrapper is a class template that helps by wrapping a reference in a copy constructible and copy assignable object of type T. The instances of std::reference_wrapper are basically objects, but they can be converted into T&. So we can use as argument with the functions that take the underlying type by reference.Example Code#include #include using namespace std; int main () {    char a = 'h', b = 'e', c = 'l', d = 'l', e = 'o' , f = 'W', g = 'o', h = 'r', i = 'l', j = 'd';   ... Read More

fork() to execute processes from bottom to up using wait() in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

2K+ Views

We know that the fork() system call is used to divide the process into two processes. If the function fork() returns 0, then it is child process, and otherwise it is parent process.In this example we will see how to split processes four times, and use them in bottom up manner. So at first we will use fork() function two times. So it will generate a child process, then from the next fork it will generate another child. After that from the inner fork it will automatically generates a grandchild of them.We will use wait() function to generate some delay ... Read More

Why we should avoid using std::endl in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

577 Views

In this section we will see why we should avoid the std::endl while printing lines into console or a file. We use std::endl for creating a newline after the current line. For few lines of IO operations, it is not making any problems. But for large amount of IO tasks, it decreases the performance.The endl is used to create new lines, but it does not send to the new line only, after sending the cursor to the next line it flushes the buffer each time.The flushing of buffers is not the programmers task; the operating system is responsible for it. ... Read More

Calculate range of data types using C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

3K+ Views

In C++ we have different datatypes like int, char, double etc. In this section we will see how to get the size of them programmatically.We can get the size of datatypes in byte, so we can simply multiply them into 8 to get the values in bits. Now we know that if the number of bits are n, then the minimum range will be – 2^(n-1), and maximum range will be 2^(n-1) – 1 for signed numbers. For unsigned numbers it will be 2^n – 1 as there are no negative numbers.Example Code#include #include #define SIZE(x) sizeof(x) * ... Read More

Print a character n times without using loop, recursion or goto in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

935 Views

In this section we will see how to print a character n times without using loops and recursion in C++. We can solve this problem by using string class constructors. There is a constructor where we are taking the character that will be printed multiple times, and the number of times it will be printed.Example Code#include using namespace std; void print_char_n_times(char my_char, int count) {    cout

C++ Program to Construct an Expression Tree for a given Prefix Expression

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

4K+ Views

An expression tree is basically a binary tree which is used to represent expressions. In an expression tree, internal nodes correspond to operators and each leaf nodes correspond to operands. Here is a C++ program to construct an expression tree for a prefix Expression in inorder, preorder and postorder traversals.AlgorithmBegin    class ExpressionTree which has following functions:    function push() to push nodes into the tree:    If stack is null       then push the node as first element    Else       push the node and make it top        function pop() to pop ... Read More

Array class in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:25

3K+ Views

Array class in C++ are efficient enough and also it knows its own size.Functions used to perform operations on array aresize() = To return the size of array i.e. returns the no of elements of the array.max_size() = To return maximum number of elements of the array.get(), at(), operator[] = To get access of the array elements.front() = To return front element of the array.back() = To return last element of the array.empty() = Returns true if array size is true otherwise false.fill() = To fill the entire array with a particular value.swap() = To swap the elements of one ... Read More

Advertisements