Get up to 80 % extra points for free! More info:

Lesson 1 - Introduction to the C++ language

Lesson highlights

Are you looking for a quick reference on C++ instead of a thorough-full lesson? Here it is:

C++ is a compiled language which results in C++ programs being very fast but harder to code:

Compiler language principle - C++ Basic Constructs

It's used mainly for applications that require a lot of performance (e.g. web browsers or AAA games) or specific system libraries, e.g. for sound or video.

C++ supports the object-oriented programming and can be considered a C language successor.

C++ lacks a Garbage Collector which automatically clears the memory used by our program in modern languages which makes it faster, but also more dangerous.

Would you like to learn more? A complete lesson on this topic follows.

Welcome to the first lesson of the C++ programming language course. We'll go through step by step, from the very beginning to the more complex structures, object models, pointers, and things such as working with files. With a little patience and persistence, you will become a good programmer.

First and foremost, it'd be appropriate to mention that the C++ language is quite complex and it's probably one of the hardest languages to learn. It combines the high speed of the older C language with the new object-oriented approach of the modern languages such as Java, C# .NET, or PHP. It's a very good choice for applications that require a lot of performance (e.g. web browsers or AAA games) or specific system libraries, e.g. for sound or video. If you don't plan to develop those apps, choose one of the modern languages mentioned above. If you are planning on developing that sort of application, you're going to become a real professional here, even though you may have a rough time getting there.

To fully understand the C++ language, we'll have to look to the past and get a good understanding of how programming languages have evolved over the course of time. Doing so will enable us to understand how C++ works.

Evolution of programming languages

1st generation languages ​- Machine code

Computer processors can perform a limited number of simple instructions, which are stored as a sequence of bits, i.e. numbers. In most cases, the aforementioned instructions are written using the hexadecimal system, so as to make reading them less of a chore. However, the instructions are so limited, that all you can really do is sum up addresses and jump between instructions. As you may already know, in the world of programming, one does not simply add two numbers together. What we do, is look at the numbers' addresses in memory and then sum them up (which takes multiple instructions). Here's what adding two numbers would look like in the hex:

2104
1105
3106
7001
0053
FFFE
0000

The instructions are given to the processor in binary. This sort of code is extremely unreadable and is dependent on the instruction set of the given CPU. I assure you, it is extremely nauseating to program in this "language". Unfortunately, every program must be compiled in binary format so that it can be executed by a computer processor.

Assembly language principle - C++ Basic Constructs

2nd generation languages ​​- Assembler

Assembler (ASM for short) is no simpler than machine code, but at least it's human readable! Here, the instructions have human readable text codes, so that people wouldn't have to memorize every single one of the number combinations. The instruction codes are later compiled into binary code. Adding two numbers up in ASM would go something like this:

ORG 100
LDA
ADD B
STA C
HLT
DEC 83
DEC -2
DEC 0
END

It's a bit more human-readable, but most people, including me, would still have no clue how this program works.

3rd generation languages

Third generation languages finally give a good amount of abstraction of how the program is seen by the computer. Rather than forcing us to adapt to the computer's arcane way of thinking, the languages focused a bit more on how we see the program. Numbers were then perceived as variables and code had an almost "mathematical-notation" sort of aesthetic.

Adding up two numbers in the C language would go like this:

int main (void)
{
    int a, b, c;
    a = 83;
    b = -2;
    c = a + b;
    return 0;
}

Pretty much anyone could assume what this program does just by looking at it. It sums 83 and -2 up, and stores the result in a variable named c. The main advantage third generation languages had over all of the previous languages was high readability. You surely can imagine what a programming revolution the C language caused, even though it wasn't the first of its kind.

The C language was designed by Dennis Ritchie, the creator of the UNIX operating system, in the 1920s. In fact, the entire UNIX kernel is written in the C language. This is probably why the language is so popular, and it also points to the extreme performance of the language. C++ builds upon the C language. The ++ command performs an integer value incrementation in the C language which makes the name mean something like "the better version of C".

Compiled languages

Compiled languages have their source code in a language that people can fully understand. However, the source code must still be translated into machine code so that it can be executed by the processor. This translation is provided by a compiler, which compiles the entire program into machine code.

Compiler language principle - C++ Basic Constructs

Object-oriented programming

Object-oriented programming (OOP) is the key feature which C++ adds to C. Basically, the C++ language is the C language with more syntax added. This makes C++ able to compile all C programs and use C libraries as well. OOP enables us to make large programs extremely readable and maintainable by dividing them into communicating objects. This makes C++ a both fast and modern language, however, it's still considered a difficult one and as one which is used mainly for specific applications.

The same program for adding 83 and -2 using OOP would look something like this in C++:

#include <iostream>
using namespace std;

class Calculator
{
public:
    int add(int a, int b);
};

int Calculator::add(int a, int b)
{
    return a + b;
}

int main(void)
{
    Calculator calculator;
    int c = calculator.add(83, -2);
    cout << "Result:" << c;
    cin.get();
    return 0;
}

Notice that it isn't a pure algorithm anymore. Instead, we're adding another abstraction layer by calling functions on an actual calculator object. Of course, it doesn't seem very logical or efficient for a task as simple as adding 2 numbers. However, it makes complex algorithms way more readable. We'll skip OOP in the C++ basics course since our main focus will be the basic C++ syntax. There's a special course afterward whose aim is specifically OOP.

Garbage Collector

What C++ lacks is a Garbage Collector which automatically clears the memory used by our program in modern languages. C++ doesn't have anything like that which makes it faster, but also more dangerous since it's up to the programmer to clean up after themselves. This is the most significant difference that C++ has when compared to modern languages, and it's both an advantage and a disadvantage. You get speed and you give up code simplicity.

Now, we know what we're going to work with. In the next lesson, Visual Studio and your first C++ console application, we'll install the necessary tools to create our first C++ program.


 

All articles in this section
C++ Basic Constructs
Skip article
(not recommended)
Visual Studio and your first C++ console application
Article has been written for you by David Capka Hartinger
Avatar
User rating:
3 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities