GNU/Linux Programmer's Solver - C++ Language

Hello World program in C++. Basic structure, comments and string literals
PROGRAMMING C++ HELLO WORLD
#include <iostream>

/* This is a comment that
 * can be extended for several lines
 */

int
main (int argc, char *argv[])
{
  // This is a one line comment
  std::cout << "Hola Mundo!" << std::endl;  

  std::cout << "------------------------" << std::endl
        << "We can't define multiline strings" << std::endl
        << "directly, but we can compose them" << std::endl
        << "like this " << std::endl
        << "--------------------------" << std::endl;

  return 0; // Comments can also be at the end of the line
}

As can be seen from the previous code, a C++ program is very similar to its corresponding C version. C comments can be used as is, and C++ also allows us to define single-line comments, which begin with the characters // and end at the end of the line.

We see that here we have to use a different header file (the file extension is not necessary) and the way to write strings to the console is different, although constant strings are still represented the same way, between double quotes ". The declaration of the main function is identical to the one we used in C, and C++ also uses the character ; to end instructions and braces to group commands.

DID YOU KNOW

C++ is, in a way, a superset of C. This means that we can include C code within any C++ program without any issues.

In this case, to print to the screen, we have used the standard C++ stream cout. cout is actually a static object of the ostream class that is associated with standard output. In this book, we will not cover object-oriented programming, so if you don’t know what we’re talking about, it’s enough to remember that cout is an object that allows us to write to the screen and that it does so using the << operator.

In our example program, we are using the manipulator std::endl to introduce carriage returns into our strings. We could use \n as we did in our C program, but this is the standard way to do it in C++.

DID YOU KNOW…

While the output of the program will be identical using \n or std::endl, the latter forces the flushing of the buffers, or in other words, std::endl ensures that the string is displayed on the screen immediately. In most programs we will write, that is not relevant, but there are cases in which it should be taken into account.

Files containing C++ code use the .cpp extension, and header files normally .h. However, this is not mandatory and you can find older code using extensions such as .C (uppercase), .c++, .cc, or .cxx. Similarly, you can find C++ header files with extensions .H (uppercase), .h++, .hh,.hxx, or hpp, with the latter being the most used today, although .h can be used as well.

I’M A NEWBIE

Although the default extension for programs in C++ is cpp, the cpp program of the system has nothing to do with this language. cpp comes from C Pre-Processor or C Preprocessor and is the program responsible for processing the #include and other preprocessor directives. Preprocessor directives usually start with the # character, such as the includes we have seen previously.

C++, like C, is a compiled language, and to be able to execute our program, we first have to compile it. This can be done on GNU/Linux systems with the g++ compiler using the following command.

$ g++ -o hello hello.cpp

As with C, the make utility knows how to handle C++ programs and we can compile our program hello.cpp with it (assuming there is no hello.c in the same directory that would take precedence).

$ make hello
g++     hello.cpp   -o hello

The C++ language was developed by Bjarne Stroustrup of Danish origin, also at Bell Labs. C++, initially known as C with classes, was born as an object-oriented extension of the C language. The first version of the language was released in 1982. In 1985, the book “The C++ Programming Language” was published. As with its C counterpart, this book was the definitive reference for the language until official standards were defined. For quite a few years, each new version of the language was a small nightmare of incompatibilities. Fortunately, those times are long gone.

Summary

  • Compiled language
  • Extension: cpp/h (C/cxx/c++/h++/hpp)
  • Terminador de líneas: ;
  • Agrupa instrucciones con : {...}
  • Comentarios multilinea: Between characters /* */
  • Comentarios misma linea: //
  • Delimitador de cadenas: "cadena¨
  • Puedes imprimir textos con cout << str (Does not add carriage return)
  • No soporta cadenas multilínea nativamente
  • No soporta HERE-DOCS
  • Punto de entrada: main
  • Requires definition of all elements beforehand (include files to declare functions)

Related Posts

GNU/Linux Programmer's Solver - C Language

Hello World program in C. Basic structure, comments and string literals

Read

PROGRAMMING C HELLO WORLD

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Objective-C Language

Hello World program in Objective-C. Basic structure, comments and string literals

Read

PROGRAMMING OBJECTIVE-C HELLO WORLD

Return to Home Page