CSCE 121 Chapter 12-14

From Notes
(Redirected from CSCE 121 Chapter 12)
Jump to navigation Jump to search

« previous | Monday, October 11, 2010 | next »


GUI (yay!)

Objects are "attached" to a window object, and the display engine takes care of the rest.

Running in Terminal: ssh -X -l mbarry sun.cse.tamu.edu

Simple Example

Graph lib based on FLTK

#include "graph_lib.h"

int main() {
  using namespace Graph_lib;                  // provided by Dr. Stroustrup
  Point tl(100,200);
  Simple_window win(tl, 600, 400, "Canvas");  // location, width, height, title

  // Create polygon with three sides.
  Polygon poly;
  poly.add(Point(300,200));
  poly.add(Point(350,100));
  poly.add(Point(400,200));
  poly.set_color(Color::red);

  win.attach(poly);
  
  win.wait_for_button();                      // display window
}

Graphical utilities

Here are just a few of the graphing utilities

Generic Member Functions:

Axis

Axis xa(Axis::x|Axis::y, Point(a,b), int length, int notches, string label)

Member Functions:

  • xa.set_color(Color c);
  • xa.label.set_color(Color c);

Function

Function sine(sin, 0, 100, Point(20,150), 1000, 50, 50); // Sine curve

Plot sin() in range [0:100) with origin at 20, 150 using 1000 points; scale x values by 50, scale y values by 50

Image

Image ii(Point(0,0), "filename.jpg");

Member Functions:

  • ii.set_mask(Point, int width, int height);

Rectangle

Rectangle r(Point(0,0), 100, 200);
  • r.set_fill_color(Color c);


Converting data to a string: osstream

ostringstream oss;
oss << "screen size: " << x_max() << "*" << y_max() << "; window size: " << win.x_max() << "*" << win.y_max();
string screen_info = oss.str();

Code Library Definitions

struct Shape {/* ... */};

struct Point{
  int x,y;
  Point(int xx, int yy) : x(xx), y(yy) {}
};

struct Line : Shape {    // ':' means 'extends' or 'is a type of'
  Line(Point p1, Point p2);
};

struct Lines : Shape {
  void add(Point p1, Point p2) {
    Shape::add(p1);
    Shape::add(p2);
  }
}</code>

Implementation of <tt>Lines</tt>:
<code cpp>int x_size = win.x_max();
int y_size = win.y_max();
int x_grid = 80;
int y_grid = 40;

Lines grid;
for (int x = x_grid; x < x_size; x += x_grid)  // vertical lines
  grid.add(Point(x,0), Point(x, y_size));
for (int y = y_grid; x < y_size; y += y_grid)  // horizontal lines
  grid.add(Point(0,y), Point(x_size, y));

win.attach(grid);


Wednesday, October 13, 2010


typedef's: Functional Arguments

Declare a type of a function that takes a single double argument and returns a double.

Allows functions (function names) to be passed as arguments to another function

typedef double Fct(double);

void execute(Fct f) {
  cout << f(30);
}

// ...

double fun (double x) {
  return x/2.0;
}

// ...

int main() {
  execute(fun);
}


Friday, October 15, 2010


Logically different operations

shape.add() copies objects

window.attach() creates reference

Hide the "lower" code; e.g. DBAL

Libraries
"collection of functions meant to be used together."

Inheritance Modifiers

Only for public inheritance:

struct S1 : S2 {};        // defaults to public
class S1 : public S2 {};  // defaults to private; "public" flag makes public members public
public
anyone can access the members
protectedNEW!
only class and any derived classes can access members
private
only the current class can access members
class Shape {
  protected:
    Shape();   // Shape class is abstract; can only be inherited
    void add(Point p);
};

Shape ss;      // Cannot be instantiated

Shape class

  • Deals with color and style
  • draws points on window by connecting them

How Shape draws lines:

void Shape::draw_lines() const {
  if (color().visible() && 1 < points.size())
    for (int i=1; i < points.size(); ++i)
      fl_line(points[i-1].x, points[i-1].y, points[i].x, points[i].y);

How Lines overloads the draw_lines() function:

void Lines::draw_lines() const {
  for (int i=1; i < number_of_points(); i+=2)
    fl_line(point(i-1).x, point(i-1).y, point(i).x, point(i).y);


virtual Function Modifier

virtual void draw_lines() const;

virtual enables "late-binding" or "polymorphism" -- let runtime decide which function to execute (when overloaded in derived classes)

Creating pure virtual function

virtual double increase(int i)=0;  // note the "=0"

Interfaces: abstract classes

struct Engine {
  // no data
  // (usually) no constructor (protected if defined)
  virtual double increase(int i)=0;  // must be defined in derived class
  virtual ~Engine();                 // (usually) a virtual destructor
};

class M123 : public Engine {
  public:
    M123();  // constructor
    double increase(int i) { /* ... */ }
    // ...
    ~M123();    // destructor: cleanup, release sources
};

Engine eee;  // ERROR: Engine is abstract
M123 meee;   // OK

Object-Oriented Programming

  • Base and derived classes ("inheritance")
    • A function expecting a Shape parameter can accept any derived object type
    • Simplifies use
  • Virtual functions ("polymorphism")
  • Private and Protected members ("encapsulation")