Introduction to C++ part 1

Hello!

So here is the start for my C++ tutorials. The reason for making these posts are to empower what I have learned so I do not forget the codes that I worked hard to learn, to learn new things as I go on to more advanced areas in C++ and to learn to instruct others. My aim is to learn to be able to run a local program that can install, save and retrieve data.

Before I dive in to the first program, I will go some background information for the environment that needs to be installed. There are many different compilers all with their good and bad sides. Some of the compilers are free and in some you have to pay for the software monthly or one time fee depending on the compiler and the company/individual that provides them. There are many better compilers than what I use in my tutorials, but I will just do it for the sake of the familiar environment. You can choose any other compiler you want because C++ should run the same in any other platform as well.

I personally use bloodshed DEV C++ and here is the link to their websiteYou install it like any other program and start it by double clicking on it where the program should look like this. When you start the program, a tip bar will open up. You can either read it or just close it.

front

Go to the File –> New –> Source file from the top left of the program.
I think that you don not need to know about making a project folder so I will not go into that subject but the Source file should look something like this.

front

The learning material, that we are going to use for this tutorial is from the following pages.

http://www.cplusplus.com/

http://www.learncpp.com/

http://www.cprogramming.com/tutorial.html

http://www.cs.wustl.edu/~schmidt/C++/

Now I will jump in to some theory like history and development of C++, because I think it is very important for everyone to know some background information of their own area of study. C++ was developed by Bjourne Stroustrup and the language itself is combination of both High and low level language. Meaning that the language is made up of language know to us humans, that was made up to ease the job of the programmers by implementing basic rules/laws on how to build it in a way that the computer is able to understand it. The low level language instead is a language that is said to be closer to the hardware meaning that the code is bunch of numbers and alphabets. Its much more harder to be understood by ordinary people like you and me but the code is said to be more efficient, better and faster.
Without going to the whole history of different programming languages and the history of the computers I will skip ahead back to essentials of C++.
For more information please visit the link below or Google it. Wikipedia also has a good information base about C++.

http://www.stroustrup.com/#invention

Basically C++ is a good programming language with  C and C# if you want to be able to have more control over the memory and be closer to hardware. C++ is also a very logical language where instead of learning abstract laws and rules of how to compile your program, in C++ the way of compiling a code is meant to be logical and algorithmic meaning that it’s very easy language to work your codes on if you have brains for some mathematical thinking. That is how I think of C++. It all depends on what do you want to achieve with your code. I recommend you to Google different companies that use C++ in their work and I guarantee you that you will be surprised of the amount of companies that work with it.
Different languages have different attributes that are better to work on in different fields. For business applications that work on the internet, the most popular language, I think is java and incorporation of different technologies with it. Then again, you will see that there is programs that work for example like in satellites that were written in Java.

Anyway, enough with the gibberish talk. I will add theory as we will move on with the tutorial as I will learn more (again) about the language that I have started to forget.
For our first program we will write a “Hello World” program like in every other coding language to see if our compiler and environment is installed properly.

In the first line of our first program we will write

#include <iostream> // A header that contains how cout (“ceeout”) work’s
iostream is basically a library and this library contains code to handle the I/O and you should always the the using namespace std in your program with this.

That according to a website means

"#include <iostream>Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler’s preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.”

It means that you are able to print text on the screen in the C++ program and take input from the user in the form of

cin >> variableName;

Next we will have to add

using namespace std;

“All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.”

and then we add

int main()
{

//The program code will be written here

{

This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it – the instructions contained within this function’s definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.

Now the reason for the copy paste fest is the reason that, in these day’s there so much misinformation among people about the real functionality of the codes and what they really mean, that I thought that it would be good to just paste the description. I will later add the original manual description here for you to get a better idea of what is really happening. Please under no circumstances make examples with C++ functionality with real world object because, in my opinion it’s better for student to know what the codes really mean than to teach them with drawn pictures of colorful boxes with = signs and give a story for all this. It is a good way to teach the rules of the compiler but in the long run, it will just make students more confused when they will get into more complex programming tasks.
Statements like “It will shove the value 1 inside this box named i that can take values between x-y” do not help the students to understand what really happens in the machine level so that they would understand the meaning of the real objective of programming that is creating programs, that work efficiently as possible with good programming standards.

Anyway lets get back to our code. The next line will contain the functionality that will print “Hello World!” on the screen.

cout << “Hello World!” << endl;

Now what happened in the code above is that the C++ will print the code with the
C out function where the text between the “quotes” will be printed and note that you have to enclose the “Hello World!” between the << code line << to make it work because it means that it will start a new function with the cout. In the end we have endl meaning End Line that is equivalent of new line ( \n or println() in java ). Later we will learn how to do mathematical operations in this same line so it’s important for you to understand to be able to segment the code.

"cout is an object of class ostream that represents the standard output stream. It corresponds to the cstudiostream stdout.

By default, most systems have their standard output set to the console, where text messages are shown, although this can generally be redirected.

Because cout is an object of class ostream, we can write characters to it either as formatted data using for example the insertion operator (ostream::operator<<) or as unformatted data using the write member function, among others (see ostream). “

For example

cout << “Hello World!” << ” You suck! ” << 1+1 << endl;

will print on the screen

Hello World! You suck! 2

Where the 1+1 is a mathematical operation when it’s not enclosed in the “quote” marks.
So you now should understand that the quote marks will turn the line into text based for example

cout << “1+1” << endl;

would print on the screen

1+1

After the text you want to print on the screen you have to write

system("pause");

That will pause the function/code in somewhere of the code where you have to press a button for it to continue to function. We will use it in our code to pause the code so we can see it on the command line. Otherwise without the pause, the compiler will finish it in a millisecond and we wont be able to see the “Hello World!” on the screen. After Pause write

return 0;

Meaning

“The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.”

After all this your code should look something similar like mine below

#include <iostream>
using namespace std;
int main(void)
{

cout << “Hello World!” << endl;
system(“pause”);
return 0;
}

If you were able to see the hello world text, you have successfully compiled your first program. Otherwise, you did something wrong in the process for example forgot semicolons or added too many semicolons or there is something extra in your code. The output should look something like below depending again on the values you gave.

c++

As you can see there is something written in Finnish language “Jatka painamalla mitä tahansa näppäintä . . .” meaning, “Continue by pressing any key . . .”
The reason for that is the pause function that we added. Otherwise you would not have had the terminal open. Some might say that pause is a bad programming style and that you should never use it but for educational purposes, it do not think that there is any major reason why we should not use it. Always remember to save your work in different versions. The end of the C++ files is called .cpp.

Feel free to comment. I will update this section constantly with more theories and content.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s