Found 7347 Articles for C++

How do I generate random floats in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

In C or C++, we cannot create random float directly. We can create random floats using some trick. We will create two random integer values, then divide them to get random float value.Sometimes it may generate an integer quotient, so to reduce the probability of that, we are multiplying the result with some floating point constant like 0.5.Example#include #include #include using namespace std; main() {    srand((unsigned int)time(NULL));    float a = 5.0;    for (int i=0;i

How to automatically generate a stacktrace when a gcc C++ program crashes?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

For Linux and we can use gcc to compile C/C++ codes. This compiler uses glibc library. We can use the backtrace() function to trace the error. This function is present inside the execinfo.h header file. In this example, we are going to display Segmentation fault error using the stack trace feature.Example#include #include #include #include #include using namespace std; void error_handler(int sig) {    void *array[10];    size_t size;    size = backtrace(array, 10); //get the void pointers for all of the entries    cout

How to execute a command and get output of command within C++ using POSIX?

Samual Sam
Updated on 30-Jul-2019 22:30:26

655 Views

Here we will see how to use the POSIX command through C++. The process is very simple, we have to use the function called system(). Inside this we have to pass string. That string will contain the POSIX command. The syntax is like below.system(“command”)Example#include using namespace std; int main () {    cout

What is the size of int, long type as per C++ standard?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

368 Views

Here we will see what are the sizes of the int and long type data in C++. The sizes are depending on the system architecture and the Operating system.So in the 32-bit system, the standard is ILP32. In this standard the int, long and the pointer variables are of 32-bits.For the 64-bit system there are two variations. For Linux Operating system the standard is LP64. Here long and pointer are of 64-bits, but int are of 32-bits. For the Windows operating system, the standard is LLP64. Here long long is 64-bit, but int and long are of 32-bits.Example#include using ... Read More

When are Constructors Called in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

550 Views

Here we will see, when constructors are called. Here constructors are of different types. Global, local, static local, dynamic.For the global object, the constructors are called before entering into the main function.Example#include using namespace std; class MyClass {    public:       MyClass() {          cout

Rule Of Three in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

208 Views

The Rule of three is a rule of thumb when using C++. This is kind of a good practice rule that says that If your class needs any ofa copy constructor, an assignment operator, or a destructor, defined explicitly, then it is likely to need all three of them.Why is this? It’s because, if your class requires any of the above, it is managing dynamically allocated resources and would likely be needing the other to successfully achieve that. For example, if you require an assignment operator, you would be creating copies of objects currently being copied by reference, hence allocating ... Read More

What happen when we exceed valid range of built-in data types in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

358 Views

Here we will see what will be the results, if we exceed the range of built-in datatypes in C++. So let us see some examples.First one is the character type data. Here we are using a loop from 0 to 300, so it should print from 0 to 300, then stop. But it will generate one infinite loop. The character type data holds from -128 to 127. So after increasing from 127, it will be -128 again. So it will never reach at the point 300.Example#include using namespace std; int main() {    for (char x = 0; x ... Read More

Difference between set, multiset, unordered_set, unordered_multiset in C++

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

245 Views

Here we will see what are the differences for set, multiset, unordered_set and unordered_multiset in C++. Let us see the properties of them using some example.SetThe properties of set are like belowStores data in sorted orderStores only unique valuesWe can insert or delete data, but cannot change the dataWe can remove more than one element using start and end iteratorWe can traverse using iteratorsSets are implemented using Binary Search TreeNow let us see an exampleExample#include #include using namespace std; main() {    int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41};    set my_set;    for(int i = 0; i

Factorial of Large Number Using boost multiprecision Library

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

112 Views

To find the factorial of a large number, we can use the boost library. This library provides high precision numbers. Using boost multiprecision library we can get more precision than 64 bits.Example#include #include using boost::multiprecision::cpp_int; using namespace std; cpp_int Large_Fact(int number) {    cpp_int fact = 1;    for (int i = 1; i > fact >> endl; }Output9332621544394415268169923885626670049071596826438162146859296389521759999322 9915608941463976156518286253697920827223758251185210916864000000000000000000 000000

override Keyword in C++

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

4K+ Views

The function overriding is the most common feature of C++. Basically function overriding means redefine a function which is present in the base class, also be defined in the derived class. So the function signatures are the same but the behavior will be different.But there may be a situation when a programmer makes a mistake while overriding that function. Like if the signature is not the same, then that will be treated as another function, but not the overridden method or that. In that case, we can use the override keyword. This keyword is introduced in C+ +11. When the ... Read More

Advertisements