teaching machines

CS 330 Lecture 28 – C++ References and the Pillars of OOP

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

Agenda

TODO

Code

Functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

class Polynomial {
  public:
    Polynomial(int degree);
    double evaluateAt(double x) const;

    void setCoefficient(int i, double coefficient);

  private:
    int degree;
    double *coefficients;
};

class LinearFunction : public Polynomial {
  public:
    LinearFunction(double slope, double y_intercept);
    /* double evaluateAt(double x) const; */

  private:
    /* double slope; */
    /* double y_intercept; */
};

class QuadraticFunction : public Polynomial {
  public:
    QuadraticFunction(double a, double b, double c);
    /* double evaluateAt(double x) const; */

  private:
    /* double a, b, c; */
};

#endif

Functions.cpp

#include "Functions.h"

Polynomial::Polynomial(int degree) :
  degree(degree) {
  coefficients = new double[degree + 1];
}

double Polynomial::evaluateAt(double x) const {
  double sum = 0.0;  
  double xpow = 1.0;
  for (int i = 0; i <= degree; ++i) {
    sum += xpow * coefficients[i];
    xpow *= x;
  }
  return sum;
}

void Polynomial::setCoefficient(int i, double coefficient) {
  coefficients[i] = coefficient;  
}

LinearFunction::LinearFunction(double slope,
                               double y_intercept) :
  Polynomial(1) {
  setCoefficient(0, y_intercept);
  setCoefficient(1, slope);
  /* slope(slope), */
  /* y_intercept(y_intercept) { */
}

/* double LinearFunction::evaluateAt(double x) const { */
  /* return slope * x + y_intercept; */
/* } */

QuadraticFunction::QuadraticFunction(double a,
                                     double b,
                                     double c) :
  Polynomial(2) {
  setCoefficient(0, c);
  setCoefficient(1, b);
  setCoefficient(2, a);
  /* a(a), */
  /* b(b), */
  /* c(c) { */
}

/* double QuadraticFunction::evaluateAt(double x) const { */
  /* return a * x * x + b * x + c;   */
/* } */

graph.cpp

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

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

void draw(const Polynomial &f);

int main(int argc, char **argv) {
  LinearFunction f(1.0, 0.0);
  QuadraticFunction xsquared(1.0, 0.0, -1.0);
  Polynomial p(3);
  p.setCoefficient(0, 0.0);
  p.setCoefficient(1, 0.0);
  p.setCoefficient(2, 0.0);
  p.setCoefficient(3, 1.0);
  draw(p);
  return 0;
}

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

void draw(const Polynomial &f) {
  const int NROWS = 21;
  const int NCOLS = 51;

  char plot[NROWS][NCOLS];

  for (int r = 0; r < NROWS; ++r) {
    for (int c = 0; c < NCOLS; ++c) {
      plot[r][c] = ' ';
    }
  }

  for (int c = 0; c < NCOLS; ++c) {
    plot[NROWS / 2][c] = '-';
  }

  for (int r = 0; r < NROWS; ++r) {
    plot[r][NCOLS / 2] = '|';
  }

  plot[NROWS / 2][NCOLS / 2] = '+';

  for (int c = 0; c < NCOLS; ++c) {
    double x = c / (NCOLS - 1.0) * 6.0 - 3.0;
    double y = f.evaluateAt(x);
    int r = (int) (y * NROWS * 0.5 + NROWS * 0.5);
    if (r >= 0 && r < NROWS) {
      plot[r][c] = '.';
    }
  }

  for (int r = NROWS - 1; r >= 0; --r) {
    for (int c = 0; c < NCOLS; ++c) {
      std::cout << plot[r][c];
    }
    std::cout << std::endl;
  }
}

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

makefile

Note to copy and pasters: makefile rules need to be indented with real tabs, not spaces.

all: graph

Functions.cpp: Functions.h

Functions.o: Functions.cpp
  g++ -c Functions.cpp

graph: graph.cpp Functions.o Functions.h
  g++ -o graph graph.cpp Functions.o

run: run_graph
run_graph:
  ./graph

clean:
  rm -f *.o

Haiku

Even Britney knows
“OOP! I did it again. Drat!
I should refactor”