teaching machines

CS 455 Lecture 1 – Meta and Learn C++ in 75 Minutes

January 21, 2013 by . Filed under cs455, lectures, spring 2013.

Agenda

TODO

About you

  1. Your name?
  2. Last unassigned book you read?
  3. Where are you from?
  4. If not computers, what?

This course…

C++

first.cpp


#include <iostream>

void f(int *a) {
  std::cout << "a1: " << *a << std::endl;
  *a = 7;
  std::cout << "a2: " << *a << std::endl;
}

void g(int &a) {
  std::cout << "a4: " << a << std::endl;
  a = 8;
  std::cout << "a5: " << a << std::endl;
}

int main(int argc, char **argv) {

  int a = 6;
  bool isOff = true;

  std::cout << a << std::endl;

  if (a) {
    std::cout << "oh yeah!" << std::endl;
  } else {
    std::cout << "boo!" << std::endl;
  }

  f(&a);
  std::cout << "a3: " << a << std::endl;

  g(a);
  std::cout << "a6: " << a << std::endl;

  int *nums = new int[10];
  nums[10] = 100;
  std::cout << "nums[10]: " << nums[10] << std::endl;
  2[nums] = 3;
  std::cout << "nums[2]: " << nums[2] << std::endl;

  delete[] nums;

  int *num = new int(10);
  delete num;

  return 0;
}

Vector3.h


#ifndef VECTOR3_H
#define VECTOR3_H

class Vector3 {
  public:
    Vector3();
    Vector3(float x, float y, float z);
    ~Vector3();

    float GetLength();

    float& operator[](int index);
    /* float operator+=(int index); */

  private:
    float xyz[3];
};

#endif

Vector3.cpp


#include <cassert>
#include <cmath>

#include "Vector3.h"

/* ------------------------------------------------------------------------- */

Vector3::Vector3() {
  xyz[0] = xyz[1] = xyz[2] = 0.0f;
}

/* ------------------------------------------------------------------------- */

Vector3::Vector3(float x, float y, float z) {
  xyz[0] = x;
  xyz[1] = y;
  xyz[2] = z;
}

/* ------------------------------------------------------------------------- */

Vector3::~Vector3() {
}

/* ------------------------------------------------------------------------- */

float Vector3::GetLength() {
  return sqrtf(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2]);
}

/* ------------------------------------------------------------------------- */

float& Vector3::operator[](int index) {
  assert(index >= 0 && index <= 2);
  return xyz[index];
}

/* ------------------------------------------------------------------------- */

vecfun.cpp


#include <iostream>
#include "Vector3.h"

int main(int argc, char **argv) {
  Vector3 v(3.0f, 4.0f, 5.0f);
  std::cout << "v.GetLength(): " << v.GetLength() << std::endl;

  v[0] = 18.0f;

  std::cout << "v[0]: " << v[0] << std::endl;
  std::cout << "v[1]: " << v[1] << std::endl;
  std::cout << "v[2]: " << v[2] << std::endl;
  /* std::cout << "v[17]: " << v[17] << std::endl; */
  /* std::cout << "v.GetX(): " << v.GetX() << std::endl; */
  /* std::cout << "v.GetY(): " << v.GetY() << std::endl; */
  /* std::cout << "v.GetZ(): " << v.GetZ() << std::endl; */

}

Haiku

“Impose no morals”
C++ wags no finger
It pulls the trigger