Noutați

Learn Beneficial Methods Of Modern C++

Hello everyone, Yilmaz here, from LearnCPlusPlus.org. Our educational LearnCPlusPlus.org web page is growing thanks to the support of you. We have many new readers, and we keep adding new C++ posts every day. These are good to learn the features of modern C++ compilers. In this round-up of recent articles, we explain some features of modern C++ along with other beneficial methods for intermediate and professional developers. This week, we have C++ examples and an explanation about using non-copyable movable types in C++ templates. The rvalue references are important and we explain rvalue references to eliminate unnecessary copying in C++. C++11, C++17, and C++20 standards added to the richness of the language, and we explain one of these additions – explicit conversion operators. If you wonder what an explicit specifier is, we explain them in another post. C++11 introduced two new features: defaulted and deleted functions, we can use these two keywords to delete or to default methods in C++, and we explain this in the other two posts. Table of Contents Where can I learn C++ with a free C++ compiler? How to use modern C++ with C++ Builder? How to learn C++ for free using C++ Builder? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or a professional version of C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use modern C++ with C++ Builder? In C++, memory and CPU/GPU management are very important. Every declaration and usage of any items can cause a lot of heavy calculations, memory usage, and high CPU/GPU usage if used or manipulated unwisely. Using copy and move types in templates is very important when you develop a professional app. In the first post, we explain how you can use non-copyable movable types in C++ templates. https://learncplusplus.org/learn-how-to-use-non-copyable-movable-types-in-c-templates/ The rvalue references are a compound type like standard C++ references, which are referred to as lvalue references. New rvalue reference rules were set by the C++11 specifications. In many cases, data is copied that simply needs to be moved, that is, the original data holder need not retain the data. The rvalue reference can be used to distinguish cases that require copying versus cases that merely require moving data. In the next post, we explain how we use rvalue references to eliminate unnecessary copying in C++. https://learncplusplus.org/learn-how-to-eliminate-unnecessary-copying-in-c/ In modern C++, explicit-qualified conversion functions work in the same context as explicit-qualified constructors and produce diagnostics in the same contexts as constructors do. C++11, C++17, and C++20 standards have improvements in this explicit specifier. This is done to avoid situations when the compiler uncomplainingly accepts code […]

Read More

What Is A Virtual Function Specifier In Modern C++?

In modern C++ software, a virtual function is a function in a base class or struct that can be redefined in derived classes or structs. They are member functions whose behavior can be overridden in derived classes. The virtual function specifier is the ‘virtual’ specifier to declare a virtual function in a base class. In this post, we explain how we can use virtual function specifiers in modern C++. What is a virtual function specifier in modern C++? A virtual function is a function in a base class or struct that can be redefined in derived classes or structs. They are member functions whose behavior can be overridden in derived classes. The virtual function specifier is the ‘virtual‘ specifier to declare a virtual function in a base class. It is used by declaring the function prototype in the usual way and then prefixing the declaration with the virtual keyword. Here is the syntax of a virtual function:   virtual function_declaration;   To declare a pure function (which automatically declares an abstract class), prefix the prototype with the virtual keyword, and set the function equal to zero. Here is an example to virtual function and pure virtual function in C++:   virtual int funct1(void);       // A virtual function declaration.   virtual int funct2(void) = 0;   // A pure function declaration.   How to use virtual function specifier in modern C++? When you declare virtual functions, keep these guidelines in mind: They can be member functions only. They can be declared a friend of another class. They cannot be a static member. A virtual function does not need to be redefined in a derived class. You can supply one definition in the base class so that all calls will access the base function. To redefine a virtual function in any derived class, the number and type of arguments must be the same in the base class declaration and in the derived class declaration. The case for redefined virtual functions differing only in return type is discussed below. A redefined function is said to override the base class function. You can also declare the functions int Base::Fun(int) and int Derived::Fun(int) even when they are not virtual. In such a case, int Derived::Fun(int) is said to hide any other versions of Fun(int) that exist in any base classes. In addition, if class Derived defines other versions of Fun(), (that is, versions of Fun() with different signatures) such versions are said to be overloaded versions of Fun(). Is there a simple example of a virtual function in modern C++? Here is a simple example how you can use virtual function specifier in modern C++. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17   class Tx { virtual void myf() { std::cout

Read More

What Is An Explicit Virtual Override in Modern C++?

Modern C++ has many additions compared to the original C++ standard. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords, the final and the override specifiers. The override specifier is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. This override specifier is used with the C++ compiler that has C++11 and the other higher C++ standards. In this post, we explain an override specifier in modern C++. What Is the override specifier in C++? The override specifier (keyword) is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. In other words, it specifies that a method overrides a virtual method declared in one of its parent classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords: Final specifies that a method cannot be overridden, or a class cannot be derived. Override specifies that a method overrides a virtual method declared in one of its parent classes. The override specifier generally has two purposes, It shows that a given virtual method is overriding a virtual method of the base class. It indicates to the compiler that you are not adding or altering new methods that you think are overrides, and the compiler knows that is an override. In this post, we explain how to use the override specifier in C++. How to use the override specifier in C++? The override specifier is used to designate member functions that override a virtual function in a base class.   function_declaration override;   and, here is an example:   class Tbase { virtual void a(); };   class Tx : Tbase { void a() override; };   Is there a simple example to explicit virtual override specifier in C++ ? Here is a simple class example about override and final specifiers that you can override a method of it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   class Tbase { virtual void a(); void b(); virtual void c() final; virtual void d(); };   class Tx : Tbase { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int d() override; // error, different return type };   int main() {     class Tx o1; }   Here is a simple struct example about override and final specifiers that you can override a method of it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   struct st_base { virtual void a(); void b(); virtual void c() final; virtual void d(); };   struct st_x : st_base { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int d() override; // error, […]

Read More

What is a range-based for loop in modern C++?

In C++, the for loops are one of the great features of C and C++ language that has many options to break, continue, or iterate in a loop. In modern C++, there is a range-based for loop that makes it simple to iterate through a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.). The range-based for loop is a feature for the for() loops introduced by the C++11 standard and in this post, we explain what is range-based for loop in examples. If you are new to programming and looking for a classic for() loop here is the post about it. What is range-based for loop in modern C++? In C++, for() function is used for loops, and they are one of the great features of C and C++ language and have many options to break or continue or iterate blocks of functionality. In modern C++, there is a range-based for loop that makes it simple to iterate trough a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.). The range-based for loop is a feature for the for() loops introduced by the C++11 standard. Range-based for loop is a feature for the for() loops introduced by the C++11 standard. In Clang-enhanced C++ compilers, you can create for loops that iterate through a list or an array without computing the beginning, the end, or using an iterator. Here is the syntax for the range-based for loop in C++:   attr (optional) for ( init_statement (optional) range_declaration : range_expr) loop_statement;   or with loop body:   attr (optional) for ( init_statement (optional) range_declaration : range_expr) {    // loop_statements }   Is there a simple array example with a range-based for loop in C++? We can use range-based for loop to iterate through an array as below.   #include   int main() { int arr[] = { 00, 10, 20, 30, 40, 50 };   for (int a : arr) std::cout

Read More

Learn How To Use The Override Specifier In Modern C++

Modern C++ has many additions compared to the original C++ standard. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords, the final and the override specifiers. The override specifier is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. This override specifier is used with a C++ compiler that is compatible with C++11 and the other higher C++ standards. In this post, we explain an override specifier in modern C++. What Is the override specifier in C++? The override specifier (keyword) is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. In other words, it specifies that a method overrides a virtual method declared in one of its parent classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords: final specifies that a method cannot be overridden, or a class cannot be derived. override specifies that a method overrides a virtual method declared in one of its parent classes. The override specifier generally has two purposes, It shows that a given virtual method is overriding a virtual method of the base class. It indicates to the compiler that you are not adding or altering new methods that you think are overrides, and the compiler knows that is an override. In this post, we explain how to use the override specifier in C++. How to use the override specifier in C++? The override specifier is used to designate member functions that override a virtual function in a base class.   function_declaration override;   and here is an example:   class Tbase { virtual void a(); };   class Tx : Tbase { void a() override; };   Is there a simple example of how to use the override specifier in C++? Here is a simple class example about override and final specifiers that you can override a method of it, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   class Tbase { virtual void a(); void b(); virtual void c() final; virtual void d(); };   class Tx : Tbase { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int d() override; // error, different return type };   int main() {     class Tx o1; }   Here is a simple struct example about override and final specifiers that you can override a method of it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   struct st_base { virtual void a(); void b(); virtual void c() final; virtual void d(); };   struct st_x : st_base { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int […]

Read More

How To Use A Move Constructor In Modern C++

Hello everyone. With the C++11 standards, the move constructor allows you to move the resources from one object to another object without copying them. In this post we have gathered together some recent C++ articles from LearnCPlusPlus.org about Move Constructors. All of the C++ examples in these posts can be used with C++ Builder Enterprise, Architect, Professional Editions, or the free version C++ Builder 11 CE Community Edition. Our standard C++ examples can also be used with Dev-C++, BCC C++ Compilers, and some other compilers such as the GCC compiler. If you just starting out on your C++ journey and want to jump to a modern IDE and C++ compiler, there is a free version of C++ Builder, C++ Builder 11 CE Community Edition released in April 2023. If you are a start-up developer, student, hobbyist, or just interested in learning to code then the C++ Builder Community Edition may well be just the thing for you. Table of Contents Where can I learn to code in C++ with a free C++ compiler? How to use a move constructor in C++ with C++ Builder CE? How to learn C++ for free with C++ Builder CE? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn to code in C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or the professional, full-featured C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use a move constructor in C++ with C++ Builder CE? The Move Constructor is one of the great features of Object Oriented Programming in C++. The move constructor allows you to move the resources from one object to another object without copying them. One of the move constructors is the Implicitly-defined Move Constructor which is defined or defaulted in a base class, and in the first post, we explain the Implicitly-defined Move Constructor in Modern C++. https://learncplusplus.org/what-is-an-implicitly-defined-move-constructor-in-modern-c/ Another move constructor is the Implicitly-declared Move Constructor, which is declared in a base class. In the next post we explain the implicitly-declared move constructor in Modern C++. https://learncplusplus.org/what-is-an-implicitly-declared-move-constructor-in-modern-c/ There is also an Eligible Move Constructor. In the next post, we explain an eligible move constructor in modern C++. https://learncplusplus.org/what-is-an-eligible-move-constructor-in-modern-c/ The other move constructor term is the Trivial Move Constructor which is defined or defaulted in a base class. We explain what a trivial move constructor in C++ is and how to use it. https://learncplusplus.org/what-is-a-trivial-move-constructor-in-modern-c/ Finally, the other move constructor is the Deleted Implicitly-declared Move Constructor (also it is shown in compiler errors as Implicitly-deleted Move Constructor) which is deleted in a base class directly or has been deleted because of some other declarations. In the last post, […]

Read More

Let’s Learn 5 Features Of Modern C++

Hello C++ Developers, I hope now you are enjoying your summer vacation, or you are happy with your work ???? Over on LearnCPLusPlus.org we add new C++ posts every day. These are good to learn the features of modern C++ compilers. In this round-up of recent articles we cover some important features of C++ such as the friend declaration, the inline namespace, the extended sizeof function, the assignment operator, and the rules of C++ such as the Rule of Zero, the Rule of Three, the Rule of Five, and the Rule of Six. Some of these topics may not be necessary for beginners but they are important to understand as you progress on your coding journey. Table of Contents Where can I learn C++ with a free C++ compiler? How to use a move constructor and copy assignment in C++ with C++ Builder CE? How to learn C++ for free using C++ Builder CE ? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or a professional C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use a move constructor and copy assignment in C++ with C++ Builder CE? In C++ the standards have a wonderfully named extended friend declaration feature to address the class declarations which is a template parameter as a friend. This extended friend declaration is available with the C++ compiler that has C++11 and above standards. In the first post, we explain a friend and extended friend declaration in modern C++. https://learncplusplus.org/what-is-an-extended-friend-declaration-in-modern-c/ Namespaces are a kind of library or framework in C++. They are useful to hold the same name of classes, methods, and other entities in different namespaces. The C++11 standard and subsequent standards, allow the inline keyword in a namespace definition and in the next post, we explain how to use inline namespaces in modern C++. https://learncplusplus.org/what-is-the-inline-namespace-feature-in-modern-c/ Knowing the physical size of any data is very important in programming. While the programmers are trying to minimize data types or stream packages, the actual amount of global data transfer is increasing thanks to the demands of new technologies. To help combat this trend, we need to know each data type in programming. The sizeof() function is very useful to get the size of variables in bytes. C++11 extends the functionality of sizeof function so that class or class members can be sent as parameters even if no object has been instantiated. In another post, we explain with examples, what is the sizeof function, what is the extended sizeof() function, and how does C++11 extend the sizeof() operator on classes? https://learncplusplus.org/what-is-an-extended-sizeof-in-modern-c/ One of […]

Read More

Five Simple Examples Of C++ FMX Applications

The C++ Builder CE Community Edition is a free version of professional C++ Builder that you can develop GUI based desktop and mobile applications in C++. In this post, we will give you five simple C++ FMX applications as examples that you can compile with C++ Builder 11 CE. The latest C++ Builder 11 CE was released in April 2023. If you are a start-up developer, student, hobbyist or just interested in learning to code then C++ Builder Community Edition may well be just the thing for you. Read the FAQ notes on the CE license and then simply fill out the form and download C++ Builder 11 CE. If you download C++ Builder Community Edition (or RAD Studio CE version) or any Professional, Architect, Enterprise versions of C++ Builder. Install it on your windows computer and run RAD Studio or C++ Builder. Beginners and students normally start to learn C++ with simple code. Let’s create a new Multi-Device C++ application for Windows by using FMX framework in C++ Builder CE. How to develop C++ FMX applications in C++Builder CE? If you download and install the C++ Builder CE Community Edition then we can start to coding, RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. VCL applications focus only Windows, if you want to develop same app for multiple-OS’es you can use FMX. FMX and VCL mostly has similar components and component properties and methods, there are some small changes in different components. There are also significant changes in graphics to support multi-OS devices. Personally, I found that FMX has more options in graphics and much more powerful than VCL in graphical operations. Let’s start to develop a C++ app with GUI using FMX framework. 1. Choose File->New-> “Multi-Device Application – C++ Builder” menu. This will create a New C++ Project for Windows. This will allow you develop C++ apps with FMX UI elements. If you don’t need UI Elements, this means you don’t need VCL or FMX frameworks, you create a console application too. Modern applications have a GUI’s and skinned Styles. Note that VCL projects are Windows only and FireMonkey projects are Multi Device (multi-platform) applications that you can compile and run on Windows, MacOS, iOS and Android. 2. Save all Unit files and Project file to a folder. 3. Drag components to your form design Simply drag and drop components from the Palette window on the right side; Memo (TMemo) and Button (TButton) to your form design. Arrange their width, height and position. You can edit each of their properties from the Object Inspector on the left side. Note that you can switch between the GUI Design mode to Code Mode by pressing F12, or vice versa. If you want, you can switch to your header file (.h) of your cpp file (.cpp) from the button tabs. You can change your Build Configuration from the left Project window by setting it to Debug or Release mode. //—————————————————————————   #include #pragma hdrstop   #include “Unit1.h” //————————————————————————— #pragma package(smart_init) #pragma resource “*.dfm” TForm1 *Form1; //————————————————————————— __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } 4. Double click to Button1 to create OnClick() event for this button. Add these lines into Button1Click() event, between { and } brackets as given below, […]

Read More

Learn How To Use The Final Specifier In Modern C++

Modern C++ has many additions compared to the original C++ standard. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords, the final and the override. The final specifier (keyword) is used for a function or for a class that cannot be derived and overridden by derived classes. This final specifier is used with the C++ compiler that has C++11 and the other higher C++ standards. In this post, we explain a friend and extended friend declaration in modern C++. What Is the final specifier in C++? The final specifier (keyword) is used for a function or for a class that cannot be overridden by derived classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords: final specifies that a method cannot be overridden, or a class cannot be derived. override specifies that a method overrides a virtual method declared in one of its parent classes. In this post, we explain how to use the final specifier in C++. The final specifier is and Explicit Virtual Override that prevents a class from being further inherited or prevents a function from being overridden. We can add the final specifier to a class definition or to a virtual member function declaration inside a class definition. A class with the final specifier is not allowed to be a base class for another class. A virtual function with the final specifier cannot be overridden in a derived class. If a virtual member function f in some class B is marked final and in a class D derived from B, a function D::f overrides B::f, the program is ill-formed (the compiler does not issue a message). How to use the final specifier in C++ functions? The final specifier is used to designate virtual functions that cannot be overridden in a derived class. Here are the syntaxes of how to use it:   function_declaration final;   or with the body,   function_declaration final { }   or with virtual function declaration,   virtual function_declaration final;   and, here is an example for the Clang-enhanced C++ compilers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20   #include   class Tx { virtual void f() final; };   class Ty : public Tx { virtual void f(); // ERROR: declaration of ‘f’ overrides a ‘final’ function };   int main() {    Ty o1;      return 0; }   in previous-generation compilers, it was used with the [[final]] keyword like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20   #include   class Tx { virtual void f() [[final]]; };   class Ty : public Tx { virtual void f(); // ERROR: declaration of ‘f’ overrides a ‘final’ function };   int main() {    Ty o1;      return 0; }   If you need an official docwiki, please check this: Workaround for C++11 attributes How to use the final specifier in modern C++ classes? The final specifier is used to designate classes that cannot be inherited. Here are the syntaxes about how to use it:   class class_name final   or with other base classes as […]

Read More

Five Simple C++ Console Examples for Beginners

The C++ Builder CE Community Edition is a free version of professional C++ Builder that you can develop GUI based desktop and mobile applications in C++. In this post, we will give you five simple C++ console examples for beginners you can run using C++ Builder 11 CE. The latest C++ Builder 11 CE was released in April 2023. If you are a start-up developer, student, hobbyist or just interested in learning to code then C++ Builder Community Edition may well be just the thing for you. Read the FAQ notes on the CE license and then simply fill out the form and download C++ Builder 11 CE. How to develop a C++ console application in C++Builder CE? If you download C++ Builder Community Edition then we can start to coding, Create a new C++ Builder Console application from File->New-> menu In the New Console Application window leave Target Framework blank “None” Be sure that Source Type is C++ and press OK This will open a new code editor window at the center. When you start coding, first of all, you should include libraries that you wish to use. The C++ language has many libraries and each of them has commands or functions for specific tasks. For example, the iostream library has standard input and output methods to display data and read from files and similar sources. Generally, for beginners, the iostream header is enough to enable you to create simple apps. We can include this library header as below, Second, you should add a main procedure. This is the main part of the program – hence the name – and it is executed first. In the simplest sense, all other parts of your program are launched from the main section either directly or indirectly. Things get a little more complicated than that once you start to write more complex programs but for now you can think of the main section as the ‘main loop’ where things begin to happen in your program code. After that you should write your lines of program code into this procedure, between the { and } brackets. You can print texts by using std::cout as below,   std::cout

Read More