Programming dev-c++ FireMonkey FMX ide learn learn

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

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

Five Simple Examples Of C++ VCL 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++ console examples to help you understand how C++ Builder 11 CE runs applications. 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 C++ application for Windows by using VCL framework in C++ Builder CE. How to develop C++ VCL 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. Let’s start to develop a C++ app with GUI using VCL framework. 1. Choose File->New-> “Windows VCL Application – C++ Builder” menu. This will create a New C++ Project for Windows. This will allow you develop C++ apps with VCL 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, void __fastcall TForm1::Button1Click(TObject *Sender) { String str = “Hello World”;   Memo1->Lines->Add(str); } This example above is a modern “Hello World” example for Windows which runs with C++ Builder. 5. Now you can compile this C++ code; just press the F9 key or just click the Run button in the […]

Read More

What Is An Extended sizeof In Modern C++?

In modern programming, the size of any data is very important, while the programmers are trying to minimize data types or stream packages, global data transfer is increasing by the requests of new technologies. Thus, 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 this 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?. What is the sizeof() function in C++? The sizeof() function is very useful to get the size of variables in bytes. Generally, size of data packages ( structs, classes, objects, arrays) need to be checked. When you create a struct or class, and If you are using them in I/O operations, check how much size it is. Syntaxes for the sizeof operator is, and, Checking the size of data is sometimes important in professional programming. These checks allow you to optimize your data packages thus your application use a lower amount of memory and it will have more efficient and thus higher speed I/O operations. Game data packages, video and audio packages, TCP/IP data packages, AI related data packages are these kinds of packages. Is there a simple example of sizeof operator in C++?? Here is a simple sizeof() example,   char s[5]; int i = sizeof(s);   What is the extended sizeof operator in modern C++? 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. You can access to the members of a class by using the :: specifier. For example, we can get the size of a member in a class as below.   class Ta {    public:       int i = 0; };   int main() {   int x = sizeof(Ta::i); }   Note that this member should be a public member. Is there an extended sizeof operator in modern C++? We can check the size of different class types as shown below. 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   #include   class Ta { };   class Tb { };   class Tc : Ta, Tb { };   int main() { char s[5]; int i = sizeof(s);   std::cout

Read More

What Is The Inline Namespace Feature In Modern C++?

In modern C++, namespaces are a kind of library or framework. They are useful to hold the same name of classes, methods, and other entities in different namespaces. C++11 standard and other standards above, allow the inline keyword in a namespace definition and in this post, we explain how to use inline namespaces in modern C++. What is a namespace in modern C++? Namespaces in C++ allow users to group entities like classes, methods, variables and functions in a narrower namespace scope instead of using in the global scope. This prevents name conflicts between classes, methods, functions and variables in large projects. Namespaces organize the elements of developers into different logical scopes referred to by names. Thus, different users (or namespaces) may use same method names and variables in different running processes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19   #include namespace nsA { void myf() { std::cout

Read More

What Is An Extended Friend Declaration in Modern C++?

In modern 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 this post, we explain a friend and extended friend declaration in modern C++. What is a friend declaration in modern C++? The friend declaration is used in a class body with the friend keyword and grants a function or another class access to private and protected members of the class where the friend declaration appears. C++11 extends the current language to support a wider range of friend declarations. Now, in modern C++has the extended friend declaration feature to address class declarations which is a template parameter as a friend. This extended friend declaration is available with a C++ compiler that has C++11 and above standards. Syntaxes for the friend declaration (since C++11):   friend typename_specifier ;   and   friend simple_type_specifier ;   Is there a simple example of a friend declaration in modern C++?? Here is a simple friend declaration C++ example.   class Ta;   class Tx { friend Ta; };   How do I make a function declaration with friend declaration in modern C++? In C++, we can declare functions with friend. Here is the syntax:   friend function_declaration   and we can declare the F() function with friend as shown below.   float F(float m) { return m*9.81; // F=m.g }   class Tx { friend float F(float m); };   How to create a function definition with friend declaration in modern C++? In C++, we can define functions with friend. Here is the syntax how we can do,   friend function_definition   and we can declare a reset() function with friend as below.   class Tx { int i; friend void reset( Tx& T) { T.i = 0; } };   What is an elaborated class specifier with friend declaration in modern C++? According to the C++ standard, an elaborated type specifier must be used in a friend declaration for a class. That means the class-key of the elaborated_ type_specifier is required. In C++, we can use elaborated class specifier with friend. Here is the syntax.   friend elaborated_class_specifier ;   How to use a template with friend declaration in modern C++? Here is an template example that uses friend.   template class Tw { friend Tw; };   How to use a namespace with friend declaration in a class in modern C++? Here is a friend example that is used in a class of a namespace.   class Ts;   namespace Ns { class Tx { friend Ts; }; }   Is there a full example of an extended friend declaration in modern C++? Here is a full example to extended friend declaration in modern C++. 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73   #include   class Ta;   class Tb { […]

Read More

What Is An Assignment Operator In C++?

One of the most commonly used features of C++ software, in common with many programming languages, are assignment operators. These take the form of copy assignment and move assignment operators. In C++, a copy assignment operator can be used with “operator=” to create a new object from an existing one. In this post, we explain assignment operators with copy assignment operator examples in C++. If you are looking for more information on assignment operators in C or C++, the following post discusses them in depth. 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 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 of a 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 assignment operator in C++? Here is 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 Assignment Operator   std::cout

Read More

What Are The Rules Of Zero, Three, Five, And Six In C++?

In C++, classes and structs are one of the most important parts of modern software development. In modern C++, there are some rules to support the principles of programming, in class definitions there are a few rules to be considered, these are the Rule of Zero, the Rule of Three, the Rule of Five, and the Rule of Six. In this post, we explain all of these rules with examples. C++ is an Object-Oriented Programming (OOP) language, and 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). Most developers find that using OOP techniques helps them to map real-world behavior and bring an organizational structure to data. 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. First, let’s refresh our memory about the fact that Resource Acquisition Is Initialization (RAII) in OOP programming, and the Single Responsibility Principle and how that relates to the Rule of Zero in C++. 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++? The 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. You should prefer the case where no special member functions need to be defined.  Here is more about Rule of Zero with C++ Examples, What is the rule of three in C++? The Rule of Three states that if you need to define a class that has any of the following special member functions a copy constructor, copy assignment operator, or destructor then usually you need to define all these three special member functions. So, these 3 special member functions below should be defined if you have at least one of them defined, Copy constructor Copy assignment operator Destructor Here is more about Rule of Three with C++ examples, What is the rule of five in C++? The Rule of Three is outdated after C++11. C++11 comes with two additional special members of move semantics: the move constructor and the move assignment operator. So, there is another rule, the Rule of Five. The Rule of Five states that if you need to define any of the five special members below, copy constructor, copy assignment operator, move constructor, move assignment operator, or a destructor then you probably need to define or delete (or at least consider) all five of them. Here is more about Rule of Five with C++ examples, Actually, this could be called “The Rule of Six“, because the […]

Read More