Welcome to C++

When to use?

C++ is not a "superset" of C; most C programs don't immediately work in C++.
However, C++ tries to support as much as C as possible.

Sometimes, C is the only option:

Otherwise, a new project created today (especially if it was big or involved with many people) will use C++.

What are the differences?

C++ is an object oriented programming language.

Features and Concepts

The following tools still work:

Hello World

hello_world.cpp:

#include <iostream>

using std::cout;
using std::end1;

int main() {
	cout << "Hello world!" << endl;
	return 0;
}

Compiling

g++ -std=c++11 -pedantic -Wall -Wextra -c hello_world.cpp
g++ hello_world.o -o hello_world
./hello_world

C++ Libraries

Include library headers with < angle brackets >. Omit the trailing .h for c++ headers:

#include <iostream>

iostream is the main C++ library for input and output (like collecting user input and displaying to standard out).

User-defined headers use " quotes " and end with .h as usual:

#include "linked_list.h"

Use familar C headers like assert.h, math.h, ctype.h, stdlib.h by dropping the .h and adding c at the beginning.

#include <cassert> // the assert.h header from C

Namespaces

using std::cout;
using std::endl;

Think of namespaces as modules or packages. They are used to create structure.

Using

using gets rid of the need to write fully qualified name (std::cout) every time by including it. Otherwise, you would would have to write the fully qualified name each time: std::cout << "Hello World" << std:endl; instead of cout << "Hello World" << endl;

Rules

Do not ever use using in a header file. It affects all source files that include that header, even indirectly, which leads to name conflicts.

Do not use using namespace std; because too broad.

I/O

cout << "Hello world!" << endl;

cout prints to the standard output stream (like stdout in C).

endl is the new line character.

<< is the insert output which allows you to insert something into the out put.

There are no format specifier (%d, %s, etc.). Items that need to be printed are arranged in printing order, which is easier to read and understand. However, if you still want to use it, it does work after including <cstdio>.

Chaining

In the above example, we send two things to cout, this is called chaining. The insert operator joins all the items to write in a "chain".
The leftmost item in the chain is the stream being written to.

cout << "We have " << inventory << " " << item << "s left." << endl;

inventory and item are variables.

Operator Overloading

<< usually does bitwise left-shift; but if operand on the left is a C++ stream (cout), << is the insert operator. Shifting only applies to numbers.
More on this later.

User input

string name;
cin >> name;

If you input an entire sentence, it will still read one token at a time (word by word per iteration). This code will store the smallest word in an inputted sentence and print it out.

string word, smallest;
while (cin >> word) {
	if(smallest.empty() || word < smallest) {
		samllest = word;
	}
}
count << smallest < endl;
cin.get(ch)