CSCE 314 Lecture 26

From Notes
Jump to navigation Jump to search

« previous | Monday, October 31, 2011 | next »


Semantics

What do programs mean?

Example:

  • Meaning = Statement × State → State
  • Assignment = Variable target; Expression source (replaces state value of target with walue of source)

Object-Oriented Programming

OOP = encapsulated state + inheritance (with dynamic binding)

A twist on ADTs

ADTs don't provide a clear answer to initialization/allocation or reuse between similar types

  • Classes (init) and Inheritance (reuse) provide one answer
  • Lends to hierarchy of ADTs (Arrays and lists are both "sequences")

Stack in C

// stack.h
struct node
{
    int data;
    struct node *next
};

typedef struct node* STACK;

int empty(STACK s);
STACK newstack();
int pop(STACK* s);
void push(STACK* s, int x);
int top(STACK s);

// stack.c
#include "stack.h"
#include <stdlib.h>

int empty(STACK s) {return s == 0;}
...

Vocabulary

Object
An entity that has a unique identity and encapsulates a state
provides access to internal state via external methods
Class
A blueprint for creating objects

Class Invariant

A logical condition that ensures that an object of a class is in a well-defined state.

Every method assumes that the class invariant condition holds and must ensure that it still holds when the program exits.

Example:

class triangle {
    double a, b, c;
    // ...
};

Invariant: