Proper implementation of multiple programming paradigms
The standard library provides algorithms and containers allowing the programmer to focus on the problem.
Third party libraries cover almost everything well (except the web).
Examples include boost, SDL, wxWidgets and many more. See this comprehensive list of open source C++ libraries.
C++ is not a strict superset of C.
Code that is valid C is not automatically valid C++.
For example, C++ offers many additional features compared to C. These include proper object orientation, anonymous functions, operator overloading, templates and many more.
Therefore, C++ uses many additional keywords that cannot be used as
identifiers anymore. These include
auto
, class
, namespace
,
new
...
#include <stdio.h>
int main() {
int new = 3;
printf("%d\n", new);
return 0;
}
Download valid_c.c
Wikipedia has more information on compatibility of C and C++
#include <iostream>
/* Hello World program written in C++. */
int main(void) {
std::cout << "Hello, World!" << std::endl; // output some text
return 0; // 0 means success
}
Allow to include
code declared elsewhere
#include <iostream>
/* Hello World program written in C++. */
int main(void) {
std::cout << "Hello, World!" << std::endl; // output some text
return 0; // 0 means success
}
// single line comment
int x = 5; // comments can be added at the end of a line
/*
* multiline comment
* ...
*/
Removed by the preprocessor
Serves a special purpose in C++ programs:
program execution begins with main(.)
#include <iostream>
/* Hello World program written in C++. */
int main(void) {
std::cout << "Hello, World!" << std::endl; // output some text
return 0; // 0 means success
}
Indicate success (0
) or a failure code to the calling
operating system shell
#include <iostream>
/* Hello World program written in C++. */
int main(void) {
std::cout << "Hello, World!" << std::endl; // output some text
return 0; // 0 means success
}
Executable program files are created from readable source code.
main.cpp
executable
Creating a C++ executable is a four step process.
Download hello.cpp
By default, all four stages (preprocessor, compiler, assembler
and linker)
are done
clang++ hello.cpp # or ` hello.cpp`
./a.out
Provide a name for the created executable (instead of the default
a.out
clang++ hello.cpp -o hello
./hello
Compilation can be done with either clang++
or
.
clang++ $INFILE -o $PROGRAM_NAME
$INFILE -o $PROGRAM_NAME
Stop after preprocessor and send output to stdout
clang++ -E hello.cpp
Creates assembler files (usually *.s
)
clang++ -S hello.cpp
*.o
filesmain(.)
main(.)
clang++ -c hello.cpp
clang++ -o hello hello.o