Getting Started With C++

Getting Started with C++

Requirements:

1. Linux Distribution
2. A C++ compiler (G++ or clang)
3. Any text editor

My Setup:

1. Debian 10
2. g++ version 6.3.0
3. pluma

In this tutorial, i will be teaching you the basics of C++. C++ is a general purpose programming language that incorporates multiple paradigms. It supports procedural, functional, object oriented and generic coding. The major runtime implementations include LLVM Clang, as well as GCC. It was influenced by Ada, ALGOL 68, C, CLU, ML, and Simula. C++ was standardized by an International Organization of Standardization (ISO) group known as JTC1/SC22/WG21. It has published 5 revisions of the standard so far and is currently working on the next revision (As of today that revision is C++20) . They release the standard under the name: ISO/IEC 14882:YEAR . The latest revision was ISO/IEC 14882:2017.
Though it maybe similiar to C, there are still some differences . For example, C allows implicit conversion from void * to other pointer types, but C++ does not due to type safety reasons. C++ also defines new key words such as new and class, which may be used as identifiers in a C program. On the other hand, C99 introduced a number of features that C++ did not support , such as variable-length arrays, native complex-number types , designated initializers, and compound literals. To intermix the two languages , you must use an extern statement like such: extern “C” {/*…*/}.

Ok lets get into coding. Im gonna open up bash and create a new directory :

clim@debian:~$ mkdir Cpp
clim@debian:~$ cd Cpp
clim@debian:~/Cpp$ pluma main.cpp
#include <iostream>

int main(int argc, char **argv){
	std::cout << " Hello World!\n";
	return 0;
}

Lets dissect this line by line:

#include <iostream>

the iostream system header file defines basic input and output streams such a std::cout and std::cin . These are global objects that allow you to read and write to the console.

int main(int argc, char **argv)

the main function is user implemented , and defined by the C++ standard as either int main() or int main(int argc, char **argv). For more information, see section 3.6.1.2 ( 3.6 Start and Termination, 3.6.1 Main Function).

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

Ok so you might be wondering, "what is this std:: you keep writing?". This is called a namespace, and it is used for modularity . This prevents naming collisions. I prefer doing it this way, but you could also write:

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

Other tutorials will show you the "using namespace std;" line of code, but i believe this is poor practice. This forces you to use all the other objects within the namespace you dont need, and creates a problem for bigger projects who may have similar names as those in the standard namespace, such as vector , or map.

Next, you might be asking "Why are you using two less then signs?" That is an overloaded operator. In C++ you can override operators such as +,-,*,/,<>,,= and more. the std::cout is an output object, and the “<<" is like an append operation. Anything you append into std::cout will be placed on the terminal line. I use \n at the end similar to how i would create a new line in C , however there is another way to do this:

std::cout <<"Hello World!" << std::endl;

std::endl flushes the buffer and places your cursor on a new line.

return 0;

This is actually unnecessary due to the standards, however this just tells the program to end the program (it calls std::exit(int status_code)) with a value of 0. Ok so lets compile and run this:

clim@debian:~/Cpp$ g++ main.cpp -o hello
clim@debian:~/Cpp$ chmod +x hello
clim@debian:~/Cpp$ ./hello
Hello World!

I am using g++ as my c++ compiler, the -o flag tells the compiler what the name of the object code should be. I then use chmod +x on the program to tell the terminal that this file is executable. Lastly, I run the program using ./program_name . That ends the introduction of C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s