I chose C++ instead of Python

Congrats, now you don’t need to rename this topic to “I chose C# instead of Python”. :joy:

#include iosream should be #include <iostream>.

using namespace std; like this is frowned upon. It dumps everything from the standard library into the current scope, which causes namespace pollution, naming conflicts, etc.
You should instead only use it in the scope of a function or method – here main() –, or even better use fully qualified names (e.g. std::string) or “import” only what you need (e.g. using std::string;).

You should also note that std::endl forces a flush of the output buffer, which is relatively expensive and rarely needed. It’s better to use the newline character \n, for instance std::cout << "your name\n";.

Furthermore, if the character size is fixed, I’d use a const char* name = "Edward"; (i.e. constant character pointer) instead of a std::string for performance reasons.
std::string, much like other dynamic containers, comes with some overhead, because it can be resized. Generally speaking, internally it has a certain size (capacity) and when it requires more space for new characters, it needs to copy everything to a new larger chunk of memory (expensive). This has the benefit of being “dynamic”, but comes at a cost.
For your case this doesn’t really matter, but it’s something to be aware of. The same goes for std::vector for instance.

You can also auto-format code here by putting it between ```code```, so that it doesn’t look like a mess.

3 Likes

Thank you so much for your guidance.

I have studied the std:: rule, but I have not had any exercises on this ground. Just minutes ago, I was taking note of the instruction:

// using namespace std; - Remove this line

int main() {
std::string greeting = “Hello”;
std::cout << greeting;
return 0;
}

I copied the original code from a website, took it into the Dev-C++ editor, and added some statements to it and removed some.

The original code had only int variables, but I added string variables to it so that it takes the form of a new code.

In addition to studying the lessons on the “W3Schools” website, I refer to other relevant websites, find example codes, and review their structures to learn more things. That helps me to learn C++ faster and more deeply. Furthermore, I try to memorize the keywords, operators, functions, data structures, and other lists as much as I can to progress faster.

I know I’m at a preliminary stage, but some questions pop up in my mind about the future that I think I should know now.

I know C++ is a back-end language that works in the background of systems (close to the machine). What interface (API) is there to bring it onto the screen?

For example, I read somewhere that C++ is not suitable for GUI and cannot create windows or widgets like Python, C#, or Java by itself, so how do the hidden operations of C++ code appear on the screen?

That’s not true. None of these languages can create operating system windows or graphical user interfaces completely “out of the box” without relying on libraries. Even in Python, you’d typically use something like PyGame (which itself is largely written in C) or another external framework.

If you want to interact with a compiled C++ executable, the most immediate and straightforward way is through the terminal—just like many command-line tools such as ffmpeg, vim, or htop.

If you want a graphical user interface, that’s a different story. There are libraries such as raylib, openFrameworks, SDL3, SFML, and GLFW that let you create OS-specific windows, render graphics, and handle system input/output like keyboard, mouse, audio, and sometimes networking.

For UI components specifically, there’s Dear ImGui and similar libraries that help you build interactive interfaces.

Strictly speaking, you could also do all of this from scratch in C or C++, but that requires working directly with the operating system’s APIs and is considerably more complex.

To be clear: creating a window involves interacting with the operating system, since the OS manages windowing. The graphical interface inside that window is ultimately just rendered images and text that respond to user input.

1 Like

Do these libraries belong to C++ itself, or are they separate and independent programs that C++ connects to?

I read a bit about another program or software named Qt. It has been said that it interacts with C++ to create windows and widgets. I am not familiar with this program and have no experience with it. Are the mentioned libraries something like Qt or are they different?

I have another question to better understand how C++ deals with UI or GUI.

I imagine, there is a C++ application for a medical testing device. Well, C++ operates and controls the device hardware components. Which program brings the results of the C++ calculations onto the device monitor as text?

They are independent frameworks written in C or C++. They are not separate “programs that C++ connects to,” but rather libraries — meaning collections of reusable code that you include in your own program.

When you compile your C++ project, your code and the library code are linked together (either statically or dynamically) into the final executable.

Yes, Qt is another GUI framework for C++.

It’s not exactly that “C++ controls the hardware.” C++ is a programming language. Your C++ code is compiled into machine instructions that the processor understands (ultimately binary like 00010101…).

Your compiled program runs on the device and is responsible for:

  • Performing calculations
  • Interacting with hardware
  • Rendering output to the display
  • Handling user input

Typically, the hardware manufacturer provides an API (Application Programming Interface) that lets you communicate with specific components (sensors, displays, motors, etc.). You include those libraries in your C or C++ code and compile everything into a binary that runs on the device.

A good example is an Arduino microcontroller. You usually program it in C/C++. If you connect an Adafruit display, they provide a library that lets you draw text and graphics to the screen. The Arduino platform itself provides libraries to access GPIO pins and other hardware features.

In short: there is no separate “mystery program” that displays the text. Your compiled program, using the appropriate hardware libraries, is what renders the output to the screen.

I think you’ll get a much better sense of this once you learn about the relationship between source files (*.cpp) and header files (*.h or *.hpp), and how they are compiled and linked on your platform.

It’s worth trying to compile a small program manually in the terminal at first, before relying on the conveniences an IDE provides. That way, you’ll gain a clearer understanding of what’s actually happening behind the scenes.

1 Like

Thank you very much

Very nice and clear explanation :slight_smile:

I read in an article that frameworks and libraries have different processes in programming and are vice versa. In a script, it is the code that uses libraries, whereas a framework works in the the opposite direction and uses code to do something.

Are raylib, openFrameworks, SDL3, SFML, and GLFW frameworks or libraries?

It’s worth trying to compile a small program manually in the terminal at first, before relying on the conveniences an IDE provides.

How do I do that, when terminal has no compiler?

Honestly, the line between “library” and “framework” is blurry. I use the terms interchangeably. I might be wrong though.

It depends on your platform. macOS and Linux come with GCC installed, so you can compile a simple source file (i.e. main.cpp) in a terminal with:

g++ -std=c++20 -Wall main.cpp -o main

-std=c++20 specifies the C++ standard that you’re using, here C++ 20.
-Wall provides standard useful warnings (that can be used to debug your code).
GCC is a great C compiler, but it handles C++ as well.

This then outputs a platform-specific binary called “main” that you can execute with:

./main

On Windows, it might spit out a main.exe?

I haven’t compiled anything on Windows, but I believe you first have to chose and install a compiler.
A popular choice for beginners seems to be MinGW-w64, which packages the GNU Compiler Collection (including g++) for Windows.

Here is a great YouTube playlist by The Cherno that covers some of the C++ basics:

Another great channel is CppNuts for more bite-sized videos about the standard library (std::) and other random programming stuff:

Here’s a cool video from Pezza on how they design their custom UIs in SFML (since you seem interested in GUIs):

Yesterday, kavan posted a beyond cool video, where they did a quantum-level atom simulation—with the electron as a wave in constant superposition—in C++ with SDL and a custom raytracer for 3D-rendering:

Enjoy. :slight_smile:

1 Like

I am very grateful for your instructions and enlightenment :slight_smile:

Sadly, for some reason, I cannot access YouTube for now. So I cannot watch the videos.

I haven’t compiled anything on Windows, but I believe you first have to chose and install a compiler.

If I install GCC or MinGW-w64 on my system, will the terminal be able to recognize and use it to compile code?

Of course, I am currently using the Dev-C++ IDE.

From the start, my operating system has always been Windows.

After installing one of those compilers, can CMD compile more complex C++ scripts? It is just a question for understanding.

See here the mess already starts. If you want GCC for Windows I would use TDM-GCC.
Buf if you are more serious with GCC you should probably go for WSL2 and create a Linux subsystem. Then you are using C++ with Linux on your Windows system!
For Windows also MinGW is quite famous. It comes with a dedicated bash shell but its bloated. Now if you use CMake as built chain on Windows you will be braindead in 2 weeks. Maybe on Windows stay with Visual Studio and go for Microsoft.

1 Like

Sure, modern compilers are beasts that can optimise and handle all sorts of complexities.
If you’re happy with your IDE, simply roll with that for now, even if it abstracts some of the involved processes away.
Since you’re a beginner, it’s probably best to get accustomed to the language basics first.

1 Like

Thank you a bunch, sir

The information could help me learn valuable things about working with the giant machine language, C++, and the relative complementary programs.

Thank you again :rose:

1 Like