// lines end with a semi-colon (this is not optional)
// variables must be declared with a type before the variable name
int i = 0;
// control structures are similar to JavaScript (for loops, while loops, if/else, etc.)
int value = 0;
for (int i = 0; i < 5; i++) {
value++;
}
while(value >= 0) {
value--;
}
// no === in C++, only ==
if (value == 0) {
value = 1;
} else if (value == 1) {
value = 2;
} else {
value = 0;
}
// integers
int i = 1;
// floats
float f = 1.5;
// booleans
bool b = true;
// characters (note the single quotes)
char c = 'a';
// strings
std::string str = "Hello world!";
// OR
using namespace std;
string str = "Hello world!";
// arrays
int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/*
* This will print "Hello World" to the console.
*
* cout << means "send the following stuff to stdout in a formatted way."
*
* << endl means "add a carriage return (end-of-line) character to the end of the message."
*/
cout << "Hello World" << endl;
/*
* You can also chain multiple << in a row.
* This is useful because, unlike in JavaScript,
* you can't add values of different types (e.g. string and int).
*/
cout << "Hello World" << 12345 << endl;
// functions must be declared with the type of value that they will return
// and the types of arguments that they take
int addOne(int x) {
return x + 1;
}
// functions that don't return a value will have a return type of "void"
void doNotReturn() {
doSomeStuff();
drawSomething();
}
/*
* Preprocessor directives modify your code before it is compiled (they pre-process it!).
* #include tells the C++ compiler to copy/paste some code
* from another file into your file
*/
// include a header file from the standard library (iostream gives you cout and endl)
#include <iostream>
// include a header file that you (or someone else) wrote
#include "ofMain.h"
// #define defines a constant
#define PI 3.14159265359
// #pragma once tells the C++ compiler to only #include this file once, to prevent duplication
#pragma once
/*
* namespaces are a way of grouping together identifiers to avoid conflicts.
* to reference a name in a namespace, we use the :: syntax, e.g. std::string
*/
std::string foo = "bar";
// this tells the C++ compiler to load all the identifiers from the std namespace
using namespace std;
// now we can reference string without the :: syntax
string foo = "bar";
// add.h
#pragma once
int add(int x, int y);
// add.cpp
#include "add.h"
int add(int x, int y) {
return x + y;
}
// main.cpp
#include "add.h"
int main() {
int i = add(1, 2);
}
// Person.h
#pragma once
class Person {
// anything that is public can be accessed on an instance of this class
public:
Person(string _name); // constructor
~Person(); // destructor, don't worry about this too much
void greet();
// anything that is private can only be accessed by internal methods of this class
private:
string name;
};
// Person.cpp
#include "Person.h"
Person::Person(string _name) : name(_name) {
cout << "hello " << name << endl;
}
void Person::greet() {
cout << "greetings " << name << endl;
}
Person::~Person() {
cout << "goodbye " << name << endl;
}
// main.h
#include Person.h
int main() {
// make a new Person
Person me("Oren"); // should print "hello Oren"
me.greet(); // should print "greetings Oren"
me.name = "Steve"; // this would cause a compiler error
}
// after main() finishes running, should print "goodbye Oren"
// structs are another way of storing multiple pieces of data in one object.
// technically they can do anything that classes can do, but I like to use them
// as POD (plain old data) containers, without any constructors, destructors, or methods
typedef struct {
string name;
int age;
} Person;
Person me;
me.name = "Oren";
me.age = 25;
// vectors are arrays that can dynamically change in size,
// like arrays in JavaScript or ArrayLists in Java
// when we declare a vector, we specify the type of data that it will contain inside <>
vector<int> numbers;
// adding to the back of a vector
for (int i = 0; i < 100; i++) {
numbers.push_back(i);
}
// accessing a value in a vector by index
int middle = numbers[50];
// removing from the back of a vector
int size = numbers.size();
for (int j = 0; j < size; j++) {
numbers.pop_back();
}
// in C++, when we assign a value to a variable, we're usually copying that value.
// however, sometimes we actually want to reference the exact same value in memory,
// e.g. when we are modifying an object in a vector of objects
// ofPoint is an openFrameworks class that stores (x, y) coordinates
vector<ofPoint> points;
for (int i = 0; i < 5; i++) {
ofPoint p(i, 0);
points.push_back(p);
}
ofPoint copy = points[0];
copy.x = 10;
cout << points[0].x << endl; // this will still print 0
// we can use the & operator to make a reference variable
ofPoint & reference = points[0];
points[0].x = 10;
cout << points[0].x << endl; // this will print 10