1. Basics
The C++ programming language
C++ (/ˈsiː plʌs plʌs/) is a general-purpose programming language. It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation.
(from wikipedia)
History of C++
Origins
Assembly -> BCPL -> B -> C -> D
-> C++ -> Java
Algol -> Algol68 -> C#
Fortran -> Simula67
Timeline
- 1980 Bjarne Stroustrup starts working on C with Classes
- 1983 The new language is named as C++
- 1985 Aleksey Stepanov works on Generic programming
- 1990 The language has exceptions
- 1990 The Annotated Reference Manual (ARM) (book from Stroustrup and M. Ellis)
- 1991 ISO standardisation process starts
- 1994 The Design and Evolution of C++ (book from Stroustrup)
- 1998 C++ standard ISO/IEC 14882:1998
- 2003 Minor bugfixes (C++03 standard)
- 2011 Major language revision (C++11 standard)
- 2014 Minor revision (C++14)
- 2017 Major language revision (C++17)
Design goals of C++
Type safety
C++ is a statically, strongly typed programming language The compiler decides the type of all (sub)expression in compile time
In run time: pointers, conversions, Object-oriented constructs brings dynamism into the static type system.
Resource safety
- Not just memory! All resources (files, sockets, locks, etc.)
- No garbage collection by def. (but can implement)
- Use the RAII (Resource Acquisition Is Initialization) idiom
Many beginner makes resource errors.
Performance
- Direct access to HW resources. No virtual machine
- High performance trading, phone exchange systems
- Low energy cosumption (Mars rover)
C++ programs does not guarantees high performance! They gives control to the programmer to decide on performance-related issues.
Predictability
Orthogonal features should work well together. Capability to safety implement large systems (2-10 million eLoC).
Learnability, readability
Lots of problems in earlier versions. C++11 version goes from expert-friendly to novice-friendly.
Compiling, linking
preprocessing compiling linking executing
header source object library
a.h
b.h -> b.c -> b.o ---------|
-----> a.out (b.exe)
e.h | |
f.h -> d.c -> d.o ---------| |runtime
| |
g.h -> g.c -> g.o | |
h.h -> h.c -> h.o -> h.a (h.lib) |
archive |
i.h -> i.c -> i.o |
j.h -> j.c -> j.o -> j.so (j.dll) --|
shared object
First C++ program: hello world
1
2
3
4
5
6
7
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}
Compiling, linking, executing
Compiler errors, warnings
If we make a syntax error, the compiler emits error(s):
1
2
3
4
5
6
7
8
9
10
11
/*
* BAD VERSION !!!
* Missing semicolon
*/
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl // missing ;
return 0;
}
If there is a syntax error, do compiler do not generate object code. When we have a warning, the compiler does generate object output.
1
2
3
4
5
6
7
#include <iostream>
int main()
{
int i = 1;
return 0;
}
Warnings can be serious things in C++, you should treat them as errors unless you are absolute sure in the opposite. Even there, it is a good habit to write warning-free code.