Exploring the Cpp program
Cpp program language is based on the C language syntax, and it is extending its features, lets see what is the minimum Cpp program.
Now, because C++ is based on C it’s syntax is basically the same. Let’s take a look at a C++ version of hello world program. That’s in C++.
// hello.cpp
#include
using namespace std;
int main( int argc, char** argv) {
cout << "Hello, World!\n";
return 0;
}
when you compile it and run it run it, you will see it does exactly the same thing. And there are a few distinct differences.
Pre-processor Directives
#include
You'll notice that the include directive includes something called IO stream, and it does not have a dot H at the end. It's traditional for C++ include files to not have the dot H.
Namespace
using namespace std;
We also see a line called using namespace standard. Namespaces are a concept that is in C++, but not in C. And we'll learn about name spaces later on in the topics. But Suffice it to know at this point that the objects in the standard library.
The C++ standard library are all in the standard name space. So for example cout is actually in the standard namespace. If we take out that line that says using namespace std, then we will get a syntax error (Symbol could not be resolved).
So we need to declare that cout is in the standard name space
int main(int argc, char** argv) {
std::cout << "Hello, World!\n";
return 0;
}
And we do that like this. std and two colons. And that declares that cout is in that standard namespace. Now, if I save it and run it, it continues to run fine.
So I tend to have the using namespace line towards the beginning of all of my examples in C++ because it makes it easier. I can just type cout instead of std::cout, especially if there's a lot of objects in the code that you use the standard namespace.
Main Function
You'll also notice that the text is output with the cout object rather than using printf. And see how it is an object. So it's using an operator here. It's actually overloading the left shift operator << in order to send text to the output stream.
That's become a common thing for input and output streams, and actually the IO stream library does that a lot with the various input out stream classes. In fact, you can use printf if you like. And I'm just going to put that in here instead, say, printf and call. the printf function.
#include
using namespace std;
int main(int argc, char** argv) {
printf("Hello, World!\n");
return 0;
}
Even though in the gcc library, printf is declared in IO stream, and it's actually even declared in the standard namespace. so this program will run just fine and it will print the hello world message.
But It's more common when you're using printf to go ahead and include the stdio.h file or it's C equivalent cstdio. And so C plus plus equivalents of dot H files have a C at the beginning and no H at the end to designate that they're part of the C standard library and not the C++ standard library.
#include
int main(int argc, char** argv) {
printf("Hello, World!\n");
return 0;
}
And then I no longer need this using namespace. I can save this and run it And you'll notice that we're basically running exactly the same program that we had in the hello.c file, but we're running it in a hello.cpp file and compiling it with the C++ compiler.
$ g++ hello.cpp
$ ./aout
Hello, World!
So it is possible because the entire C language is included in C++ to simply write C programs in a C++ file. Obviously, if you have C++, it's a good idea to make use of its various features, but it's also good to know that your regular C code will run just fine in C++.