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!”.
<span class="kw">class</span> HelloWorld
{
<span class="kw">public</span>:
<span class="dt">void</span> PrintHelloWorld()
{
std::cout << <span class="st">"Hello World!</span><span class="ch">\n</span><span class="st">"</span>;
}
};
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.
<span class="kw">class</span> HelloWorldStatic
{
<span class="kw">public</span>:
<span class="dt">void</span> <span class="dt">static</span> PrintHelloWorld()
{
std::cout << <span class="st">"Hello World!</span><span class="ch">\n</span><span class="st">"</span>;
}
};
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.
<span class="kw">class</span> HelloWorldReturn
{
<span class="kw">public</span>:
<span class="dt">static</span> std::string ReturnHelloWorld()
{
<span class="kw">return</span> <span class="st">"Hello World!</span><span class="ch">\n</span><span class="st">"</span>;
}
};
You can either call the method directly with std::cout
.
std::cout << HelloWorldReturn::ReturnHelloWorld();
Or save it in a variable and print the variable.
<span class="kw">auto</span> 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:
<span class="kw">class</span> HelloConstructor
{
<span class="kw">public</span>:
HelloConstructor()
{
std::cout << <span class="st">"Hello "</span>;
}
};
<span class=“kw”>class</span> HelloDestructor
{
<span class=“kw”>public</span>:
~HelloDestructor()
{
std::cout << <span class=“st”>“World!</span><span class=“ch”>\n</span><span class=“st”>"</span>;
}
};
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
andstruct
are almost identical. The only difference is the members are defaultprivate
in classes, and defaultpublic
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.
See Also
- C++ Hello World
- C++ Smart Pointers: weak_ptr
- Default explicit equality operator
- Mistakes
- compile_commands.json with CMake