Found 7347 Articles for C++

How do inline variables work in C++/C++17?

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

3K+ Views

In C++, we can use the inline keyword for functions. In C++ 17 version, the inline variable concept has come.The inline variable is allowed to be defined in multiple translation units. It also follows the one definition rule. If this is defined more than one time, the compiler merges them all into a single object in final program.In C++ (before C++17 version), we cannot initialize the value of static variables directly in the class. We have to define them outside of the class.Example Code#include using namespace std; class MyClass {    public:       MyClass() {       ... Read More

Printing the correct number of decimal points with cout in C++

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

13K+ Views

Here we will see how to print some floating point numbers up to some pre-defined decimal places. In C++, we can use setprecision with the cout to do this word. This is present under the iomanip header file in C++.Example Code#include #include using namespace std; int main() {    double x = 2.3654789d;    cout

C++ equivalent of instanceof

Ankith Reddy
Updated on 16-Feb-2022 07:41:26

14K+ Views

C++ has no direct method to check one object is an instance of some class type or not. In Java, we can get this kind of facility.In C++11, we can find one item called is_base_of. This will check if the given class is a base of the given object or not. But, this does not verify whether the given class instance uses that function. The nearest possible functionality similar to "instanceof" can be achieved using dynamic_cast (expression). This tries to covert the given value into the specified type and returns the result. if the cast is unsuccessful this returns a null ... Read More

What are all the common undefined behaviours that a C++ programmer should know about?

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

91 Views

In C++, there are some undefined behaviors. These are identified by doing some tasks in C++. There are no such direct definitions. These few things should be known to all of the programmers, who want to use C++ for different purposes.Here we will see some C++ Codes. and try to guess the results. The codes will generate some runtime errors.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

C++ tricks for competitive programming

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

320 Views

Here we will see some good 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 numbers have ... Read More

C++ program to Zoom digits of an integer

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

129 Views

In this program we will see how to zoom digits of an integer in C++. The Zooming means print the numbers using some other characters in bigger form. The logic is simple, but we have to create larger numbers one by one from 0 to 9.Example Code#include using namespace std; void print_zero() {    for (int i=0; i

C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself

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

195 Views

In this program we will count the number of ways one number can be represented by sum of numbers smaller than itself. This program will count the partition of given numbers. We take a number n as input, then starting from a number break it by removing 1 at a time. If the new partition is generated, increase the counter.AlgorithmpartitionCount(n)Input : The number nOutput : The number of partitionsBegin    Create array p of size n    k := 0    count := -1    put n as the first element of array p    Repeat the following steps, do ... Read More

C++ Program to Compute the Area of a Triangle Using Determinants

George John
Updated on 04-Jul-2020 14:33:05

471 Views

In this section we will see how to find the area of a triangle in 2D coordinate space using matrix determinants. In this case we are considering the space is 2D. So we are putting each points in the matrix. Putting x values at the first column, y into the second and taking 1 as the third column. Then find the determinant of them. The area of the triangle will be half of the determinant value. If the determinant is negative, then simply take the absolute value of it.$$Area\:=\:absolute\:of\begin{pmatrix}\frac{1}{2} \begin{vmatrix} x_1\:\:y_1\:\:1 \ x_2\:\:y_2\:\:1 \ x_3\:\:y_3\:\:1 \end{vmatrix} \end{pmatrix}$$Here we are assuming ... Read More

Template Specialization in C++

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

691 Views

In C++, the templates are used to create generalized functions and classes. So we can use any type of data like int, char, float, or some user defined data also using templates.In this section, we will see how to use the template specialization. So now we can define some generalized template for different types of data. And some special template function for special type of data. Let us see some example to get better idea.Example Code#include using namespace std; template void my_function(T x) {    cout

Templates in C++ vs Generics in Java

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

2K+ Views

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector or vector .Example Code#include #include using namespace std; template inline T const& Max (T const& a, T const& b) { ... Read More

Advertisements