Programming dev-c++ end FireMonkey FMX ide learn learn

What Is A Copy Assignment Operator in C++?

Object-Oriented Programming (OOP) is a method of mapping real-world objects and data to computer functions and data structures. Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. One of the features of an OOP Editor is a copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain what a copy assignment operator is along with some C++ examples. What are classes and objects in C++? Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.   class myclass {           public:       std::string str; };   What is a copy assignment operator in C++? The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the “operator=“. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the typical declaration of a copy assignment operator which is defaulted, Syntax (Since C++11).   class_name & class_name :: operator= ( const class_name& ) = default;   Here is an example in a class.   Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator   Is there a simple example of using the copy assignment operator in C++? The forced copy assignment operator is default in any class declarations. This means you don’t need to declare it as above, let’s give examples without using it. Let’s give a simple C++ example to copy assignment operator with default option, here is a simple class.   class myclass {   public:   std::string str;   };   Because this is default in any class declaration, and it is automatically declared. This class is same as below.   class myclass {   public:   std::string str;     Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator };   And here is how you can use this “=” copy assignment operator with both class examples above.   Tmyclass o1, o2;   o2 = o1; // Copy Assingment Operator   Is there a full example of how to use the copy assignment operator in C++? An example with a copy assignment operator in a Class. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27   #include #include   class Tmyclass {   public:   std::string str;        // Tmyclass& operator=(const Tmyclass& other) = default; // Copy Assignment Operator };   int main() { Tmyclass o1, o2;   o1.str = “LearnCplusplus.org”;   o2 = o1; // Using Copy Assingment Operator   std::cout

Read More

Typical Declaration Of A Copy Assignment Operator With std::swap

Object-Oriented Programming (OOP) is a method of mapping real-world objects and data to computer functions and data structures. Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. One of the features of an OOP tool is the copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain the typical declaration of a copy assignment operator with C++ examples. What are classes and objects in C++? Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.   class myclass {           public:       std::string str; };   What is a copy assignment operator in C++? The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the “operator=“. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the typical declaration of a copy assignment operator when the swap is used. Syntax:   class_name & class_name :: operator= ( class_name )   Here is an example in a class.   class Tmyclass {   public:   Tmyclass& operator=(Tmyclass other) // Using Copy Assingment Operator   {    // Copy or swap things    return *this;   } };   What is a typical declaration of a copy assignment operator with std::swap? Let’s give a simple C++ example of a typical declaration of a copy assignment operator with std::swap.   class myclass {   public:   std::string str;     myclass& operator=(myclass other)   {        std::cout

Read More

Typical Declaration Of A Copy Assignment Operator Without std::swap

Object-Oriented Programming (OOP) is a method of mapping real-world objects and data to computer functions and data structures. Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. One of the features of an OOP IDE is the copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain what the typical declaration of copy assignment operator is along with some C++ examples. What are classes and objects in C++? Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.   class myclass {           public:       std::string str; };   What is copy assignment operator in C++? The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the “operator=“. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the typical declaration of a copy assignment operator when the copy and swap idiom is not used, Syntax.   class_name & class_name :: operator= ( const class_name& )   Here is an example in a class.   Tmyclass& operator=( const Tmyclass& other) // Using Copy Assingment Operator { if (this != &other) // not a self-assignment { // Copy or swap things }    return *this; }   What is typical declaration of a copy assignment operator without std::swap? Let’s give a simple C++ example of the typical declaration of a copy assignment operator with std::swap. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17   class Tmyclass {   public:   std::string str;     Tmyclass& operator=( const Tmyclass& other) // Using Copy Assingment Operator   { if (this != &other) // not a self-assignment { std::cout

Read More

What Is The Rule of Zero in C++?

Object Oriented Programming (OOP) is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). These attributes and methods are variables and functions that belong to the class – part of the class’s code and they are generally referred to as class members. Classes and structs are very useful in modern programming. There are some rules to support the principles of programming, one of which is the Rule of Zero in C++. In this post, we explain what is Rule of Zero in C++ with examples. What is resource acquisition in C++? The principle of Resource Acquisition Is Initialization (RAII) term used in several OOP programming languages, which relates to the ability to manage resources, such as memory, through the copy and move constructors, destruction, and assignment operators. RAII is about the declaration and use of destructors, copy-move operators, and memory management in these members and methods. These cause new rules in development. What is the Single Responsibility Principle in C++? The Single Responsibility Principle (SRP) is a computer programming principle that states “A module should be responsible to one, and only one, actor.” This principle exposes a rule for the classes in C++, called Rule of Zero. Now, let’s see what the Rule of Zero in C++ is. What is the Rule of Zero in C++? Rule of Zero means that, if all members have default member functions, no further work is needed. This is the simplest and cleanest semantics of programming. The compiler provides default implementations for all of the default member functions if there are no special member functions that are user-defined. In other words, you should prefer the case where no special member functions need to be defined. In this rule, classes that have custom destructors, copy/move constructors, or copy/move assignment operators should deal exclusively with ownership; other classes should not have custom destructors, copy/move constructors or copy/move assignment operators. Rule of Zero helps simplify our C++ code without risking resource management issues at run time. The term The Rule of Zero was coined by R. Martinho Fernandes in his paper in 2012. This paper is highly recommended reading to get the full details of this concept. The Rule of Zero rule can be seen in the C++ Core Guidelines here C.20: If you can avoid defining default operations, do. As in the example below, std::map and std::string have all the special functions, so you don’t need to create a constructor and destructor for them.   class Tmy_map {     public:       // … no default operations       private:       std::string title;       std::map<int, int> coords; };   Tmy_map m;       // default construct Tmy_map m2 {m};  // copy construct   How can we apply the Rule of Zero in C++? Here are some notes as to-dos in Rule of Zero. A simple empty C++ class is perfectly equivalent to default implementations (Rule of Five) in a class. A modern compiler is able to provide a default implementation, in example, this simple class. This class is exactly the same as the one below in modern C++.   class Tx { public:    Tx() = default;       Tx(Tx const& other) = default;    Tx& operator=(Tx const& other) = default;       Tx(Tx&& other) = default;    Tx& operator=(Tx&& other) = default;       ~Tx() = default; }   Use a std::unique_ptr if your class can be moved but should not […]

Read More

How To Use Gamepad Or Joystick Controller In A C++ Builder FMX App?

C++ is one of the most powerful programming languages that we can use to create games that are playable with mouse, keyboard and gamepads or joysticks. We also use C++ in commerce, industry, in robotics, and in the control of IoT devices. Despite competition from other programming languages the games market is still very much dominated by C++. Many games seek to go beyond keyboards to control the gameplay by using specialized controllers either in addition to or instead of keypresses on the computer’s keys. Two of the most well-known controllers in these areas are joysticks or gamepads. The simplest examples of interfacing with game controllers on Windows is by using the XInput library. Even though XInput has been around for a long time it can still be used with the latest C++ Editor and compiler for Windows. In this post, we explain how you can use gamepad or joystick controller in C++ using with Xinput library. Is there a component for using a gamepad or joystick in C++ on Windows? If you are looking for a component library, there are Delphi and C++ Builder-compatible components built on the XInput library. The Controller library is a Delphi and C++ Builder component that allows applications to receive input from the Xbox Controller. The main features of this library are: Uses Windows XInput API Available for Delphi and C++ Builder from versions 6 right up to the current modern version 11. Source code included in the registered version. Royalty-free distribution. You can download the trial version of the XInput library, or you can buy a professional edition from here. How can I control a gamepad or joystick in C++ with Xinput Library? XInput library is deprecated but is still a supported library by Microsoft. They recommend moving towards the GameInput library or Windows.Game.Input for Windows applications. If we look at these kinds of libraries (i.e GamePad.h library), like MS’ own DirectXTK, we can see that the toolkit allows one to define USING_XINPUT vs. USING_GAMEINPUT vs. USING_WINDOWS_GAMING_INPUT to pick which underlying library is used. If we compare  XInput compared to GameInput: XInput library is old and easy to implement Windows applications. XInput library is limited to 4 controllers XInput library has no uniform interface to other input (like mouse/keyboard) XInput library occurs with higher latency XInput library is not friendly with other controllers. i.e. no support for Xbox One Rumble Motors. Briefly, DirectInput is better than XInput. If you still want to use XInput, you can check more about https://learn.microsoft.com/en-us/windows/win32/xinput/getting-started-with-xinput XINPUT_GAMEPAD structure (declared in ) is explained here : https://learn.microsoft.com/en-us/windows/win32/api/xinput/ns-xinput-xinput_gamepad As in there, this structure has these members: wButtons, bLeftTrigger, bRightTrigger, sThumbLX, sThumbLY, sThumbRX, sThumbRY. Here wButtons member is used as a bitmask of the device digital buttons, it can be used as below, wButtons Device digital button flags Gamepad Bitmask XINPUT_GAMEPAD_DPAD_UP 0x0001 XINPUT_GAMEPAD_DPAD_DOWN 0x0002 XINPUT_GAMEPAD_DPAD_LEFT 0x0004 XINPUT_GAMEPAD_DPAD_RIGHT 0x0008 XINPUT_GAMEPAD_START 0x0010 XINPUT_GAMEPAD_BACK 0x0020 XINPUT_GAMEPAD_LEFT_THUMB 0x0040 XINPUT_GAMEPAD_RIGHT_THUMB 0x0080 XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100 XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200 XINPUT_GAMEPAD_A 0x1000 XINPUT_GAMEPAD_B 0x2000 XINPUT_GAMEPAD_X 0x4000 XINPUT_GAMEPAD_Y 0x8000 How to use a gamepad or joystick in a C++ Builder FMX application? Here are the steps to use gamepad or joystick in C++ Builder FMX app, Create a new Multi-Device C++ Builder FMX application Drag a Timer (TTimer) component on to Form In Object Inspector window, set its interval to 15 and be sure it is enabled Drag a shape Circle (TCircle) component on to Form In Object […]

Read More

How To Change Background Color Of A Component In An FMX C++ App

C++ Builder is the easiest and fastest C++ IDE for building professional applications with powerful GUI components like Memo, Edit, ListBox, StringGrid and many more. Each component can be ‘skinned’ with Styles to change their visual appearance. Styles are very powerful and because of this it can sometimes take a little to get used to how they work. In this post, we explain how to change background color of a component. Here is how you can do, How can I change background color of a component using styles in an FMX app? Syles are sets of graphical details that define the look and feel of an application visually and they are one of most beautiful and useful UI features of RAD Studio. Styles make your UI elements have a professionally designed appearance with minimum effort from you. Official Styles are designed by Embarcadero’s professional graphic designers. There are also other 3rd party Styles; also, users may generate their own styles. Styles are similar to themes in Windows or skins of old-style applications like WinAmp – but brought right up to date for the modern world. In RAD Studio, Multi-Device C++ Builder FireMonkey Projects, using styles on your new projects is very easy. You design your application’s screens in normal ways with buttons, labels, edit boxes, memos, trackbars, panels, switches etc. These controls will automatically adopt whatever appearance is enabled by the chosen style. You can also add these Styles to your Old C++ Builder Projects or to Old C++ Builder Unit files. You just need a RAD Studio, C++ Builder version which supports styles. We highly recommend you use the latest RAD Studio or C++ Builder versions which have new features and improvements on Styles. Here is how you can use Styles. In addition, you can create your custom styles in RAD Studio and C++ Builder. Here are two examples about Memo (TMemo) and Edit (TEdit) components. How to change background color of an FMX component in C++ code? Here is a simple function example to change background color of a component in C++, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 void SetBackgroundColor(TStyledControl *control, TAlphaColor color) {   control->NeedStyleLookup();   control->ApplyStyleLookup();   TFmxObject *fmxobj = control->FindStyleResource(“background”);   TRectangle *rect = new TRectangle(fmxobj);   fmxobj->AddObject(rect);   rect->Align = TAlignLayout::Client;   rect->Fill->Color = color;   rect->Stroke->Color = claNull;   rect->HitTest = false;   rect->SendToBack(); } Is there a full C++ example of how to change the background color of a component in FMX and C++? In this example there are Memo, Edit, ComboBox, ListBox components and a Button component. You need to create a new C++ Builder Multi-Device application and then you should drag these components on your form. You can choose your colors from here https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Colors_in_FireMonkey If you want to know more about colors in Modern C++ here are some examples of how to use RGB or ARGB colors in C++. After that, you can modify your code as below. Here is a full C++ example of to change the background color of a component in an FMX C++ application. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include #pragma hdrstop #include “Component_Background_Color_Unit1.h” //————————————————————————— #pragma package(smart_init) #pragma resource “*.fmx” TForm1 […]

Read More

How To Change Background Color Of An Edit In An FMX C++ App

C++ Builder is the easiest and fastest C++ IDE for building everything from simple to professional applications using visually amazing GUI controls and forms. Each form and component can be skinned with Styles which allow your application to have its own professionally designed attractive look and feel. The Style systems of the VCL and FMX (FireMonkey) award-winning frameworks is very easy to use but there is sometimes a learning curve to climb to understand how to get the best out of them. In this post, we explain how to change background color of an Edit (TEdit) which is one of the most frequently asked questions. These methods below, can be applied to other components too. How to change the background color of an Edit control using Styles? Styles are sets of graphical details that define the look and feel of an application visually. They are one of most beautiful and useful UI features of RAD Studio, that makes your UI elements ‘skinned’ with a professionally designed graphical look and feel. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps (not wrapped in some kind of runtime interpretation layer) and the powerful FireMonkey (FMX) framework for cross-platform UIs. Both VCL and FMX apps support Styles. There are many different styles available, and they cater for almost every possible taste in aesthetics as well as providing dark and light mode versions. More details about Styles can be found here. You can also find additional Premium Styles here. If you want to learn how you can modernize your components with styles here is the post you need; Right-click on the Edit and select ‘Edit Custom Style…’ Expand Edit1Style: you’ll see the background node. Click on background to select it. Then add a TRectangle via the Palette. The IDE should expand the background node and show a new Rectangle1Style tied to the TRectangle, Via the Object Inspector change the Color of TRectangle Click on the Apply Style button in the ‘Style Designer’. Compile and run your application If you still have problems to set color, may be this docwiki can help you : https://blogs.embarcadero.com/edit-custom-style-to-change-the-background-color-of-a-fmx-tedit/ How to change background color of an Edit in C++ code? If you want to change the background color of a Edit component (TEdit) in C++ Builder, first you should set the StyleLookup property to “Editstyle” as below.   Edit1->StyleLookup = “Editstyle”;   If you look at the Custom Style of Edit, there is “background” property, so we should find this resource by using the FindStyleResource() method of Edit as given example below.   Edit1->StyleLookup = “Editstyle”; auto fmxobj = Edit1->FindStyleResource(“background”, false);   if this resource object found, we can create a rectangle (TRectangle) as a background as below.   std::unique_ptr rect(new TRectangle(fmxobj));   Here we used unique_ptr which more modern to create this rectangle. Now we can set properties of our Rectangle, including its color.   rect->Align = TAlignLayout::Client; rect->Fill->Color = color; rect->Stroke->Color = color; // = claNull; rect->HitTest = false; rect->SendToBack();   Now, at last we need to add this object by using .get() method of unique_ptr. And the final trick here is you must release this unique_ptr by using .release() otherwise it doesn’t have any effect.   fmxobj->AddObject(rect.get()); rect.release();   I asked one of Embarcadero’s engineers to explain this a little more to me and he noted that “If you insist on using unique_ptr, you must add a call to .release(); to tell the unique_ptr that you no longer want it to clean up […]

Read More

How To Use Gamepad Or Joystick Controller In A C++ Builder VCL App?

C++ is one of the most powerful programming languages that we can use to create games that are playable with mouse, keyboard and gamepads or joysticks. We use C++ not just in games but also in almost every kind of industry, in robotics, and in the control of IoT devices. Games frequently have better playability if the user can control the game with input devices other than keyboard. The most popular controllers for games are gamepads and joysticks. One of the simplest ways to use such game controller hardware on Windows is using the venerable XInput library. Despite XInput having been around for a long time it can still be used with the latest C++ IDE and compiler for Windows. In this post, we explain how you can use gamepad or joystick controller in C++ using with Xinput library. Is there a ready-made component for controlling a gamepad or joystick in C++ on Windows? If you are looking for a component library, there are Delphi and C++ Builder-compatible components built around the XInput library. The Controller library is a Delphi and C++ Builder component that allows applications to receive input from the Xbox Controller. The main features of this library are, Uses Windows XInput API. Available for Delphi/C++ Builder 6 – 11 and Lazarus 2.0.12 Source code included in the registered version. Royalty free distribution. You can download the trial version of the XInput library, or you can buy a professional edition from here. How can I control a gamepad or joystick in C++ with the Xinput Library? XInput library was developed quite some time ago and is a deprecated but still supported library by Microsoft. They recommend moving towards the GameInput library or Windows.Game.Input for Windows applications. If we look at these kinds of libraries (i.e GamePad.h library), like MS’ own DirectXTK, we can see that the toolkit allows one to define USING_XINPUT vs. USING_GAMEINPUT vs. USING_WINDOWS_GAMING_INPUT to pick which underlying library is used. If we compare  XInput compared to GameInput: XInput library is easy to implement Windows applications. XInput library is limited to 4 controllers. XInput library has no uniform interface to other input (like mouse/keyboard). XInput library occurs with higher latency. XInput library is not friendly with other controllers. i.e. no support for Xbox One Rumble Motors. In brief, DirectInput is better than XInput. If you still want to use XInput, you can check more about https://learn.microsoft.com/en-us/windows/win32/xinput/getting-started-with-xinput XINPUT_GAMEPAD structure (declared in ) is explained here: https://learn.microsoft.com/en-us/windows/win32/api/xinput/ns-xinput-xinput_gamepad In there, this structure has these members: wButtons, bLeftTrigger, bRightTrigger, sThumbLX, sThumbLY, sThumbRX, sThumbRY. Here wButtons member is used as a bitmask of the device digital buttons, it can be used as below. wButtons Device digital button flags Gamepad Bitmask XINPUT_GAMEPAD_DPAD_UP 0x0001 XINPUT_GAMEPAD_DPAD_DOWN 0x0002 XINPUT_GAMEPAD_DPAD_LEFT 0x0004 XINPUT_GAMEPAD_DPAD_RIGHT 0x0008 XINPUT_GAMEPAD_START 0x0010 XINPUT_GAMEPAD_BACK 0x0020 XINPUT_GAMEPAD_LEFT_THUMB 0x0040 XINPUT_GAMEPAD_RIGHT_THUMB 0x0080 XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100 XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200 XINPUT_GAMEPAD_A 0x1000 XINPUT_GAMEPAD_B 0x2000 XINPUT_GAMEPAD_X 0x4000 XINPUT_GAMEPAD_Y 0x8000 How to use a gamepad or joystick in C++ Builder VCL Application? Here are the step to use gamepad or joystick in C++ Builder VCL app, Create a new VCL Windows application in C++ Builder. Drag a Timer (TTimer) component onto the Form. In the Object Inspector window, set its interval to 15 and be sure it is enabled. Drag a Shape (TShape) component on to Form. In Object Inspector window, set its Shape property to stCircle, change Brush color something that makes you happy. Double click to […]

Read More

Advantages and Disadvantages of The C++ Programming Language

In the last 40 years C++ has earned, and maintained, a reputation as one of the most efficient programming languages. C++ is still hugely popular, often in the top 3 of any lists of popular programming languages and is used widely across a wide array of operating systems. Choosing the right programming language and IDE (we call them C++ tools) are very important in the early stages of learning to program. Generally, new developers want to know the advantages and disadvantages of the C++ programming language, and in this post, we try to explain its pros and cons for you. What are the pros and cons of C++ programming language? What are the advantages of C++? 1. C++ is compiler-based with very fast execution times C++ is a compiler-based programming language and that helps make it the fastest and one of the most powerful programming languages. This is one of the reasons to use hardware and software algorithms efficiently. In general, there are two types of programming languages: Interpreted and Non-Interpreted (Compiled). All computers work with something called machine code (code that can be directly executed by the computer’s CPU) that tells the computer what to do. This is the most native and fastest code, but it requires writing many lines for even quite simple things and is hard to generalize for all kinds of machines. It’s also not very easy to understand for humans. A compiler (C or C++ Compiler, etc.) is a computer program that converts a program written in a ‘high level’ programming language such as C++ code into executable machine code. The high-level language looks more like English and is much easier to understand and less complicated. 2. C++ is a structured and object oriented programming language C++ allows developers to use C language which is a structured programming language and it also allows Object Oriented Programming. Object Oriented Programming (OOP) is a way to integrate with objects which can contain data in the form of attributes or properties of objects, and code blocks in the form of methods, and functions of objects. These attributes and methods that belong to the class are generally referred to as class members. Object-Oriented Programming is a good way to work on data and work with functions in memory. Classes and Objects are the best way to work on properties and functions. Object-Oriented Programming has many advantages over procedural programming and it is the most characteristic feature of the visual and modern C++ programming language. 3. C++ supports visual 2D, 3D and GUI-based programming C++ is useful to make GUI-based applications on Windows, iOS, Android, Mac OS, Linux, and many other less popular OSes. C++ has different GUI frameworks i.e. VCL, FMX, GTK, and other Visual components. Modern C++ has been enhanced with Visual Bindings, High-DPI 4K supports, responsive features, skinned UI elements, glamorous one-click GUI sets, 2D and 3D components like OpenGL, Skia, and Viewport3D; and has many game engines like Unreal Engine, Unity, O3DE. C++ is also useful to develop graphical 2D and 3D applications, real-time computational simulations, and analysis, C++ highly uses CPU and GPU-based applications with graphical supports like Physics, OpenCL, Vulkan, HPC, etc. 4. C++ is a mid-level programming language with low-level capabilities Because of its capability in assembly-level coding and compiling abilities, I prefer calling C a ‘close to low-level language’. Briefly, we may say C and C++ languages are close […]

Read More

How To Use Base-8 (Octal) Numbers In Modern C++?

In general, in nearly all programming languages, including Modern C++, we use base-10 numbers, while our computer hardware uses the binary base-2 system (bits). I personally believe that base-10 is because we typically have 10 fingers and since we learn to counting on our fingers we use base-10 primarily. However, due to the way binary works it is sometimes convenient to use base-8 system octal (oct) and base-16 system hexadecimal (hex) numbers in programming. In this post, we explain how to use Base-8 octal numbers in Modern C++. What is the octal number system? The octal – or oct – number system is the base-8 system, Octal uses the digits from 0 to 7. In general, we use a base-10 number system in normal life. In the digital world we also use base-2, base-8, and base-16 as well as base-10. If you want to explore the different numbering systems, try this Wikipedia article. Let’s give some octal examples and their values in the decimal system, 1octal  = 1 10octal  = 8 100octal = 64 How to declare base-8 (octal) number in modern C++? In C++, when we use octal numbers in C++ code, we put 0 on the left side of an octal number. This zero shows it is an octal representation. int x; // 015 in octal is 13 base10 or decimal x = 015; How to initialize base-8 (octal) number in modern C++? here the first zero on the left side of 0010 defines this number representation is an octal representation. The integer x value is results as 8; How to set an integer to base-8 (octal) number in modern C++? We can use std::istringstream() which is declared in library. Here Octal number “010” is declared into x as a octal number, now when you print this variable, it will be displayed in octal decimal system,    int x;    std::istringstream(“010”) >> std::oct >> x;    std::cout << x << std::endl; How to print out base-8 (octal) number in modern C++? When we print out base-8 (octal) numbers we use std::oct format as below, int x = 0010; std::cout << std::oct << x << std::endl; How to print out base-8 (octal) number in C language? C has very powerful printf() function and there is a ‘%o‘ Format Specifier to print out octal numbers. How to display base-8 (octal) number in C++ Builder? C++ Builder is using UnicodeString in most of components, and UnicodeString very powerful printf() method. There is a ‘%o‘ Format Specifier to print out octal numbers that can be used as below, int i = 64; // decimal UnicodeString ustr; ustr.printf(“%o n”, i); Edit1->Text = ustr; How to convert base-8 (octal) number to (base-10) decimal number in modern C++? When you declare an integer with octal representation, it is automatically converted to base-10 number. For example: here a = 8 in the decimal system. there is another method std::stoi that converts octal representation in string to an integer. Here is an example, int x = std::stoi(“011”, 0, 8); std::cout << x << std::endl; How to convert (base-10) decimal number to base-8 (octal) number in modern C++? In C++, computation is done in base-2 while you have a decimal number. If you want to convert base-10 decimal number to base-8 octal number, you only need to convert to a octal number or string when you […]

Read More