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.
Table of Contents
Example 1
Lets start with this example : https://www.w3schools.com/cpp/cpp_getstarted.asp
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <string> using namespace std; int main() { string greeting = "Hello"; cout << greeting; return 0; } |
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.
1 2 3 4 5 |
#include <iostream> #include <string> using namespace std; |
Also we don’t need this main() function because it is automatically generated in Project1.cpp that runs our Unit1.cpp Form based module.
1 2 3 4 |
int main() { } |
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
1 2 3 |
string greeting = "Hello"; |
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;
1 2 3 |
String greeting = L"Hello"; |
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
1 2 3 |
cout << greeting; |
will be
1 2 3 |
Memo1->Lines->Add(greeting); |
And out final modern code will look like this
at last we don’t need this return, because our button is a void function
1 2 3 |
return 0; |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { int a = 5, b = 10, temp; cout << "Before swapping." << endl; cout << "a = " << a << ", b = " << b << endl; temp = a; a = b; b = temp; cout << "\nAfter swapping." << endl; cout << "a = " << a << ", b = " << b << endl; return 0; } |
As in first example we use Add() method instead of all lines with couts will be as follow;
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 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int a = 5, b = 10, temp; Memo1->Lines->Add("Before swapping."); Memo1->Lines->Add("a = " + IntToStr(a) + ", b = " + IntToStr(b) ); temp = a; a = b; b = temp; Memo1->Lines->Add("\nAfter swapping."); Memo1->Lines->Add("a = " + IntToStr(a) + ", b = " + IntToStr(b) ); } |
Example 3
Lets look at this example https://beginnersbook.com/2017/08/cpp-functions/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; /* This function adds two integer values * and returns the result */int sum(int num1, int num2){ int num3 = num1+num2; return num3; } int main(){ //Calling the function cout<<sum(1,99); return 0; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; int sum(int num1, int num2){ int num3 = num1+num2; return num3; } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Memo1->Lines->Add( IntToStr(sum(1,99)) ); } |
Example 4
Lets look at this example https://www.w3schools.com/cpp/cpp_user_input.asp
1 2 3 4 5 6 7 8 |
int x, y; int sum; cout << "Type a number: "; cin >> x; cout << "Type another number: "; cin >> y; sum = x + y; cout << "Sum is: " << sum; |
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;
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 <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int x,y,sum; x=Edit1->Text.ToInt(); y=Edit2->Text.ToInt(); sum=x+y; Memo1->Lines->Add( L"Sum is: " + IntToStr(sum) ); } |
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);