Found 7347 Articles for C++

Is there a difference between copy initialization and direct initialization in C++?

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

2K+ Views

The Copy initialization can be done using the concept of copy constructor. As we know that the constructors are used to initialize the objects. We can create our copy constructor to make a copy of some other object, or in other words, initialize current object with the value of another object. On the other hand, the direct initialization can be done using assignment operation.The main difference between these two types of initialization is that the copy initialization creates a separate memory block for the new object. But the direct initialization does not make new memory space. It uses reference variable ... Read More

What is the effect of extern “C” in C++?

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

2K+ Views

The extern “C” keyword is used to make a function name in C++ have the C linkage. In this case the compiler does not mangle the function. Let us see what is the mangling in C++ first, then we can discuss about the extern “C” keyword.In C++ we can use the function overloading feature. Using this feature, we can create functions with same name. The only difference is the type of the arguments, and the number of arguments. The return type is not considered here. Now the question comes how the C++ distinguishes overloaded functions in object code?In the object ... Read More

What is the difference between const int*, const int * const, and int const *?

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

415 Views

Here we will see some different types of variable declaration based on integer pointers integer constants and the integer constant pointers.To determine them we will use the Clockwise/Spiral Rule. By discussing the terms, we can understand the rules also.The const int *. This is used to tell the compiler that this is a pointer type variable, and this can store address of some constant int. The Clock rule is saying like this −Now the another one is const int * const. This is used to denote that this is one constant pointer variable, which can store the address of another ... Read More

What is the most effective way for float and double comparison in C/C++?

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

6K+ Views

Here we will see how to compare two floating point data or two double data using C or C++. The floating point / double comparison is not similar to the integer comparison.To compare two floating point or double values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.To compare using this criteria, we will find the absolute value after subtracting one floating point number from another, then check whether the result is ... Read More

What's the best way to trim std::string in C++?

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

8K+ Views

Here we will see how to trim the strings in C++. The trimming string means removing whitespaces from left and right part of the string.To trim the C++ string, we will use the boost string library. In that library, there are two different methods called trim_left() and trim_right(). To trim string completely, we can use both of them.Example#include #include using namespace std; main(){    string myStr = " This is a string ";    cout

What is a null-terminated string in C/C++?

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

9K+ Views

In C the strings are basically array of characters. In C++ the std::string is an advancement of that array. There are some additional features with the traditional character array. The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by ‘\0’). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.The size of the string may smaller than the array size, but if there are some null character inside that array, that will be treated as the end of that string.See ... Read More

The best way to hide a string in binary code in C++?

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

492 Views

Here we will see how to hide some string into some binary code (Here binary code is represented in hexadecimal number).The approach is very simple. We can use the string stream to convert decimal number to hexadecimal numbers. Now from the string, we will read each character, and take its ASCII value, these ASCII values are converted into hexadecimal values. Then we can print them one by one.Example#include #include using namespace std; string dec_to_hex(int decimal){ //function is used to convert decimal to hex    stringstream my_ss;    my_ss

Stringstream in C++

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

9K+ Views

Here we will see the string stream in C++. The string stream associates a string object with a string. Using this we can read from string as if it were a stream like cin.The Stringstream has different methods. These are like below −clear(): Used to clear the streamstr(): To get and set the string object whose content is present in streamoperator > : This is used to read from stringstream object.Let us see two examples of string streams. In the first program we will divide the words into separate strings.Example#include #include #include #include using namespace std; ... Read More

Reverse a string in C/C++ using Client Server model

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

1K+ Views

Here we will see how we can create a system, where we will create one client, and a server, and the client can send one string to the server, and the server will reverse the string, and return back to the client.Here we will use the concept of socket programming. To make the client server connection, we have to create port. The port number is one arbitrary number that can be used by the socket. We have to use the same port for client and the server to establish the connection.To start the program, start the server program first −gcc ... Read More

How to write your own header file in C?

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

14K+ Views

Here we will see how to create own header file using C. To make a header file, we have to create one file with a name, and extension should be (*.h). In that function there will be no main() function. In that file, we can put some variables, some functions etc.To use that header file, it should be present at the same directory, where the program is located. Now using #include we have to put the header file name. The name will be inside double quotes. Include syntax will be look like this.#include”header_file.h”Let us see one program to get the ... Read More

Advertisements