Program Execution Model
Code is compiled before it in run, the same as Java and C.
We use the gcc compiler to deal with C++ compilation as follows:
g++ hello-world.cpp -o hello-worldThis is the standard way to compile C++. It automatically treats files with extensions like .cpp, .cc, and .cxx as C++ and, crucially, automatically links the C++ Standard Library (libstdc++).
Basic Syntax
Most of this is similar to C, with headers that can be used to include packages and a main() function that is the core of the execution.
Basic I/O
Here is a basic example of outputting a string:
std::cout << "Hello World"The above line prints the text to the console.
You can remember << as pushing the text into the output stream (i.e std::cout in this case)
To add a newline, we can add the following to the end of the string:
std::cout << "Hello World" << std::endl;In fact, this is a general idea of how we can build streams using << where we can chain the operator to add multiple objects to the stream.
Now, for inputs, we can use std::cin >>. Here is an example of how this fits into a program.
#include <iostream>
int main() {
int x;
std::cin >> x;
std::cout << "You entered: " << x << std::endl;
}Note that the direction of the arrows (>>) for std::cin is now reversed compared to <<. You can remember as data flowing into x.
Variables
There are some universal programming language ideas here, which overlap with C. However, while both languages use variables as named memory locations to store data, C++ introduces significant new types and rules for how and where variables can be used.
Here are some differences to keep in mind:
- Reference Variables: C++ introduces references (
type&), which act as aliases for existing variables. C does not have references and must use pointers (type*) for similar functionality. - Declaration Placement: In older versions of C, local variables usually had to be declared at the very start of a block (before any statements). C++ (and modern C99+) allows variables to be declared anywhere in the code, typically right before they are first used.
The rest of the basic ideas about variables such as initialization, declaration, arithmetic operations and data types are the same as C.
Statements
The statements and operations in C++ are a super-set of what is available in C. C++ extends the set significantly with new operators, exception handling, and overloading.
New Operators in C++:
- Scope resolution (
::) – lets you access global variables or class members. - Member access (
>*,.*) – for pointers to members. - Dynamic memory (
new,delete) – replacesmalloc/freein idiomatic C++. - Type casting operators –
static_cast,dynamic_cast,const_cast,reinterpret_cast(more controlled than C’s(type)cast). - Overloading – operators can be redefined for user-defined types in C++.
New Statements in C++:
- C++ adds
try/catch/throwfor exception handling, which C does not have. - C++ also allows range-based
forloops (since C++11), which don’t exist in C. - C++ supports
newcontrol statements likeusingfor namespaces and type aliases, absent in C.
Increment/Decrement Nuance:
- Both languages have
++and-, but in C++ these can be overloaded for custom classes, while in C they only apply to built-in numeric types.