Found 7347 Articles for C++

What are __FILE__, __LINE__, and __FUNCTION__ in C++

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

6K+ Views

Here we will see what are the __FILE, __LINE__ and __FUNCTION__ in C++.The __FILE__This macro is used to get the path of the current file. This is useful when we want to generate log files. The following code will explain its functionality.Example#include using namespace std; int errorLog (const char* file, const std::string& msg){    cerr

Printing 1 to 1000 without loop or conditionals in C/C++

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

437 Views

Here we will see how to print 1 to 1000 without loop or any conditional statements. As the loops cannot be used, so we can try recursions, but here another constraint that, we cannot use the conditions also. So the base case of the recursion will not be used.Here we are solving this problem using static members. At first we are initializing the static member with 1, then in the constructor we are printing the value and increase its value. Now create an array of 1000 objects of that class, so 1000 different objects are created, so the constructor is ... Read More

Dynamic_cast and static_cast in C++

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

3K+ Views

static_cast: This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. This can cast related type classes. If the types are not same it will generate some error.Example#include using namespace std; class Base {}; class Derived : public Base {}; class MyClass {}; main(){    Derived* d = new Derived;    Base* b = static_cast(d); // this line will work properly    MyClass* x = static_cast(d); // ERROR will be generated ... Read More

What is an unsigned char in C++?

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

5K+ Views

In C++ we have seen there is character type data called char. Sometimes we have seen unsigned char also. So here we will see what is basically the unsigned char means. What are the basic differences between signed char and unsigned char?Signed char and unsigned char both are used to store single character. The variable stores the ASCII value of the characters. For an example if ‘A’ is stored, actually it will hold 65. For signed char we need not to write the signed keyword. But for unsigned, we have to mention the keyword. The syntax is like below.unsigned char ... Read More

Meaning of 'const' last in a function declaration of a C++ class?

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

709 Views

Sometimes we can find the keyword ‘const’ present at the last of function declaration. So what does it mean?Using this one function can be made as constant. The idea of constant function is that, the function cannot be modified from the objects, where they are called. It is recommended to use the constant functions in our program.Let us see one example of constant function.Example#include using namespace std; class MyClass {    int value;    public:       MyClass(int val = 0) {          value = val;       }       int getVal() const ... Read More

How to initialize private static members in C++?

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

20K+ Views

Here we will see how to initialize the private static member variables initialization in C++. We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class.To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.The following code will illustrate the of static member initializing technique.Example#include using namespace std; class MyClass{    private:       static int st_var;    public:       MyClass(){          st_var++; //increase the value ... Read More

C++ Program to Perform Baillie-PSW Primality Test

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

393 Views

The Baillie-PSW Primality Test, this test named after Robert Baillie, Carl Pomerance, John Selfridge, and Samuel Wagstaff. It is a test which tests whether a number is a composite number or possibly prime.AlgorithmMillerTest()Begin    Declare a function MillerTest of Boolean type.    Declare MT_dt and MT_num of integer datatype and pass as the parameter.    Declare MT_a and MT_x of integer datatype.       Initialize MT_a = 2 + rand( ) % (MT_num - 4).       Initialize MT_x = pow(MT_a, MT_dt, MT_num).    if (MT_x == 1 || MT_x == MT_num - 1) then       ... Read More

C++ Program to Implement the Solovay-Strassen Primality Test to Check if a Given Number is Prime

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

228 Views

Solovay-Strassen Primality Test is used to test a number whether it is a composite or possibly prime number.AlgorithmsBegin    Declare a function modulo to the long datatype to perform binary calculation.       Declare m_base, m_exp, m_mod of long datatype and pass them as a parameter.       Declare two variables a, b of long datatype.          Initialize a = 1, b = m_base.       while (m_exp > 0) do          if (m_exp % 2 == 1) then             a = (a * b) % ... Read More

C++ Program to Generate Random Hexadecimal Bytes

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

1K+ Views

We shall discuss about a C++ program which can generate random Hexadecimal numbers. Here we shall use rand() and itoa() functions to implement the same. Let us discuss on these functions separately and categorically.rand(): rand() function is a predefined method of C++. It is declared in header file. rand() is used to generate random number within a range. Here min_n is the minimum range of the random numbers and max_n is the maximum range of the numbers. So rand() will return the random numbers between min_n to (max_n – 1) inclusive of the limit values. Here if we mention ... Read More

C++ Program to Generate Randomized Sequence of Given Range of Numbers

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

418 Views

At first let us discuss about the rand() function. rand() function is a predefined method of C++. It is declared in header file. rand() is used to generate random number within a range. Here min_n is the minimum range of the random numbers and max_n is the maximum range of the numbers. So rand() will return the random numbers between min_n to (max_n – 1) inclusive of the limit values. Here if we mention lower and upper limits as 1 and 100 respectively, then rand() will return values from 1 to (100 – 1). i.e. from 1 to 99.AlgorithmBegin ... Read More

Advertisements