CS 330 Lecture 27 – Inheritance and polymorphism
Agenda constructors and destructors a destructor’s job: release dynamically-allocated members a linked list in C++ a Stack using inheritance in C++ implementation inheritance vs. interface inheritance the enlightened stance: favor composition, shallow hierarchies, interfaces how polymorphism works TODO Why extends is evil: http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html?page=1 What OOP isn’t: http://hacksoflife.blogspot.com/2010/12/what-oop-isnt.html Program This Given the List<T> implementation discussed in class, write […]
CS 330 Lecture 26 – C++’s standard template library
Agenda map vector using STL to write a probababble engine http://www.gutenberg.org/ Code bsearch.cpp #include <iostream> template<typename T> int binarysearch(T *haystack, T needle, int first, int last) { if (first > last) return -1; int mid = (first + last) / 2; if (needle < haystack[mid]) { return binarysearch<T>(haystack, needle, first, mid – 1); } else […]
CS 330 Lecture 25 – Generics/templates II
Agenda Java before generics genericizing in Java type erasure adding templates to C templates in C++ Code ArrayList.java package cs330; public class ArrayList { private Object[] stuff; private int size; public ArrayList() { stuff = new Object[10]; size = 0; } public Object get(int i) { return stuff[i]; } public int size() { return size; […]
CS 330 Lecture 24 – Generics/templates
Agenda named constructor pattern typing: strong and static and dynamic a peril of static typing: code Java generics C++ templates TODO Read http://docs.oracle.com/javase/tutorial/java/generics/index.html. Code makefile drawer: drawer.cpp Vector.h Matrix.h Matrix.cpp Vector.cpp g++ -Wall -o drawer Vector.cpp Matrix.cpp drawer.cpp Vector.cpp #include <cassert> #include “Vector.h” Vector2::Vector2() : x(0.0f), y(0.0f) { } Vector2::Vector2(float x, float y) : x(x), y(y) […]
CS 330 Lecture 23 – Matrix/vector and templates
Agenda operator<< operator() named constructor pattern templates TODO Read Microsoft’s reference on C++ templates: http://msdn.microsoft.com/en-us/library/y097fkab(v=vs.80).aspx. Code Vector.h #ifndef VECTOR_H #define VECTOR_H #include <iostream> #include <fstream> class Vector2 { public: Vector2(); Vector2(float x, float y); // write a subscript operator float& operator[](int index); const float& operator[](int index) const; Vector2 operator+(const Vector2& other); // overload a print operator […]
CS 330 Lecture 22 – Custom types in C++
Agenda a gdb primer object-oriented programming classes inheritance polymorphism control abstraction vs. data abstraction writing a matrix class and a vector class overloading [], (), *, and << generic code through templates TODO Read http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Constructor. Read http://www.hackcraft.net/raii/. Code Vector.h #ifndef VECTOR_H #define VECTOR_H class Vector2 { public: Vector2(); Vector2(float x, float y); // write a subscript operator […]
CS 330 Lecture 21 – First C++
Agenda what do you know about C++? C++ roots: Simula Bjarne Strostroup TIOBE index for C++ values vs. pointers vs. references (which?) classes in C++ mapping between C with Classes and C can be made on stack for heap, why new/delete? (RAII) access control constructor/deconstructor use initialization lists default constructor copy constructor an example class: FasterString […]
CS 330 Lecture 20 – File I/O and Quiz
Agenda writing a WAV file the characteristics of imperative Code mucis.c N.B. The fwrite calls below have their middle arguments transposed. Element size comes before the number of elements. #include <stdio.h> #include <stdlib.h> #include <math.h> /* ————————————————————————- */ const int SAMPLES_PER_SECOND = 22050; const int BEATS_PER_MINUTE = 200; const float PI = 3.14159f; /* ————————————————————————- […]
CS 330 Homework 4 – due before Thursday, March 29
See the PDF.
CS 330 Lecture 19 – Abstracting a GUI
Agenda the libexpat API slurping a file in C adding a GUI abstraction layer using XML TODO Why we chunk-read: http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/ C file I/O: http://en.wikibooks.org/wiki/A_Little_C_Primer/C_File-IO_Through_Library_Functions Code parse_xml.c #include <expat.h> #include <stdio.h> #include <stdlib.h> /* typedef parser_t * XML_Parser; */ /* ————————————————————————- */ void start(void *data, const char *tag, const char **attributes) { printf(“data: %d\n”, *((int *) data)); […]