Noutați

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 Are The Rules Of Modern C++?

Hello developers, actually there are no rules! ???? You have a broad freedom in C++. If your C++ app is running well and performing its functions well then, you’re doing good. For me, I think this is the main rule of developing applications, “develop C++ apps running perfectly that help people and the world“. In professional programming, you strive to create better applications that run perfectly and use memory, CPU, and GPU efficiently. But of course, joking apart, in reality, all computer programming languages actually do have rules; both those of syntax and those of semantics. This week we have more new C++ posts about some minor and major important rules of modern C++. If you are just starting out on your C++ journey and want to jump into the rules of modern C++, 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. 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. Table of Contents How can we learn the rules of modern C++? Are there C++ examples of how to apply the rules of modern C++? What is new in C++ Builder CE? What might be next for C++ Builder? How can we learn the rules of modern C++? In modern C++, when you develop applications for future updates or upgrades to new compilers, and you want to use memory, CPU, GPU, and other peripherals of that device in a correct way, there are some recommendations in class definitions. In class definitions, there are some recommendations; called The Rule of Zero, Rule of Three, Rule of Five, and Rule of Six. In modern C++, there are some rules to support the principles of programming, one of which is the Rule of Six in C++ (also known as the Rule of Five, excluding the constructor). In the first post, we explain the Rule of Six in C++ that includes other the Rule of 5, the Rule of 3, and the Rule of 0. https://learncplusplus.org/what-is-the-rule-of-six-in-modern-c/ In the next post, we explain the Rule of Five in C++ (also known as the Rule of Six, including constructor) https://learncplusplus.org/what-is-the-rule-of-five-in-modern-c/ The Move Assignment Operator is one of the great features of Object-Oriented Programming in professional development. It complements features like the copy assignment operator, copy constructor, move constructor, and destructor. Since the C++11 standards, the move assignment operator is declared by using “operator=” and it allows you to move one object to another object. In the next post, we explain what a move assignment operator is along with some C++ examples. https://learncplusplus.org/what-is-the-move-assignment-operator-in-modern-c/ When you define your variable you should know well what it is to be used for. Is it a numeric or alphanumeric variable or a binary data that holds all kinds of bytes. If it is a numeric value, what are its limits? If it is between 0 to 255 you don’t need to define with int, an unsigned char (UBYTE) is enough. This may speed up your code 1-4 times faster. If it is a larger number, you can use long, long long, or unsigned long long int for positive numbers. These small rules are […]

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

Learn How To Draw Charts With Simple TeeChart (TChart) Examples in C++

The RAD Studio, C++ Builder 11 and C++ Builder CE Community Editions have a lot of amazing visual and nonvisual components that you can use in your modern applications for Windows and mobile. One of these is the free chart component for the VCL and FMX frameworks called TeeChart (TChart). TChart comes with RAD Studio including RAD Studio 10.x,11.x and the CE versions. How To Install and Use TeeChart (TChart) in C++ Builder CE? When you install RAD Studio on the second page of the “platforms” choices are some optional modules and components. One of those is “TeeChart Standard”. This is the free version of TChart which you can use in your applications. If you want more detailed professional charts in your applications, there is also TeeChart Pro by Steema. The TeeChart Pro charting component library offers hundreds of Graph styles in 2D and 3D for data visualization, 56 mathematical, statistical, and financial functions for you to choose from together with an unlimited number of axes and 30 Palette components. Please visit their official Steema web page for more details. How To Develop Apps With TeeChart (TChart) in C++? If you installed TChart in C++ Builder, you can use this component in your VCL or FMX applications. To do this: Create a New C++ Builder VCL Windows Application in RAD Studio Go to Palette window, there is TChart component (under the TeeChart Lite category) to visualize many kind of chart graphics. Drag it on to form, or If you have a specific area like a panel, rectangle or a tab drag it on to this area. You can move, resize or align it by selecting Align->Client selection, you can also set its margins. C++ Builder 11 CE Form Design with TChart component Now you can modify its default settings and you can add your custom charts, Pie, Bars, y=f(x) series, etc. How To Draw y = f(x) Series With TeeChart (TChart) in C++? Double click to TChart to create your own Chart series. Press “Add…” Button in Editing Chart Window, this will bring you to the Series Gallery as shown below. Select Functions tab and select y=f(x) series, Press OK, and Close When you are in form designer mode, be sure that there is Series1 in the Structure panel as below, Now you have a y =f(x) graph, add a Button (TButton) Double Click to your button (i.e. Button1) When user clicks this button we can clear and add series in this button click like so: Series1->Clear(); Series1->Add(  0.0, 55.0, clTeeColor ); Series1->Add( 10.0, 72.0, clTeeColor ); Series1->Add( 30.0, 95.0, clTeeColor ); Series1->Add( 40.0, 123.0, clTeeColor ); How To Draw Bar Series With TeeChart (TChart) in C++? If you want you can add a new Bar Chart, Double click to TChart to create your own Chart series. Press “Add…” Button in Editing Chart Window, this will bring you Series Gallery as below, In Series tab, select Bar Series,(i.e. this will be named as Series2) Press OK, and Close Add a new Button (TButton), named as Button2 Double click to this button (i.e Button2) When user clicks this button we can clear and add bar series in this button click as below here Series2->Clear(); Series2->Add(  88.0, “Jan”, clTeeColor ); Series2->Add(  72.0, “Feb”, clTeeColor ); Series2->Add(  95.0, “Mar”, clTeeColor ); Series2->Add( 123.0, “Apr”, clTeeColor ); that’s […]

Read More

What Is The Long Long Int Type In C++?

In the early days of C++ there were few data types such as char, float, and int. Over time these types improved with new additions. Modern C++ is really amazing, it allows you to define your data type in accordance with the limits of your variable values. One of the largest integer types is the unsigned long long or unsigned long long int and in this post we explain how to use the long long int type. What are the fundamental variables In C++? A professional developer should always know the size of data types and their limits and which data types of a variable is needed in these limits, because the operating system allocates memory and decides what can be stored in the reserved memory. In addition to standard types in C++, there are fixed-width integers which are defined types in header  with a fixed number of bits. Fixed-width integers are called as intN_t or intX_t integers where N or X represents number of bits reserved for that type.  What is long long int type in C++? The long long integral type (or long long int) is an integer type for the larger numbers that doubles bytes of long type integer. The long long and long long int types are identical as same as long and long int types are identical. In modern C++, C++11 standard introduced long long integral type to be more compatible with C99 standards. Detailed information on this feature, can be seen in this proposal: long long type Proposal document. Adding long long was proposed previously by Roland Hartinger in June of 1995. At the time, long long had not been considered by the C committee, and the C++ committee was reluctant to add a fundamental type that was not also in C. Almost a decade later long long was part of C99, and many major C++ compilers start to support it. long long has been added to standards with C++11. If we consider that int variable has size of 4 bytes, long long int has size of 8 bytes. usigned long long int is same. Here are the most used integer types in C++, Type Typical Bit Width Typical Range int 4bytes -2147483648 to 2147483647 unsigned int 4bytes 0 to 4294967295 signed int 4bytes -2147483648 to 2147483647 short int 2bytes -32768 to 32767 unsigned short int 2bytes 0 to 65,535 signed short int 2bytes -32768 to 32767 long int 4bytes -2,147,483,648 to 2,147,483,647 signed long int 4bytes same as long int unsigned long int 4bytes 0 to 4,294,967,295 long long int 8bytes -(2^63) to (2^63)-1 unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615 Is there a simple example of how to use the long long int type in C++? Here is a simple example that shows how you can use int, long int long long int, unsigned long long int,   int i = 2147483647; long int l = 2147483647; long long int ll = 9223372036854775807; unsigned long long int ull= 18446744073709551615;   Is there an example of how to use long long int type in C++? Here is an example of how you can use long long, long long int and unsigned long long, unsigned long long int, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19   #include   int main() { int i = […]

Read More

What Is The Rule Of Six In Modern C++?

In C++, classes and structs are one of the most important parts of modern application development. In modern C++, there are some rules to support the principles of programming, one of which is the Rule of Six in C++ (also known as the Rule of Five, excluding the constructor). In this post, we explain the Rule of Six in C++ 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 help 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 six 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 Six. The Rule of Six states that if you need to define any of the six special members below, constructor 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 six of them. This rule also known as “The Rule of Five“, because the default constructor is special, and, therefore, sometimes excluded. Note that, when you define […]

Read More