Found 7347 Articles for C++

Geometry using Complex Numbers in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:46:10

192 Views

In this section, we will see how to make point class using complex class from STL in C++. And apply them on some geometry related problems. The complex number is present inside the complex class from STL (#include )Defining Point ClassTo make complex to point, we will change the name of the complex as point, then change x to real() of complex class and y to imag() of complex class. Thus, we can simulate the point class.# include typedef complex point; # define x real() # define y imag()We have to keep in mind that the x and y ... Read More

Generating Test Cases (generate() and generate_n() in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:43:39

202 Views

In this section we will see how we can use C++ STL function to generate test cases. Sometimes generating test cases for array programs can be very complicated and inefficient process. C++ provides two methods to generate test cases. These methods are as follows −The generate() methodThe C++ function std::algorithm::generate() assigns the value returned by successive calls to gen to the elements in the range of first to last. It takes three parameters first, last and gen, these are forward iterator to the initial position, backward iterator to the final position and generator function that is called with no argument, ... Read More

Gaussian Filter Generation in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:41:20

1K+ Views

As we know the Gaussian Filtering is very much useful applied in the field of image processing. It is used to reduce the noise of an image. In this section we will see how to generate a 2D Gaussian Kernel. Gaussian Distribution for generating 2D kernel is as follows.$$G(x,y)= \frac{1}{2\Pi\:\sigma^{2}}e^{\frac{x^{2}+y^{2}}{2\sigma^{2}}}$$ExampleLet us see the following implementation to get better understanding − Live Demo#include #include #include #define PI 3.1415 using namespace std; void calc_filter(double kernel[][5]) {    double sigma = 1.0;    double p, q = 2.0 * sigma * sigma;    double sum = 0.0;    for (int x = -2; x

Computing index using pointers returned by STL functions in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:39:36

111 Views

In this section we will see how to generate index using pointers that are returned by STL in C++. As we know many inbuilt functions in C++ return the pointers to the position in memory which provides the address of desired number, but it has no relation with the actual index in the container of the returned value. As an example, to determine maximum element in a code, we use std::max_element() function, this does not return the index of the desired element, but return the address in memory. But sometimes, we need to get the index from that address. Here ... Read More

How to make a C++ class whose objects can only be dynamically allocated?

Arnab Chakraborty
Updated on 27-Aug-2020 13:37:16

260 Views

In this problem we will see how we can make one class for which we can only create objects through dynamic memory allocation, no direct object creation is permitted.The idea is simple. We have to create private destructor for that class. When the destructor is private, the compiler would produce a compiler error for non-dynamically allocated objects because compiler need to remove them from stack segment once they are not available for use. For dynamically allocated objects, the programmer is responsible for deleting object, but the compiler is not responsible for that, so it permits creating objects dynamically.For avoiding memory ... Read More

Why is the size of an empty class not zero in C++?

Arnab Chakraborty
Updated on 27-Aug-2020 13:33:22

2K+ Views

Suppose we have one empty class in C++. Now let us check whether its size is 0 or not. Actually, the standard does not permit objects (or classes) of size 0, this is because that would make it possible for two distinct objects to have the same memory location. This is the reason behind the concept that even an empty class must have a size at least 1. It is known that size of an empty class is not zero. Generally, it is 1 byte. See the below example.ExampleLet us see the following implementation to get better understanding − Live Demo#include ... Read More

Where is an object stored if it is created inside a block in C++?

Arnab Chakraborty
Updated on 27-Aug-2020 13:30:13

1K+ Views

In this section we will discuss about where the variables and objects are stored in the memory when a C++ program is compiled. As we know, there are two parts of memory in which an object can be stored −Stack − All members that are declared inside block of memory, it stores inside the stack section. The main function is also a function, so elements inside it will be stored inside the stack.Heap − When some objects are allocated dynamically, then that is stored inside the heap section.The scope of the object declared inside a block or a function is ... Read More

What happens when more restrictive access is given to a derived class method in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:25:03

73 Views

In this section we will discuss about interesting facts restrictive access of derived class methods in C++. We will see some examples and analyze the output to know more about restrictions of using derived class methods in C++.Example (C++)Let us see the following implementation to get better understanding −#include using namespace std; class BaseClass { public:    virtual void display(){       cout

What happens when a virtual function is called inside a non-virtual function in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:43:40

257 Views

In this section we will discuss about interesting facts about virtual classes in C++. We will see two cases first, then we will analyze the fact.At first execute the program without using any virtual function.The execute the program using any virtual function under non-virtual function.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; class BaseClass { public:    void display(){       cout

Find the value of the function Y = (X^6 + X^2 + 9894845) % 981 in C++

Arnab Chakraborty
Updated on 27-Aug-2020 12:34:32

70 Views

Suppose we have given function like f(x) = (x^6 + x^2 + 9894845) % 971, now for a given value of x, we have to find the value of f(x).So, if the input is like 5, then the output will be 469To solve this, we will follow these steps −Define a function power_mod(), this will take base, exponent, modulus, base := base mod modulusresult := 1while exponent > 0, do −if exponent is odd, then −result := (result * base) mod modulusbase := (base * base) mod modulusexponent = exponent /2return resultFrom the main method do the following −return power_mod(n, ... Read More

Advertisements