Found 7346 Articles for C++

How to generate different random numbers in a loop in C++?

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

4K+ Views

Let us see how to generate different random numbers using C++. Here we are generating random numbers in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C++ library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below −void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudo-random number generator algorithm. This function returns nothing.To get the number ... Read More

Returning multiple values from a C++ function

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

9K+ Views

In C or C++, we cannot return multiple values from a function directly. In this section we will see how to use some trick to return more than one value from a function.We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.In this example we will see how to define a function that can return quotient and ... Read More

Checking if a double (or float) is NaN in C++

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

12K+ Views

To check whether a floating point or double number is NaN (Not a Number) in C++, we can use the isnan() function. The isnan() function is present into the cmath library. This function is introduced in C++ version 11. So From C++11 next, we can use this function.Example#include #include using namespace std; main() {    if(isnan(sqrt(30))) { //square root of 30 is a floating point number       cout

Benefits of inline functions in C++?

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

762 Views

C++ inline function is a powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality.To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore ... Read More

How do you get assembler output from C/C++ source in gcc?

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

2K+ Views

Here we will see how to generate assembler output from C or C++ code using gcc.The gcc provides a great feature to get all intermediate outputs from a source code while executing. To get the assembler output we can use the option ‘-S’ for the gcc. This option shows the output after compiling, but before sending to the assembler. The syntax of this command is like below.gcc –S program.cppNow, let us see how to output will be look like. Here we are using a simple program. In this program two numbers are stored into the variables x and y, then ... Read More

Order of evaluation in C++ function parameters

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

139 Views

We pass different arguments into some functions. Now one questions may come in our mind, that what the order of evaluation of the function parameters. Is it left to right or right to left?To check the evaluation order we will use a simple program. Here some parameters are passing. From the output, we can find how they are evaluated.Example Live Demo#include using namespace std; void test_function(int x, int y, int z) {    cout

RTTI (Run-time type Information) in C++ program

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this section, we will see what is the RTTI (Runtime Type Information) in C++. In C++ the RTTI is a mechanism, that exposes information about an object’s datatype during runtime. This feature can be available only when the class has at least one virtual function. It allows the type of an object to be determined when the program is executing.In the following example, the first code will not work. It will generate an error like “cannot dynamic_cast base_ptr (of type Base*) to type ‘class Derived*’ (Source type is not polymorphic)”. This error comes because there is no virtual function ... Read More

Function overloading and return type in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.The function overloading is basically the compile time polymorphism. It checks the function signature. If the signatures are not same, then they can be overloaded. The return type of a function does not create any effect on function overloading. Same function signature with different return type will not be overloaded.Following is the example where ... Read More

Copy elision in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

163 Views

The Copy Elision is also known as the Copy Omission. This is one of the compiler optimization technique. It avoids the unnecessary copying of objects. Almost any current compiler uses this Copy Elision technique.Let us see how it works by the help of one example code:Example Code#include using namespace std; class MyClass {    public:       MyClass(const char* str = "\0") {  //default constructor          cout

How to Parse Command Line Arguments in C++?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

422 Views

It is possible to pass some values from the command line to your C++ programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard-coding those values inside the code.The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from ... Read More

Advertisements