C++ Lesson 01 , Introduction to Programming

Google+ Pinterest LinkedIn Tumblr +

C++ Lesson 01

Introduction to Programming

Contents :
1 – What is computer programming (overview)
2 – Your first C++ program
3 – C++ overview
4 – Editors and compilers
5 – C++ reference documentation

First i want to say that if you want to become C++ programmer you will need to read some C++ books. At the end of this lesson i will post links where u can read how you can use Visual Studio or Eclipse for C++. I will post the name of some books that might help you durin your way in software development.

1 – What is computer programming (overview)

The basic answer to this question is : “Creating the sequance of instructions to enable the computer to do something”.

The more advanced answer will be “Creating the sequance of instructions. Use a compiler to translate this instructions for the computer”. In other words you write instructions or so called code to some text file using text editor or some kind of IDE (explain in 4 – Editors and compilers) then use compiler to compile the code or source code. The compiler translate your code to instructions that the computer can understand and do.

Every program and every programmer when developing some kind of software must use the following scheme. Lets call it “Programing Phases”.

There are four phases that the programmer must know when he develop software :

1 – Specification : Define a tasks/problems for your software. Here is the time to think about the software (what this software is about, who will use it, what the software must do and what he must not, define the kay subtasks). If you work with clients this phase will be esay because they will tell you the answers to all the above questions. But if you develop software on your own then u must define the tasks/problems first and the continue with the development.

2 – Design : Plan your solution. In this phase you already will know all the tasks/problems. Here you need to design the way you will solve them (for example: what kind of data structures you will use, find program algorithms for the problems u need to solve and so on).

3 – Implementation : Write the source code.

4 – Test and Debugging : Test your application for bugs and errors and if there is some use the debuger to locate and fix them.

2 – Your first C++ program

In this chapter i will show you the most basic C++ program used in all programming laguage books. The so called HELLO WORLD program. This chapter is not to show you C++ stuff but to show you how the program look. There is no need to understand the code, just observe its structure.

—————————————————————-
#include
using namespace std;

int main () {
        cout << “Hello World” << endl;

return 0;
}

—————————————————————-

This code will open a console and will print “Hello World” in it.

What the first two rows of the code are for?
The #include keyword is used as a some kind of directive. There is alot of ready to use C++ libraries that comes with the C++. You can use them any time you want. What the library is : this is text file with .h extension. The creators of the C++ language put thouse files in the language for the user use. They offer you ready to use functionality like printing text on the screen.

The one thing you need to do to use this functionallity is to #include the desire library. In the case of our program we want to print the text “Hello World” on the screen. To do this we need to include the input/output library in our program – the iostream library. When we do this we can use the iostream functionality

. One of the functions we can use is “cout” – we print text on the screen. There is other functions in the library too, functions for keyboard input for example, but we will see this on the next lesson. To move on with the code i will explay what the int main () { … } mean.

The C++ language like many other languages need an entry point for the program. The standart entry point in C++ is main () function. As we speak about function all C++ function have bodies – the { … } part. We put our program code between the opnenig “{” and closing “}”. For now we wont talk about the int before the main function and about the return at the end of the function body.

So for now we know this. We put the above code in text file save it as HelloWorld.cpp or if you use some IDE (Development enviroment like Visual Studio or Eclipse) you need to create a project and the create the .cpp file in the project, and put the code in the .cpp file.

When we start to look again at the code we know already that the first row include the iostrean libray to your program, that mean you can use all the library’s functions. We won’t discuse the second row using namespace std for now. You  must know that you need to write it there.

Next we know that the int main () is the program entry point and the { … } body is where we put our code (what we want the program to do). In the body we call the “cout “function of the iostram libray. This function print the text between the ” ” on the screen. The endl at the end meen new line so the next time we print something on the screen it will be printed at the new line.

If for example we write :

—————————————————————-

#include
using namespace std;

int main () {
        cout << “Hello World” << endl;
        cout << “Hello World” << endl;
return 0;
}

—————————————————————-

This will print :
Hello World
Hello World

We can change it to

—————————————————————-
#include
using namespace std;

int main () {
        cout << “Hello World”;
        cout << “Hello World” << endl;
return 0;
}

—————————————————————-

This will print :
Hello WorldHello World.

You need a little experiment to see what can u change and what is the printed message to understand it better.

Another think i want to disscus here is well firmated code. The code above is well formated. It can be easy read and understand. YOU MUST ALWAYS WRITE WELL FORMATED CODE!!!

The code like this is compleate mess :

—————————————————————-

#include
        using namespace std

int main () {
cout << “Hello World” << endl;
        return 0;}

—————————————————————-

YOU MUST NOT WRITE CODE LIKE THIS. There are entire books on this topic.

We will get back to iostream library at the next couple of lessons so if you dont understand compleatly the code in this chapter RELAX we will cover it again with more details.

3 – C++ overview

What is C++?
As we said C++ is programming language and we already know what programing language is from the first chapter.

C++ features :

1 – Support different platforms. This mean that u can write programs for Windows XP, Windows Vista, Linux, MAC, your CAR!, your Toster and so on.

2 – The C++ is Object Oriented language. We will cover Object Orienter Programming when we know the basics of C++.

3 – C++ is very fast laguage. This mean that your software will run faster softwere written in Java for example.

4 – Very powerful language. You can use C++ for any kind of software. You can start from Toster program, some very speecial machines can be driven by C++ software, ATM, Games, all kind of user software (for example : bank software, police … and so on you name it).

5 – C++ is hard to learn. It require TIME.

4 – Editors and compilers

Here i can recommend you to use as your editor/compiler Visual Studio or Eclipse.

Thouse are programs that offer you text editor and compiler + debugger. All in one. You already know what Text Editor is but for the newbie this is program that help you write your code and save, export and do some other things with it.

What the compiler is we said in the begining. But againthe compiler is “program” if it can be called program that translate your code file to the computer.

The debuger is the component that help you to find and locate errors, bugs, mistakes and fix them.

When you chose the editor/compiler program ask Google how to create project, start your program and all the technical things that you can’t do alone without help.

5 – C++ reference documentation

Reference Documention is the place where you can look for some new information about functions, libraries, data types and so on. I use http://www.cplusplus.com. Try it. For example try to find info about iostream library. There are examples too.

http://www.eclipse.org – This is the official Eclipse site where uyou can download it its free. You will need to download C++ version.

Excercises:

1 – Create a program that print your name and age on the screen

Will see you in the next Lesson.

Share.

About Author

Leave A Reply