C++ Hello World with Classes

By , last updated September 27, 2019

In this article we will show you how to write C++ Hello World program using classes with constructors and destructors. We will create different beginner C++ programs that will output “Hello, World!” as an example.

Hello World

The most trivial example is to create a simple cpp HelloWorld file inside your C++ project and a class with the same name. Now we are going to call a method in this class that prints “Hello World!”.

class HelloWorld
{
    public:
        void PrintHelloWorld()
        {
            std::cout << "Hello World!\n";
        }
};

In this Hello World C++ code example, we have created a class “HelloWorld” with one public function PrintHelloWorld(). Public function means that other functions outside this class may call it directly.

The standard C++ library cout function will print the “Hello World!” message on the console window.

Next step is to make an instance of the class and call the PrintHelloWorld() method:

HelloWorld hello;
hello.PrintHelloWorld();

Put this code into your main() method and run the program.

The output should be “Hello World!”.

Static Hello World

The problem with the first example, is that you must create an instance of the class before you can call the method PrintHelloWorld. It doesn’t change the state of the class, so it’s sort of useless to instantiate a class, which doesn’t do anything.

This can be mitigated by using the keyword static before the function name.

class HelloWorldStatic
{
    public:
        void static PrintHelloWorld()
        {
            std::cout << "Hello World!\n";
        }

};

This method can be called directly by any other code.

HelloWorldStatic::PrintHelloWorld();

Return “Hello World”

Just calling a method to print Hello World! is not fun. Maybe you want to do some operations on the string before you print it. Then you have to return it from a method, and then print it.

class HelloWorldReturn
{
    public:
        static std::string ReturnHelloWorld()
        {
            return "Hello World!\n";
        }
};

You can either call the method directly with std::cout.

std::cout << HelloWorldReturn::ReturnHelloWorld();

Or save it in a variable and print the variable.

auto hello = HelloWorldReturn::ReturnHelloWorld();
std::cout << hello;

Hello Constructor and Deconstructor

One of the most important aspects of C++ and classes, is the concept of doing work when constructing and deconstructing classes. This enables a very important feature called RAII, which is short for resource acquisition is initialization.

In short, classes will do work when constructed, and they will clean up after themselves when they are deconstructed.

Given this code with two classes HelloConstructor and HelloDestructor:

class HelloConstructor
{
    public:
        HelloConstructor()
        {
            std::cout << "Hello ";
        }
};

class HelloDestructor
{
    public:
        ~HelloDestructor()
        {
            std::cout << "World!\n";
        }
};

And this usage where we call the deconstructor class first:

HelloDestructor dtor;
HelloConstructor ctor;

It will print Hello World!. Even though HelloDestructor (dtor) is constructed first, it doesn’t do any work until it is deconstructed. This example illustrates when the constructor and deconstructor is called.

Difference between C and C++

C++ was called C with classes before the name C++ was coined in 1983, when Bjarne Stroustrup released the first version of C++.

In C++, class and struct are almost identical. The only difference is the members are default private in classes, and default public in structs.

A class/struct can hold a number of members, and methods that operate on the members. That is the main difference between C and C++. C have structs, but they can only contain data, and no methods.

Conclusion

The examples here are just the surface on how classes can be used. There are many more nuances and details in either way, it might be inheritance or templates.

Next step is to learn about C++ Variables and variable scopes.

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.