Found 7347 Articles for C++

What are the rules for calling the superclass constructor C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

9K+ Views

In C++, we can derive some classes. Sometimes we need to call the super class (Base class) constructor when calling the constructor of the derived class. Unlike Java there is no reference variable for super class. If the constructor is non-parameterized, then it will be called automatically with the derived class, otherwise we have to put the super class constructor in the initializer list of the derived class.In this example at first we will see constructor with no argument.Example Live Demo#include using namespace std; class MyBaseClass {    public:       MyBaseClass() {          cout

How to stop C++ console application from exiting immediately?

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

807 Views

Sometimes we have noticed that the console is being closed immediately after displaying the result. So we cannot see the result properly. Here we will see how we can stop the console from closing.The idea is very simple. We can use the getchar() function at the end. This will wait for one character. If one character is pressed, the console will exit.Example Live Demo#include using namespace std; int main() {    cout

C++ Program to Implement Johnson’s Algorithm

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

1K+ Views

Here we will see the Johnson’s Algorithm to find shortest path between two vertices. The graph is given here. The shortest path between the edges is like below. This program will take the number of vertices, number of edges, and the edges with their costs.Input − Vertices: 3Edges: 5Edge with costs −1 2 82 1 121 3 223 1 62 3 4Output − The distance matrix of the graph.081210046140AlgorithmjohnsonAlgorithm(cost)Input − The cost matrix of given Graph.Output − Matrix to for shortest path between any vertex to any vertex.Begin    Create another matrix ‘A’ same as cost matrix, if there is no ... Read More

C++ Program to Create a Random Linear Extension for a DAG

Smita Kapse
Updated on 30-Jul-2019 22:30:26

72 Views

Here we will see how to create Random Linear Extension of a Directed Acyclic Graph (DAG). The Linear extension is basically the topological sorting of DAG. Let us consider the graph is like below −The topological sorting for a directed acyclic graph is the linear ordering of vertices. For every edge u-v of a directed graph, the vertex u will come before vertex v in the ordering.As we know that the source vertex will come after the destination vertex, so we need to use a stack to store previous elements. After completing all nodes, we can simply display them from ... Read More

Bidirectional Iterators in C++

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

336 Views

Here we will see the concept of Bidirectional iterators in C++.The bidirectional iterators support all of the features of forward iterators, and also prefix and postfix decrement operators.This type of iterator can access elements in both directions, like towards the end and towards the beginning.The random access iterator is also a type of bidirectional iterator.Bidirectional iterators have features of forward iterator, but only difference is this iterator can also be decremented.The Bidirectional iterators has some properties. These are like below.PropertyExpressionThe bidirectional iterator is default-constructible, copy-assignable, and also destructibleA pA q(p) q = pWe can compare them using equality and inequality operatorsp ... Read More

Output Iterators in C++

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

519 Views

Here we will see what are the Output iterators in C++. The Output iterators has some properties. These are like below:The output iterators are used to modify the value of the containers.We cannot read data from container using this kind of iteratorsThis is One-Way and Write only iteratorIt can be incremented, but cannot be decremented.There are two sub-parts of Output iterators. These are Insert Iterator and ostream iterator.The Insert IteratorThe insert iterator is used to insert some element inside the container. The assignment operator on this type of iterator inserts new element at current position. The syntax of insert iterator ... Read More

Composite Design Pattern in C++

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

1K+ Views

Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy. This type of design pattern comes under structural pattern as this pattern creates a tree structure of group of objects.This pattern creates a class that contains group of its own objects. This class provides ways to modify its group of same objects.We are demonstrating use of composite pattern via following example in which we will show employees hierarchy of an organization.Here we ... Read More

Iterator Invalidation in C++

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

1K+ Views

In C++, we have different containers like vector, list, set, map etc. To iterate through these containers, we can use the iterators. We should be careful when we are using iterators in C++. When we are using iterating over a container, then sometimes, it may be invalidated. If the shape, size is changed, then we can face this kind of problems. In the following example, we can identify the problem of invalidation.Example Code#include #include using namespace std; int main() {    vector vec{11, 55, 110, 155, 220};    for (auto it=vec.begin(); it!=vec.end(); it++)       if ... Read More

I/O Redirection in C++

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

530 Views

In C, we can use the freopen() function for redirection purposes. Using this function, we can redirect existing FILE pointer to another stream. The syntax of the freopen is like below:FILE *freopen(const char* filename, const char* mode, FILE *stream)In C++ also, we can do the redirection. In C++, the streams are used. Here we can use our own stream, and also redirect system streams. In C++, there are three types of streams.istream : Stream, that can support input onlyostream : Stream, that can support output onlyiostream : These can be used for input and output.These classes, and file stream classes ... Read More

Floating Point Operations and Associativity in C, C++ and Java

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

190 Views

In C, C++, and java, we do some mathematical operations with floating point numbers. Now here we will check whether the floating point numbers are following the associativity rule or not.The answer is NO. The floating point numbers does not follow the associativity rules in some cases. Here we will see some examples.Example Code#include using namespace std; main() {    float x = -500000000;    float y = 500000000;    float z = 1;    cout

Advertisements