Programming Windows: Hello, C++ (Premium)

C++ was created by Bjarne Stroustrup as an object-oriented extension to the C programming language. Stroustrup began his work on the language, which was initially called “C with Classes” in a nod to a foundational object-oriented programming (OOP) concept, the class, in the late 1970s. But over time, he changed the name to C++, where “++” represents the increment operator in C. So C++ is “C plus 1,” or, more loosely, “the next C.”

As you may recall, in the book The C Programming Language, authors Brian Kernighan and Dennis Ritchie (the latter of which invented C) created a simple introductory program called hello, world that showed off some key aspects of that language. It looks like so:

#include <stdio.h>

main()
{
    printf("hello, world\n");
}

In keeping with this tradition, Stroustrup published his own book called The C++ Programming Language to describe his creation in 1985. And he also wrote a similar introductory application for his own book and language which he called Hello, World!. It looks like this:

#include <iostream>

int main()
{
    std::cout << "Hello, World!\n";
}

This code is, I think, less readable than the C version of hello, world. But I assume that even non-programmers, looking past some of the less obvious bits, can see that this application would print the words Hello, World! to the screen. And so it did.

And still does. This program, just like the original C version of hello, world, works today with the latest version of Microsoft Visual C++, part of Visual Studio 2019, without requiring any changes. To check this, I used the same methods as I did previously in Programming Windows: Hello, World (Premium). I created a file called hello.cpp with Notepad, saved it to the desktop, and created an executable file called hello.exe using the command line version of the Visual C++ compiler and linker.

Like so.

Then, I typed hello and tapped Enter. The words Hello, World! appeared as expected. Fun.

I promised not to teach you how to program, and I won’t. But a quick look at this short code block, and how it differs from straight C, might be of interest.

Syntactically, things changed quite a bit between C and C++. This is because C++ uses objects to handle even basic functionality, such as input and output to a console.

#include <iostream>

That said, the #include preprocessor directive works as it did in C: It takes every line of code in the included library, now called iostream, and combines it with the current source code file. And while iostream does for C++ what stdio.h does for C—it is the language’s standard library for input and output, since this functionality is not built-in to the core language—it is object-oriented in nature and not just a flat list of functions.

std::cout << "Hello, World!\n";

On that note, though cout (“see out”) is an object, it performs similarly to printf, a basic function, in C: It represents the standard output, which in this case is the console. But the std bit may seem particularly odd, as will the :: operator. These items indicate the namespace—basically, the scope—of the cout object. That is, it’s possible that other code that you write or use (via an include) could also have an object called cout, so using the fully-qualified expression std::out tells the C++ compiler to use the version found in the standard library namespace (std).

This is really bad form, but you could write something semi-nonsensical like so:

#include <iostream>

int main()
{
    int cout = 10;
    std::cout << "Hello, World!\n";
    std::cout << cout;
}

And it would display the following when executed:

Hello, World!
10

Point being, there are two different objects named cout in this code. One is the standard output object and one is an integer variable.

<<

Finally, << is what’s called an insertion operator. It “inserts” the data that follows it (The text “Hello, World!” plus a newline character) into the stream object that precedes it (std::out, the standard output object, or the console). And yes, there is a cin input object with which you would use >> to obtain input from the user (via the console).

Sound ponderous? It is: C++ is object-oriented, so there’s some overhead compared to the simpler C example, and most of the functionality that would be useful for even the simplest of applications requires at least one external library: C++ itself is lean. As Stroustrup pointed out, C++ is best used for writing “device drivers, embedded systems, and other code that needs to use hardware directly … to do higher-level stuff, to build complete applications, you first need to buy, build, or borrow libraries providing appropriate abstractions.”

As we’ll soon see, that is exactly what Microsoft eventually did in an effort to use C++ to overcome the nightmare of creating Windows applications with C and the Windows API: It created a set of C++ class libraries that abstracted the Windows API so that developers could more easily create and maintain ever more complex and full-featured Windows applications.

We’ll get to that work soon enough. But it seemed to make sense at the time because using objects makes particular sense for GUI applications like those that run under Windows. When you think about it, everything in Windows—the windows themselves, controls, and so on—are objects. They have properties, or attributes, like their size, position, and value. And they have events to which they must respond.

But there’s a reason why more recent object-oriented C derivatives like Java, C#, and Swift are “C-like” rather than truly based on C. They are higher-level, safer, and less difficult to read, use, and maintain. As Stroustrup noted, humorously, “C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows your whole leg off.” (That terrible code above with two cout objects is a good example of blowing your whole leg off from a readability perspective.)

Regardless, C++ has stood the test of time and is seen as more low-level today, much as C had moved into that role by the early 1990s. (As Charles Petzold has noted, all programming languages become more low-level over time.) C++ is still widely used, and it is now considered the low-level language of choice for game developers and others who need to get close to the metal on virtually any platform, including Windows. But before it settled into this comfortable role, Microsoft and some of its competitors were creating C++ class libraries for developers looking for a better way—an object-oriented way—to create standard Windows applications.

And the result was something called the Microsoft Foundation Class library, or MFC.

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Thurrott