Lecture notes for CS 503, Advanced Programming I

Advanced Programming I Lecture Notes

3 May 2007 • Objects in C


Outline

Objectives

A Stack Interface

#ifndef STACK_H #define STACK_H # include <stdlib.h> /* for NULL */ # include "list.h" typedef List Stack; # define stack_init list_init # define stack_destroy list_destroy int stack_push(Stack *, const void * data); int stack_pop(Stack *, void ** data); # define stack_peek(_s) \ ((_s)→head == NULL ? NULL : (_s)→head->data) # define stack_size list_size #endif

typedefs

#define Macros

#define Macro Parameters

Void Pointers

What's Wrong?

# include "list.h" typedef List Stack; # define stack_init list_init # define stack_destroy list_destroy int stack_push(Stack *, const void * data); int stack_pop(Stack *, void ** data); # define stack_peek(_s) \ ((_s)→head == NULL ? NULL : (_s)→head→data) # define stack_size list_size

Implementation Details

A New Stack Interface

#ifndef STACK_H #define STACK_H typedef void * Stack; Stack stack_make(); void stack_unmake(Stack); int stack_push(Stack, const void * data); int stack_pop(Stack, void ** data); int stack_peek(Stack, void ** data); unsigned stack_size(Stack); #endif

Opaque ADT Types

Singleton ADTs

Singleton ADT Interface

#ifndef STACK_H #define STACK_H void stack_init(); void stack_term(); int stack_push(const void * data); int stack_pop(void ** data); int stack_peek(void ** data); unsigned stack_size(); #endif

Client-Side Details

ADT Implementation

Implementation Details

Instance Data Structures

Static vs Dynamic Instances

ADT Operations

Implementation Details

Client-Instance Mapping

Mapping Validation

Objectives

Why Objects?

Why Objects in C?

Objects in C?

Shared Interfaces

Shared ADT Interfaces

Shared struct Fields


This page last modified on 10 May 2006.

This work is covered by a
Creative Commons License.

Function Pointers

Function Pointers in structs

Example

Implentation

static void triangle_draw(Triangle) {...}

Triangle triangle_new()

  Triangle t = malloc(struct Triangle)

  t→draw = triangle_draw
  t→area = triangle_area
  // and so on.

  return t

Parent Implentation

Interface

Some Problems

Shared Interfaces?

Shared vs Common

#define Sharing

#define Shape_Pubic_Interface \
  real (* area)(Shape); \
  void (* draw)(Shape); \
  void (* move)(Shape, real, real)

#define Circle_Circle_Interface \
  Shape_Public_Interface; \
  real (* get_radius)(Circle)

Shape Interface Example

#ifndef _shape_h_ #define _shape_h_ #define Shape_Pubic_Interface \ real (* area)(Shape); \ void (* draw)(Shape); \ void (* move)(Shape, real, real) typedef struct Shape { Shape_Public_Interface; } * Shape; #endif

Circle Interface Example

#ifndef _circle_h_ #define _circle_h_ #include "shape.h" #define Circle_Pubic_Interface \ Shape_Public_Interface; \ real (* get_radius)(struct Circle *) typedef struct Circle { Circle_Public_Interface; } * Circle; Circle circle_new(); void circle_delete(Circle); #endif

Some Problems

Differing Parameter Types

Subverted Parameter Types

Type Problems