Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 199

Deprecated: Creation of dynamic property Automattic\Jetpack\Connection\Manager::$error_handler is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-connection/src/class-manager.php on line 91

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Instagram_Gallery::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-instagram-gallery.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Mailchimp::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php on line 17

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 21

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 22

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php on line 20

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 24

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Subscribers::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php on line 15

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property WPCOM_Widget_Goodreads::$shelves is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/modules/widgets/goodreads.php on line 33

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /var/www/twodee/blog/wp-includes/script-loader.php on line 348

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198
» CS 330 Lecture 29 – Virtuality in C++ and Overloading Builtin Operators

teaching machines

CS 330 Lecture 29 – Virtuality in C++ and Overloading Builtin Operators

April 15, 2013 by . Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs330, lectures, spring 2013.

Agenda

Pulse

Student evaluations are just numbers, but I like to hear anecdotes too. Please take a moment to think about how this course is run: grading scale, homework setup, topics, and so on. Now, suppose you’re teaching a classroom full of up-and-coming computer scientists.

  1. What would you retain?
  2. What would you do differently?

Opportunity

Program This


Rewrite the following using C++ references:

template<typename T> void swap(T *a, T *b) {
  T tmp = *a;
  *a = *b;
  *b = tmp;
}

Code

Functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

class Function {
  public:
    virtual double evaluateAt(double x) const = 0;
};;;;

class SineFunction : public Function {
  public:
    double evaluateAt(double x) const;
};

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

    void setCoefficient(int i, double coefficient);

    virtual double *getRoots() const;

  private:
    int degree;

  protected:
    double *coefficients;
};

class LinearFunction : public Polynomial {
  public:
    LinearFunction(double slope, double y_intercept);
    double *getRoots() const;
};

class QuadraticFunction : public Polynomial {
  public:
    QuadraticFunction(double a, double b, double c);
    double *getRoots() const;
};

#endif

Functions.cpp

#include <cmath>
#include <iostream>

#include "Functions.h"

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

int Polynomial::GetDegree() const {
  return degree;  
}

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;  
}

double *Polynomial::getRoots() const {
  std::cerr << "I don't know how to do that." << std::endl;
  return NULL;
}

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

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

double *LinearFunction::getRoots() const {
  double *roots = new double[1];
  roots[0] = -coefficients[0] / coefficients[1];  
  return roots;
}

double *QuadraticFunction::getRoots() const {
  double *roots = new double[2];
  roots[0] = (-coefficients[1] + sqrt(coefficients[1] * coefficients[1] - 4.0 * coefficients[2] * coefficients[0])) / 2.0 * coefficients[2];
  roots[1] = (-coefficients[1] - sqrt(coefficients[1] * coefficients[1] - 4.0 * coefficients[2] * coefficients[0])) / 2.0 * coefficients[2];
  return roots;
}

double SineFunction::evaluateAt(double x) const {
  return sin(x);  
}

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

graph.cpp

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

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

void draw(const Function &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(SineFunction());
  return 0;
}

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

void draw(const Function &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

roots.cpp

#include <iostream>

#include "Functions.h"

void printRoots(const Polynomial &p) {
  double *roots = p.getRoots();
  if (roots != NULL) {
    for (int i = 0; i < p.GetDegree(); ++i) {
      std::cout << "roots[" << i << "]: " << roots[i] << std::endl;
    }
  }
}

int main(int argc, char **argv) {
  Polynomial xcubed(3); 
  xcubed.setCoefficient(0, 0.0);
  xcubed.setCoefficient(1, 0.0);
  xcubed.setCoefficient(2, 0.0);
  xcubed.setCoefficient(3, 1.0);

  printRoots(xcubed);
  printRoots(QuadraticFunction(1.0, 0.0, 0.0));
  printRoots(LinearFunction(1.0, 3.0));

  std::cout << "0.0f * -0.0f: " << 0.0f * -0.0f << std::endl;

  return 0;
}

Haiku

“What makes OOP?”
“Superpolymorphitance.”
Your next interview.