teaching machines

CS 330 Lecture 30 – Overloading Builtin Operators

April 17, 2013 by . Filed under cs330, lectures, spring 2013.

Agenda

TODO

Code

refs.cpp

#include <iostream>

int &getA() {
  int a = 7;
  return a;
}

int foo(int c, int d, int e) {
  return c + d + e;
}

int main(int argc, char **argv) {
  int &b = getA(); 
  std::cout << foo(1, 2, 3) << std::endl;
  std::cout << "b: " << b << std::endl;
  return 0;
}

Vector3.cpp

#ifndef VECTOR3_H
#define VECTOR3_H

#include <iostream>

class Vector3 {
  public:
    Vector3(float x, float y, float z);
    Vector3(const Vector3& other);
    Vector3(float scalar);

  /* private: */
    float xyz[3];
};

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

Vector3::Vector3(const Vector3& other) {
  std::cout << "copying in copy ctor" << std::endl;
  xyz[0] = other.xyz[0];
  xyz[1] = other.xyz[1];
  xyz[2] = other.xyz[2];
}

Vector3::Vector3(float scalar) {
  xyz[0] = xyz[1] = xyz[2] = scalar;  
}

int main(int argc, char **argv) {
  Vector3 a(5.0f, 6.0f, 7.0f);
  Vector3 b = a;
  b = a;
  Vector3 c = 3.0f;
  std::cout << "c.xyz[#](# in 0,3): " << c.xyz[0] << ", " << c.xyz[1] << ", " << c.xyz[2] << std::endl;
  std::string s = "foo bag";
}

#endif

FasterString.cpp

#include <cassert>
#include <cstring>
#include <iostream>

class FasterString {
  public:
    FasterString();
    FasterString(const char *src);

    int GetLength() const;
    char &operator[](int i);
    const char &operator[](int i) const;

    FasterString operator+(const FasterString& other) const;

  private:
    int length;
    char *text;
};

FasterString::FasterString() :
  length(0),
  text(NULL) {
}

FasterString::FasterString(const char *src) :
  length(strlen(src)) {
  text = new char[length];
  memcpy(text, src, length * sizeof(char));
}

char &FasterString::operator[](int i) {
  assert(i >= 0 && i < length);
  return text[i];  
}

const char &FasterString::operator[](int i) const {
  assert(i >= 0 && i < length);
  return text[i];  
}

int FasterString::GetLength() const {
  return length;  
}

FasterString FasterString::operator+(const FasterString& other) const {
  FasterString joined;
  joined.length = GetLength() + other.GetLength();
  joined.text = new char[joined.length];

  memcpy(joined.text, text, GetLength());
  memcpy(joined.text + GetLength(), other.text, other.GetLength());

  return joined;
}

std::ostream &operator<<(std::ostream &out,
                         const FasterString &s) {
  for (int i = 0; i < s.GetLength(); ++i) {
    out << s[i];
  }
  return out;
}

int main(int argc, char **argv) {
  FasterString s("I have a question. What's with the curly bracket?"); 
  std::cout << "s: " << s << std::endl;
  /* std::cout << "asdf" << std::endl; */
  FasterString a("I do");
  FasterString b("we woo");
  FasterString c = a + b;
  std::cout << "c: " << c << std::endl;
  return 0;
}

Haiku

Just numbers get +. Objects don’t; it’s too messy. Just numbers. And String.