C++ is one of the most powerful programming languages as well as one of the world’s most popular. C++ Builder is a fast and reliable IDE and compiler which is a good cplusplus reference for iOS, Windows, and Android. One of the important things we can do to help people learning the language is to create simple C++ examples for beginners. From school students to those who might consider themselves “silver surfers”, anyone who wants to learn programming can benefit from a good running start.
In recent years, actually every year, we have had many new OS releases and that means your apps should support these latest operating system versions and the great new features they bring, along with the latest devices and innovations. A good quality efficient C++ IDE and compiler are very important for beginners and professionals. It takes longer and is much harder work to build a house with the wrong or outdated tools and it’s just the same with computer software!
Actually, even with all the help, C++ is sometimes hard, especially when it comes to detecting problems or bugs. If you start well at the outset I am sure almost anyone can create simple apps that perform creditably. Of course some applications require deeper knowledge not only in programming but also in other skills, for example math, physics, computer science, electronic engineering, other engineering skills, or specific areas like artificial intelligence, image processing and IoTs. But, with the right C++ IDE to lift you up, some perseverance, and luck too software development means you can quite literally create anything you can dream of. That’s why it’s such a fascinatingly absorbing occupation.
Table of Contents
How to write a program in C++ for beginners in 2022
Step 1 : Download a C++ IDE and install it
The first step should be downloading a C++ IDE. Beginners are generally students or individual persons who are taking their first steps into the subject. Thus, a free C++ IDE is a good to place start. The C++Builder Community Edition is free C++ Builder IDE with a limited commercial use license. It is a full-featured C++ IDE for building Windows, iOS and Android apps from a single modern C++ codebase. It can be used free of charge by Embarcadero’s community of freelance developers, startups, students and non-profits or indeed anybody who meets the requirements for the Community Edition license. C++Builder Community Edition includes a code editor, powerful debugging tools, built-in access to popular local databases with live data at design time, Bluetooth and IoT capabilities and a visual UI designer with support for pixel perfect, platform specific styling.
Professional developers (and those for whom the Community Edition is not appropriate) can download a fully-working 30 day trial of the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.
Step 2 : Learn basics of the C++ IDE
Start with learning the basic buttons and menus of the IDE. In total there are 5 main elements. These are; code area, save menu, load menu, compile menu, output and error windows. You don’t need to know all features of the IDE.
Step 3 : Do you have an idea for an app?
Let’s think about an application. What do we want our app to do? When answering that question, traditionally, app designers try to imagine the user using the app to solve a problem or achieve something like messaging a friend or editing an image. The app designed tries to focus on an imaginary user clicking on buttons in the app, and achieving the goals of the app. Designers call this a “user story” because that’s pretty much what it is – the story of a user using your app to do things. The user story helps focus our minds on the things we might need in the user interface, and the kind of technical challenges we might need to address to turn our imaginary user story into reality.
Professional app designers say things like “I am a person cooking lunch and I discover I am using the last of my olive oil. I want a shopping list app in which I can remind myself I need olive oil“. Another user story might be “I am a person who needs to convert inches to centimeters very quickly“.
Now start to think about what things your app will need to display and the information it will need to collect in order to make your user story possible. This will help you sketch out an idea of what your app’s user interface will need to look like.
When you have refined what you think your app’s screens will need to look like you can turn to your C++ IDE and its screen designer.
RAD Studio’s C++ Builder version comes with the award-winning VCL framework for creating high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. Let’s start to develop a very simple C++ GUI app using FireMonkey framework to make ourselves familiar with the process of creating a user interface.
Step 4: Create a new C++ Project?
Hopefully by now you have downloaded and installed a version of C++ Builder or RAD Studio with C++. Let’s create a new Multi-Device (Cross-Platform) C++ application by using FireMonkey (FMX) framework.
Choose File->New-> “Multi-Device Application – C++ Builder” menu.
This will create a new cross-platform C++ Project which works on Windows and can also be easily compiled for other operating systems like iOS and Android. This will allow you develop C++ apps with FireMonkey FMX UI elements. If you don’t need UI (user interface) elements you create a console application instead. For now, we’re sticking with a full FireMonkey FMX application. Note that, unlike FireMonkey projects, VCL projects can only run on Microsoft Windows.
Save all Unit files and Project file to a folder.
Step 5 : How do we get started designing a C++ app screen?
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.
Take some time to explore the what the various visual controls look like. You can click on ones you added but no longer need and then click the del/delete button or right click on the relevant control in the form designer preview window and select “delete”.
Step 6 : How do I write a modern ‘Hello World’ C++ for beginners example?
Instead of using printf() and cout(), you can start with simple and modern C++ applications with GUIs. Tell them to Double click to Button1 to create OnClick() event for this button. Add these lines into Button1Click() event,
1 2 3 |
String str = "Hello World"; Memo1->Lines->Add(str); |
Here you declare (create) a str String variable, that means it is a string, and you write data “Hello World” to this variable. Memo boxes has Lines property, and this property has Add() method to add new lines to this memo box. We use “->” to point each property or method of these kind of elements, this is one of the strong part of C++ called Object Oriented Programming. We use “;” at the end of each end of commands.
Strings are UnicodeString, that means they support Unicode formats and very useful globally. Most console apps fails in displaying texts of different languages. This example above is a modern “Hello World” example for Windows, iOS and Android which runs with C++ Builder.
Step 7 : How do I compile and run a C++ for beginners example?
Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together. You can also use the Run menu from the top to run your application. You should see this below as a form application
If you get an error, check your code – it’s easy to mistype or have the braces (“}” characters) in the wrong place. Note that C++ is case sensitive, so upper and lower characters should be also same. Then press the F9 key or just click the Run button again.
From this point, you can add more code or you can switch back to screen design mode by pressing F12. And you can add more components, then more code. This is how modern C++ coding works with a UI Toolkit. You can try this simple “Hello World” example below too,
How to write a program in C++ for beginners with VCL?
If you want to know how to compile modern and visual application designed only for Windows, they can do same as above but with a VCL application. Note that VCL applications are only for Windows operating system, you can not compile for other operating systems. There are slight changes between VCL and FMX components.
Now let’s create a Visual and Modern C++ example in C++ Builder,
- Open C++ Builder or RAD Studio IDE
- Create a new Windows VCL Application in C++ Builder from File->New menu. This will allow you develop C++ apps with VCL UI elements.
- Save all units and project files with “HelloWorld_” prefix in a HelloWorld folder.
- Add a Memo (TMemo) and a Button (TButton) from the Palette to your Form. TMemo component is good to display all outputs as in console applications.
- Double click the Button to create OnClick() event, inside that Button1Click event write lines as given in the example below,
- Run project by hitting F9 or click Run with Debugging
- If there is error, please check your lines
- If all is fine then save all project
- if you check your header file you will see all your objects (Memo, Button etc. ) are automatically defined as below. You don’t need to change anything here
If you are a beginner “Hello World” examples are good to understand features of that programming language. It is good to understand how to edit text in the C++ IDE, how to write the correct C++ syntax – which the IDE will help you with, how to compile and link your program, how to debug and execute it, and how to deploy or release the finished program so that other people can use it.
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.
The latest posts along with more examples and tutorials can be found here on the Embarcadero Blog with this dynamic search https://blogs.embarcadero.com/?s=C%2B%2B and on specially C++ focused site LearnCPlusPlus.org.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition