{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# C++ Mechanics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic C++ program\n", "\n", "- Use of %%file magic\n", "- Use of #include to import libraries\n", "- Use of streams\n", "- Fully qualified names\n", "- Semi-colons and braces\n", "- Compilation and execution of binary" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello01.cpp\n" ] } ], "source": [ "%%file hello01.cpp\n", "\n", "#include \n", "\n", "int main() {\n", " std::cout << \"Hello, world!\" << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compile" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ hello01.cpp -o hello01" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello01" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## C equivalent" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello01.c\n" ] } ], "source": [ "%%file hello01.c\n", "\n", "#include \n", "\n", "int main() {\n", " printf(\"Hello, wolrd!\\n\");\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "gcc hello01.c -o hello_c" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, wolrd!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello_c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using qualified names" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello02.cpp\n" ] } ], "source": [ "%%file hello02.cpp\n", "\n", "#include \n", "\n", "using std::cout;\n", "using std::endl;\n", " \n", "int main() {\n", " cout << \"Hello, world!\" << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ hello02.cpp -o hello02" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello02" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using namespace" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello03.cpp\n" ] } ], "source": [ "%%file hello03.cpp\n", "\n", "#include \n", "\n", "using namespace std;\n", "\n", "int main() {\n", " cout << \"Hello, world!\" << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ hello03.cpp -o hello03" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello03" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using a function" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello04.cpp\n" ] } ], "source": [ "%%file hello04.cpp\n", "\n", "#include \n", "\n", "using std::cout;\n", "using std::endl;\n", "using std::string;\n", " \n", "string greet() {\n", " return \"Hello, world!\";\n", "}\n", "\n", "int main() {\n", " cout << greet() << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ hello04.cpp -o hello04" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello04" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Separate compilation" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting string_utils.hpp\n" ] } ], "source": [ "%%file string_utils.hpp\n", "\n", "#include \n", " \n", "std::string greet();" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting string_utils.cpp\n" ] } ], "source": [ "%%file string_utils.cpp\n", "\n", "#include \"string_utils.hpp\"\n", " \n", "std::string greet() {\n", " return \"Hello, world!\";\n", "}" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello05.cpp\n" ] } ], "source": [ "%%file hello05.cpp\n", "\n", "#include \n", "#include \"string_utils.hpp\"\n", "\n", "using std::cout;\n", "using std::endl;\n", "\n", "int main() {\n", " cout << greet() << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -c string_utils.cpp" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string_utils.o\n" ] } ], "source": [ "%%bash\n", "\n", "ls *\\.o" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ hello05.cpp string_utils.o -o hello05" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello05" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Separate compilation with a Makefile" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting Makefile\n" ] } ], "source": [ "%%file Makefile\n", "\n", "hello05a:\n", "\tg++ hello05.cpp string_utils.o -o hello05a\n", " \n", "string_utils.o:\n", "\tg++ -c string_utils.cpp" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "make: `hello05a' is up to date.\n" ] } ], "source": [ "%%bash\n", "\n", "make" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "%%bash \n", "\n", "./hello05a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Command line arguments\n", "\n", "Also string streams and control flow." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting hello06.cpp\n" ] } ], "source": [ "%%file hello06.cpp\n", "\n", "#include \n", "#include \n", "\n", "using std::cin;\n", "using std::cout;\n", "using std::endl;\n", "using std::string;\n", " \n", "string greet(string name) {\n", " std::stringstream s;\n", " s << \"Hello \" << name << \"!\";\n", " return s.str();\n", "}\n", "\n", "int main(int argc, char** argv) {\n", " if (argc > 1) {\n", " string name = argv[1];\n", " cout << greet(name) << endl;\n", " }\n", " else {\n", " cout << greet(\"world\") << endl;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -std=c++11 hello06.cpp -o hello06" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello06" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Cliburn!\n" ] } ], "source": [ "%%bash\n", "\n", "./hello06 Cliburn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }