Found 7347 Articles for C++

How to determine whether C++ code has been compiled in 32 or 64 bit?

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

765 Views

In C++, there is no direct way to check the environment architecture. There are two Macros for Windows systems, that can be used to check the architecture. These macros are _WIN64, and _WIN32. When the system is 64-bit, then the _WIN64 will be 1, otherwise the _WIN32 will be 1. So using macro checking, we can identify the architectureExample#include using namespace std; int main() {    #ifdef _WIN64       cout

How to initialize const member variable in a C++ class?

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

14K+ Views

Here we will see how to initialize the const type member variable using constructor?To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.Example#include using namespace std; class MyClass{    private:       const int x;    public:       MyClass(int a) : x(a){       //constructor    }    void show_x(){       cout

How do I catch a Ctrl+C event in C++?

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

10K+ Views

The CTRL + C is used to send an interrupt to the current executing task. In this program, we will see how to catch the CTRL + C event using C++.The CTRL + C is one signal in C or C++. So we can catch by signal catching technique. For this signal, the code is SIGINT (Signal for Interrupt). Here the signal is caught by signal() function. Then one callback address is passed to call function after getting the signal.Please see the program to get the better idea.Example#include #include #include #include using namespace std; // Define ... Read More

How do I terminate a thread in C++11?

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

6K+ Views

Here we will see, how to terminate the threads in C++11. The C++11 does not have direct method to terminate the threads.The std::future can be used to the thread, and it should exit when value in future is available. If we want to send a signal to the thread, but does not send the actual value, we can pass void type object.To create one promise object, we have to follow this syntax −std::promise exitSignal;Now fetch the associated future object from this created promise object in main function −std::future futureObj = exitSignal.get_future();Now pass the main function while creating the thread, pass ... Read More

Using G++ to compile multiple .cpp and .h files

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

4K+ Views

To compile multiple files like file_name.h, or file_name.cpp at once, we can use the files like a list one after another. The syntax will be like this −g++ abc.h xyz.cppTo run the program, we can use this −./a.outExamplefloat area(float r){    return (3.1415*r*r); //area of a circle } float area(float l, float w){    return (l * w); //area of a rectangle }Example#include #include "area.h" using namespace std; main(){    cout

How to include libraries in Visual Studio 2012?

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

1K+ Views

To add libraries in Visual Studio 2012, there are two different methods. The first one is manual method. The second one is adding libraries from code.Let us see the manual method first.To add some library, we have to follow these five steps −Add the #include statements necessary files with proper declarations. For example −#include “library.h”Add the include directory for the compiler look up;Go to the Configuration Properties/VC++ Directories/Include DirectoriesThen click and edit, and add new entryAdd one library directory for *.lib files:Go to project (on top bar) -> properties -> Configuration Properties -> VC++ Directories -> Library Directories, then click ... Read More

Floating point comparison in C++

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

12K+ Views

Here we will see how to compare two floating point data using C++. The floating point comparison is not similar to the integer comparison.To compare two floating point 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 lesser than the precision value or not. By this we ... Read More

What exactly is nullptr in C++?

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

185 Views

In this section we will see the nullptr in C++. The nullptr denotes the pointer literals. It is a prvalue of type std::nullptr_t. It has implicit conversion property from nullptr to null pointer value of any pointer type and any pointer to member type. Let us see one program, to understand this concept.Example#include using namespace std; int my_func(int N){ //function with integer type parameter    cout

How do I convert between big-endian and little-endian values in C++?

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

6K+ Views

Here we will see how to convert Little endian value to Big endian or big endian value to little endian in C++. Before going to the actual discussion, we will see what is the big endian and the little endian?In different architectures, the multi-byte data can be stored in two different ways. Sometimes the higher order bytes are stored first, in that case these are known as big endian, and sometimes the lower order bytes are stored first, then it is called little endian.For example, if the number is 0x9876543210, then the big endian will be −The little endian will ... Read More

When should I write the keyword 'inline' for a function/method in C++?

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

134 Views

In C++, the inline keyword is used in different places. To create inline variables, or inline namespace, and as well as to create inline methods or functions.C++ inline function is 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 ... Read More

Advertisements