C++ Mechanics

Basic C++ program

  • Use of %%file magic

  • Use of #include to import libraries

  • Use of streams

  • Fully qualified names

  • Semi-colons and braces

  • Compilation and execution of binary

In [1]:
%%file hello01.cpp

#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
}
Overwriting hello01.cpp

Compile

In [2]:
%%bash

g++ hello01.cpp -o hello01

Execute

In [3]:
%%bash

./hello01
Hello, world!

C equivalent

In [4]:
%%file hello01.c

#include <stdio.h>

int main() {
    printf("Hello, wolrd!\n");
}
Overwriting hello01.c
In [5]:
%%bash

gcc hello01.c -o hello_c
In [6]:
%%bash

./hello_c
Hello, wolrd!

Using qualified names

In [7]:
%%file hello02.cpp

#include <iostream>

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

int main() {
    cout << "Hello, world!" << endl;
}
Overwriting hello02.cpp
In [8]:
%%bash

g++ hello02.cpp -o hello02
In [9]:
%%bash

./hello02
Hello, world!

Using namespace

In [10]:
%%file hello03.cpp

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, world!" << endl;
}
Overwriting hello03.cpp
In [11]:
%%bash

g++ hello03.cpp -o hello03
In [12]:
%%bash

./hello03
Hello, world!

Using a function

In [13]:
%%file hello04.cpp

#include <iostream>

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

string greet() {
    return "Hello, world!";
}

int main() {
    cout << greet() << endl;
}
Overwriting hello04.cpp
In [14]:
%%bash

g++ hello04.cpp -o hello04
In [15]:
%%bash

./hello04
Hello, world!

Separate compilation

In [16]:
%%file string_utils.hpp

#include <iostream>

std::string greet();
Overwriting string_utils.hpp
In [17]:
%%file string_utils.cpp

#include "string_utils.hpp"

std::string greet() {
    return "Hello, world!";
}
Overwriting string_utils.cpp
In [18]:
%%file hello05.cpp

#include <iostream>
#include "string_utils.hpp"

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

int main() {
    cout << greet() << endl;
}
Overwriting hello05.cpp
In [19]:
%%bash

g++ -c string_utils.cpp
In [20]:
%%bash

ls *\.o
string_utils.o
In [21]:
%%bash

g++ hello05.cpp string_utils.o -o hello05
In [22]:
%%bash

./hello05
Hello, world!

Separate compilation with a Makefile

In [23]:
%%file Makefile

hello05a:
    g++ hello05.cpp string_utils.o -o hello05a

string_utils.o:
    g++ -c string_utils.cpp
Overwriting Makefile
In [24]:
%%bash

make
make: `hello05a' is up to date.
In [25]:
%%bash

./hello05a
Hello, world!

Command line arguments

Also string streams and control flow.

In [26]:
%%file hello06.cpp

#include <iostream>
#include <sstream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

string greet(string name) {
    std::stringstream s;
    s << "Hello " << name << "!";
    return s.str();
}

int main(int argc, char** argv) {
    if (argc > 1) {
        string name = argv[1];
        cout << greet(name) << endl;
    }
    else {
        cout << greet("world") << endl;
    }
}
Overwriting hello06.cpp
In [30]:
%%bash

g++ -std=c++11 hello06.cpp -o hello06
In [31]:
%%bash

./hello06
Hello world!
In [32]:
%%bash

./hello06 Cliburn
Hello Cliburn!
In [ ]: