October 27th, 2016

openFrameworks - C++ Basics

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    return 0;
}

I'm reading through the openFrameworks book, specifically the chapter on C++ Language Basics by http://jtnimoy.com/ and culling some high-points for reference. This is in no way a resource to replace the original. This is how I learn.

Before we get cracking, here's a link to Jeremy Rotsztain's page where you can see some of his video installation and software art projects. Pretty slick.


Syntax

Single line comments

// your code here

Multi-line comments

/*
this is a multi-line comment
still in comment mode.
 */

The following line of code tells the program to print text to stdout.

cout << "Hello World" << endl;

Anything can go between the quotes. This is commonly called a "c-string literal". cout << means "send the following stuff to stdout in a formatted way". << endl means "add a carriage return to the end of the message". Semi-colons (;) end statements.


Beyond Hello World

Imports are defined and handled at the top of C++ implementation files (.cpp), and follow this syntax:

#include <iostream>

These imports include code from header files (.h). Importing custom files require that you follow this syntax (note the quotes and the .h added to the reference):

#include "MyCustomFile.h"

In openFrameworks, double quotes indicates we want to import something that wasnt part of the system installation.

The # prior to the 'include' directive is a indication that this line will be handled by the C++ preprocessor. This happens when the C++ compiler takes a first pass at the code.


Namespaces are groups of unique names. No names in the group are identical. But, it is possible to have identical names as long as they are part of different namespaces (groups) (I feel like this could get really confusing in the code). From what I can tell, Namespaces are like named objects that store key/value pairs, some values are variables and others are methods. Namespaces are a lot like classes without setters. Namespace declarations appear at the top of .cpp files and look like:

using namespace std;

In this case, the std namespace contains two names: cout & endl. So we can reference cout and endl in the code without having to precede the method with the std class reference (like so: std::cout and std::endl).


Functions are like little programs inside each other. They are reusable snippets of code wrapped in a code block:

int main() {
  //function body goes here
}

Just like a board game, programs have starting points. This is known as a program's entry point. In C++ programs the entry point is defined by a function called main. This is the code that is executed first in your program.

Let's dissect a function. Here's what we'll start with:

int main() {
  return 0;
}
  • int : this is the return type (integer) of the function. Functions optionally return a value.
  • main : this is the name of the function.
  • () : this is the arguments list.
  • { : opening bracket begins the code block.
  • return 0; : commonly known as the return statement - responsible for returning a value that matches the functions return type definition (int).
  • } : ends the code block.

Functions provide an Encapsulation of Complexity. This is defined as a matter of breaking your application into smaller pieces, effectively reducing the perceived complexity; improving maintainability and reducing cognitive load during development.


Variables are a means for storing values for later use. They are defined by first specifying a type (int, str, float, etc) and then a custom name:

//declared and initialized on one line

int answer = 42;

or

//declared and initialized separately

int answer;
answer = 42;

Best practice is to declare and initialize variables at the same time to prevent unpredictable issues with your code.


An unabridged version of this first chapter is available. It contains details about variables and other C++ conventions and language details.


On to the next

2017 : James Walton : Digital Carpentry