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

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.

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

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

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;

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

will be

And out final modern code will look like this

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

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

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

Example 3

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

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

Example 4

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

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;

Conclusion

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

  • Instead of char or string use String or UnicodeString . Use L”” form to support UnicodeStrings instead of “” forms of strings
  • if there is cout output use Memo->Lines->Add(); to add unicode String to the lines of Memo component
  • if there is cin input use Edit boxes to obtain your variables i.e String s=Edit1->Text; or int x=StrToInt(Edi1->Text); or float f=StrToFloat(Edit1->Text);
  • Unicode string has printf() function, you can use its printf() function over UnicodeStrings. i.e. UnicodeString us; us.printf(“i=%d f=%f”, 100, 3.14); Memo1->Lines->Add(s);

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?