Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

15 Tips to Graduate from Classroom C++ to Modern C++ Part 1/3

digital particles

You may know C or C++ from college and want to develop apps, games and AI applications, but are unsure if what you learned suffices to help you reach your goal. How can you shift from “classroom” C++ to Modern C++?

 Today, coding in any programming language is an indispensable skill, and the internet has many resources, ranging from how to code from scratch to how to build high-end professional applications, APIs, and algorithms. Learning to code in at least one programming language is compulsory in educational institutions. Every aspect of life today is controlled at some point by a computer program.   With the development of computers and programming languages alongside operating systems, programming has grown larger and more complex. Today, programming is needed in all departments everywhere. 

C++ is one of the popular programming languages taught in educational programs. You may have learned introductory programming skills, but how can you put them to good use?  Is the C++ you learned in school enough to be productive? For amateurs, the answer is yes! You can be productive while using C++ by running basic code in the console. To be a professional, however, the answer is no. You need to build Modern C++ applications. So how can you move from Classroom C++ to Modern C++ and include stunning images, themes, databases, powerful functionality, and 2D or 3D graphics in your multi-platform or cross-platform application? In this article, we will provide top tips on how you can enhance your skill set. But before that, let’s share a few reasons that C++ is useful.

  1. Know Why C++ is Needed in Education 

In the programming world, there are two types of programming languages: Interpreted and Non-Interpreted (Compiled) programming languages. Our computers work with machine code (code that can be directly programmed by assembly code on a CPU or GPU) that tells the computer what to do. Executable files have this code. Machine code is the fastest and most highly native code, but it requires writing many lines for simple tasks and cannot be generalized for different kinds of CPUs. 

A compiler is a computer program that converts code in a human-readable programming language written in text into executable machine code. Depending on how good the compiler is at optimization, such code may not be quite as fast as hand-written assembly code, but the difference in speed is usually very small. C++ compilers are typically very good at optimization, and the language is designed with optimization in mind. This is one reason C++ is the fastest and most powerful programming language and is one of the most popular languages in the world. 

Interpreted programming languages are either compiled to machine code on the fly, or instead run inside a normal executable application that reads the source code and implements the behavior. Java, Python, or Visual Basic are good examples. This is why such languages are slower when executing operations, as they need to use compiled libraries for faster operations. Most interpreted programming languages and some asm languages use C/C++ compilers to build their libraries: Python, for example, has its key performance-oriented libraries written in C or C++, so you write Python code that actually makes use of C++ under the hood. Using an interpreted programming language is like being carried by a runner, while using a compiled (non-interpreted) programming language is like running yourself. This subtle difference turns into a huge difference when the same routine (e.g., for face recognition) is run millions of times in a millisecond.

C++ can be a little bit hard to learn, but is faster and more productive in all areas and on all platforms. Learning C++ at your school or university is a good plan because the C++ field is very broad. So to modernize your C++ knowledge, you should decide what kind of applications you want to develop professionally. Do you want to develop database applications? Do you want to code backend C++ applications running on servers or engineering or scientific research? Do you want to develop simple applications with stunning images and UI interfaces? Do you want to develop 2D or 3D games, AI applications, or more complex applications including all of these?

Here is the first tip: 

Tip 1. ‘Your college C or C++ knowledge is necessary to move on to Modern C++ and choose the field in which you want to work. This knowledge can also be applied while building modern applications.

  1. Use Multi-Platform Compilers or Choose The Primary OS Platform For Which You Want to Develop

C++ has standards, so generally, most compilers can run most C++ code but some compilers have some specific features. Different operating systems have different popular C++ compilers and IDEs. For example, Windows OS has Visual C++, C++ Builder, and many other compilers and IDEs; Linux has GNU C/C++ and clang; Mac-OS, iOS has clang; and Android has Android Studio with C++. There is also cross- and multiplatform support in some of these compilers and IDEs like C++ Builder and Visual C++.

If you want to focus only on one operating system, you can choose one of these. If your app succeeds on that OS and you want to migrate it to other operating systems, that might be hard to do. With Modern C++, using a multiplatform compiler or IDE (also called cross-platform or multi-device development) is better than using a specific compiler, as this allows you to easily compile your code for all platforms. This is one of the keys to making your code modern: your code might be focused on one OS, but it should be easy to compile on others.

Tip 2: Use multi-platform compilers. If you focus on one OS platform, also consider that it will be a multi-OS platform.

  1. Choose Your Professional Compiler With The IDE 

 Today there are many C++ compilers and IDEs with many features to develop professional applications, including Visual C++, C++ Builder, GNU C/C++, Objective-C, Dev-C++ (with TDM-GCC), Code-Blocks (with GNU C/C++), CLion (with GNU C/C++), Eclipse editor. Some compilers come with IDE (or IDE comes with its own compiler), some IDEs come with a popular compiler (like GNU C/C++), and some IDEs only support C++ coding with syntax highlighting (like Notepad++). 

Here, to move to Modern C++ with the latest standards, database connectivity, cross-platform support, and 2D and 3D support, we suggest C++ Builder. It runs on Windows and is also able to compile code for iOS and Android 32-bit. 

C++ Builder is part of RAD Studio, but works standalone if you don’t want to use the other language RAD Studio offers (Delphi). C++ Builder includes a Clang-enhanced compiler for Win32, Win64, and iOS. It also features a modern, high-productivity RAD Studio IDE, debugger tools, and enterprise connectivity to accelerate cross-platform UI development. You can develop GUI-based applications easily, as it comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

A free C++ Builder Community Edition can be used by students, beginners, and startups. You can download the free C++ Builder Community Edition here. Professional developers can use the professional, architect, or enterprise versions of C++ Builder. You can download and use the trial version for one month, with the same capabilities as the full version. Please visit here for more details. 

If you want to code only backend apps (or console apps), you can use the GNU C/C++ compiler (GCC, g++) with a compatible IDE. GNU g++ is another powerful C++ compiler originally written as the compiler for the Unix, Linux, and GNU operating system. The GNU system was developed to be 100% free software, and it is efficient with both 32-bit and 64-bit operations. This compiler can be used on Windows using MinGW or Cygwin Linux compatibility layers. Compiled executable files need a small DLL to run independently on Windows. It is mostly compatible with Clang standards. 

GNU C/C++ compiler (GCC, g++) can also be used by Visual Studio Code, Dev C++, Code Blocks, and CLion IDEs, and it can be directly used with Command Prompt or PowerShell. 


Tip 3: Use a professional compiler with IDE, like C++ Builder. 

  1. Consider a Modern C++ Standard When Coding

Should we consider a C++ standard when coding ? The answer is yes! We highly recommend you code with a CLANG-enhanced C++ compiler or as much as possible to CLANG standards. 

CLANG is a standard for C++ compilers (on C++ coding, compiling, debugging, etc.) considered to be production-quality C, Objective-C, C++, and Objective-C++ compilers when targeting X86-32, X86-64, and ARM. It is a new C/C++ compiler standard (C++98, C++11, C++17, C++20, C++23…) supported by the LLVM Compiler Infrastructure Project and has been a default compiler in recent years for most C/C++ compilers. This means that if you code for a CLANG compiler, most other IDEs and compilers of platforms will support your code without any changes. The latest C++17 standard is supported by the most C++ compilers. More information about core language features can be found here. C++ 20 is new and needs adaptation time.

C++ Builder and Visual C++ come with IDE and CLANG-enhanced compilers. GNU C/C++ and Objective-C are also listed as CLANG compilers. 

What does CLANG mean for a developer? If you develop your code in CLANG standard, whether focused on a single platform or multiple platforms, your code is compatible with other CLANG-enhanced compilers. You can easily move your codes from one compiler to another.

Tip 4: Code with a CLANG-enhanced C++ compiler, or code as much as possible in CLANG standards.

  1. Use Unicode Strings or Wide Strings

In College C++, char arrays are mostly given as alphanumeric text as below. 

[crayon-672a018f6609c384207231/]

Although used in very low-level code, strings held in this format (or a char*) are difficult to write safely and a common cause of bugs, including security issues.

In general, use string instead of char arrays as in the example below.

[crayon-672a018f660a4042997601/]

Unfortunately, in most code and databases, names, middle names, surnames, address texts, and so on are defined as fixed char arrays. An example from College-style C++ is below.

[crayon-672a018f660a6603317734/]

Note that most names are about three to eight characters (Jim, Ali, Chen, Mehmet); surnames and middle names are about the same. And many addresses are about 50-80 characters. Some applications have empty address fields while it is defined to be added. Yet these are fixed-length and inflexible, and the methods to deal with strings in this format are often unsafe.

With that in mind, the string usage below is much more modern.

[crayon-672a018f660a7707884826/]

std::string works with char, which means it is not a Unicode string that can support all languages in the world, but you can hold UTF8 data in a string, though it is not ideal (the data is not tagged as UTF8, and sequences of characters represent one human-language character.. We can easily change std::string definition to std::wstring definition.

Using string is very good if you are developing an application running with English texts or with ASCII text standards (in German, French, Spanish, etc.) because string consists of ASCII char arrays.

A wide string can be used in our example below.

[crayon-672a018f660a9723729597/]

Use L before the text quotes as below to support unicode formatting.

[crayon-672a018f660aa857537247/]

WideString represents a dynamically allocated sequence of wchar_t string of 16-bit Unicode characters and it is appropriate for use in COM applications. However, WideString is not reference counted. UnicodeString is a dynamically allocated string in unicode whose maximum length is limited only by available memory, it is more flexible and efficient in other types of applications.  If you are developing a global application, use Unicode strings.

The Unicode standard for UnicodeString provides a unique number for every character (8, 16, or 32 bits) more than ASCII (8 bits) characters. Unicode strings are being used widely because of their support for languages worldwide and their emojis. In Modern C++ nowadays, two types of strings are used: an array of chars (char strings) and UnicodeStrings (WideStrings and AnsiStrings are older, not compatible with all features now). CLANG|C++Builder|GNUC/C++|compilers, and IDEs are using this standard for GUI forms to support all languages to provide applications globally. More information about the structure of UnicodeStrings can be found here. RAD Studio, Delphi, and C++ Builder use Unicode-based strings—that is, the type of string is a Unicode string (System.UnicodeString) instead of an ANSI string. Use string or UnicodeString in C++ Builder as below,

[crayon-672a018f660ab908823634/]

Tip 5:  Use wide strings or UnicodeStrings in string formats to support worldwide language; avoid using fixed char arrays as much as possible.

Read part 2 of this blog post here

Read part 3 of this blog post here

Next, learn about how you can convert int to char C++ in this article about String Operations.

Exit mobile version