Site icon Learn C++

Learn How To Adapt Classic Windows C/C++ Samples To Modern C++ In C++ Builder

There are many C++ examples and many tutorials over the classic C/C++, and when you jump to Modern C++ these examples may seem to be very hard to adapt. In real, it is not complicated much, you just need to modify input and output operations to GUI based input and output components. So basically, most of classic C and C++ examples are easy to transfer to GUI based applications.

In this article, we would like to show some basic classic to modern C++ converting techniques in C++ Builder. C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files and LSP support for code. Note that it supports both C and C++ functions and it has own VCL library for Windows only applications and FMX libraries for multi-platform application.

There is a free C++ Builder Community Edition for students, beginners and startups. You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter. Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.

If you have a installed C++ Builder, here are some examples below that found from the most popular C++ web sites.

Example 1

Lets start with this example : https://www.w3schools.com/cpp/cpp_getstarted.asp

[crayon-6740a741eb7d0345442718/]

Before we start coding, lets check this classic example, we don’t need some of these definitions below because we use FireMonkey UI framework, projects has this line #include <fmx.h> , this header includes most of modern libraries and we don’t need these classic libraries. If you are not sure what you need or not you can keep them after the #include <fmx.h> line. All classic C and C++ libraries are supported in C++ Builder with its CLANG (C++11, C++14, C++17) compiler standards.

[crayon-6740a741eb7d8994570288/]

Also we don’t need this main() function because it is automatically generated in Project1.cpp that runs our Unit1.cpp Form based module.

[crayon-6740a741eb7da566101304/]

Now let’s modernize this simple Hello World example. We want to code in modern C++ and we want to display as same as in this example on a Windows form based application. We need a Memo to show outputs and a button to run inside of this main() procedure. We can also run this directly inside the OnCreate() event of Form. Here when we press a Button, things inside this main() procedure will run and outputs will be displayed in Memo. To do this;

1. Open RAD Studio or C++ Builder, Create a new Multi-Device C++ Builder application, that means we are developing a modern C/C++ Builder (FireMonkey) project with beautiful GUIs. This will generate our first project file, main cpp file and cpp file of the form. It will also generate form design and headers of cpp files. Create a new folder (i.e. Example1), save all project and units into the folder. You can see all of your source files in this folder.
2. Add a Button and a Memo components (in Standard category) on your Form, from the Component Palette right side.
3. Double click to Button to create OnClick() event of this button automatically. This will push you to code editor. Note that, you can switch Code Window or Design Window by pressing the F12 key.
4. As you see our first C++ codes were automatically generated.

5. Instead of string use Strings as in this line which generates output

[crayon-6740a741eb7dc143137441/]

we will use String to use UnicodeStrings which supports worldwide language characters in WideChar forms. Modify it by using L”” form inside our Button1Click() event as below;

[crayon-6740a741eb7e0532891458/]

6. Instead of this line below is about to output we will use String to use UnicodeStrings which supports worldwide language characters in WideChar forms.

cout outputs to standard output. Instead of this command we need to use a component, here we will use Memo as a text based output component. So this line

[crayon-6740a741eb7e2812438368/]

will be

[crayon-6740a741eb7e4198483338/]

And out final modern code will look like this

at last we don’t need this return, because our button is a void function

[crayon-6740a741eb7e6472334914/]

Now we have totally modernized C/C++ example with a Form, a Buton and Memo. Our String variable supports Unicode strings. We can run our example by hitting F9 or Ctrl+F9 or by the Run / Run without Debugging buttons. If all is successful when you run you will see your form, and your output will be displayed when you press the button.

Example 2

Lets look at this example https://www.programiz.com/cpp-programming/examples/swapping

[crayon-6740a741eb7e7427924929/]

As in first example we use Add() method instead of all lines with couts will be as follow;

[crayon-6740a741eb7e9014040978/]

Example 3

Lets look at this example https://beginnersbook.com/2017/08/cpp-functions/

[crayon-6740a741eb7eb723352381/]

Here , we don’t need to change anything in sum function, we can add this above of the Button1Click() event, we can also add this function as a member of Form class. We just need to modify main() function to OnClick() event of Button as in first example

[crayon-6740a741eb7ee763857094/]

Example 4

Lets look at this example https://www.w3schools.com/cpp/cpp_user_input.asp

[crayon-6740a741eb7f0191511618/]

Here in addition to Memo and Button, we need two Label component and two Edit components to get x and y. Drag labels change their Texts to “Type a number:”, “Type another number:”. Drag two Edit boxes nearby them. Now lets modify our fist code as below;

[crayon-6740a741eb7f3049297392/]

Conclusion

These examples may give you some tricks how you can modernize your classic C/C++ codes.
As in these examples, note that;

Exit mobile version