C/C++ Overview

The C and C++ programming languages are an industry standard and a working knowledge of both will significantly help in obtaining a job in the music technology field. Our ultimate goal in this class is to obtain enough C++ knowledge to facilitate use of the RtMidi class for low-level MIDI processing. This will provide some insight into the way many of the Max and Pd MIDI functions are implemented.

There is not sufficient time in this course for a complete presentation of C++ programming concepts. Instead, I will provide some notes and links to tutorials that you should pursue outside of class. I will be happy to answer programming questions in class or via email.

Online Tutorials

C++ Example #1

// helloworld.cpp
//
// compile: g++ -Wall -o helloworld helloworld.cpp

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;

  return 0;
}

helloworld.cpp

Variables and Data Types

// variables.cpp
//
// compile: g++ -Wall -o variables variables.cpp

#include <iostream>

int main()
{
  unsigned char a = 7;  // integer between 0 - 255
  const int b = 45;     // a fixed-value integer (cannot be changed)
  short c = 99;         // a two-byte integer
  float f = 3.497;      // a four-byte floating-point variable
  double d = -0.2345;   // an eight-byte floating-point variable

  // In general, don't combine variables of different types unless you
  // are clear on the possible outcomes.  The following will work but
  // may produce a compiler warning.
  long result = a + b + c;

  std::cout << "result = " << result << std::endl;

  //b = 44;      // won't work ... 'b' is a const
  //a = -4;      // unsigned variable ... but compiler might not complain
  c = 55000;     // what happens here?

  std::cout << "c now = " << c << std::endl;

  a = 0xA4;      // set 'a' to a MIDI "aftertouch" status byte on channel 4

  std::cout << "a = " << (int)a << ", channel masked a = " << (int)(a & 0xF0) << std::endl;

  return 0;
}

variables.cpp

C++ Vectors

// vectors.cpp
//
// compile: g++ -Wall -o vectors vectors.cpp

#include <vector>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  vector<unsigned char> a;    // create an empty vector of unsigned 8-bit integers
  vector<float> b( 5, 0.0 );  // create a vector of 5 floats, each initalized to a value of 0.0

  a.push_back( 4 );           // add the value '4' to the end of vector 'a'
  a.push_back( 1 );           // add another value to the end of vector 'a'

  b[0] = 2.0;                 // change value of first element in 'b' ... no error checking on index value

  for ( unsigned int i=0; i<b.size(); i++ ) {
    b.at( i ) = 4.0;           // change all values in 'b' to 4.0
    // b[i] = 4.0;             // also works but does not throw an error if 'i' is out of range
  }

  // Access vector contents using indexing
  cout << "a contains:";
  for ( unsigned int i=0; i<a.size(); i++ )
    cout << " " << (int) a[i];
  cout << endl;

  // Access vector contents using an "iterator"
  cout << "a contains:";
  vector<unsigned char>::iterator it;
  for ( it=a.begin(); it<a.end(); it++ )
    cout << " " << (int) *it;
  cout << endl;

  // Remove all elements in 'a'
  a.clear();

  // Populate vector with values
  for ( unsigned int i=0; i<8; i++ )
    a.push_back( i );

  std::cout << "new a contains:";
  for ( unsigned int i=0; i<a.size(); i++ )
    std::cout << " " << (int) a[i];
  std::cout << std::endl;

  // Randomly remove a few items from 'a'
  srandomdev(); // 'seed' the random number generator
  for ( unsigned int i=0; i<2; i++ ) {
    int nElements = a.size();
    int idx = random() % nElements;
    a.erase( a.begin()+idx );
  }

  std::cout << "a now contains:";
  for ( unsigned int i=0; i<a.size(); i++ )
    std::cout << " " << (int) a[i];
  std::cout << std::endl;

  return 0;
}

vectors.cpp

McGill ©2003-2016 McGill University. All Rights Reserved.
Maintained by Gary P. Scavone.