Title: Principles of Object-Oriented Software Development
1Principles of Object-Oriented Software Development
2The language C
Introduction Terminology
Expressions Control Objects
Inheritance Techniques Summary
3C -- is much more than a better C 1972 C
Kernigan and Ritchi (Unix) 1983 C
(Simula 1967) 1985 ANSI/ISO C 1996
ANSI/ISO C Design principles -- the
benefits of efficiency superset of C --
supporting OOP static typing -- with
user-defined exceptions explicit -- no
default virtual functions extensible --
libraries in C and C
4Keywords
C overview
- inline, new, delete, private, protected, public
Language features constructors -- to create
and initialize destructors -- to reclaim
resources virtual functions -- dynamic
binding (multiple) inheritance -- for
refinement type conversions -- to express
relations between types private, protected,
public -- for access protection friends --
to allow for efficient access
5Some basic terminology name -- denotes an
object, a function, set of functions, enumerator,
type, class member, template, a
value or a label introduced by a
declaration, used within a scope, and
has a type which determines its use. object --
region of storage a named object has a
storage class that determines its lifetime,
and the meaning of the values found in
an object is determined by the type of the
expression used to access it
C -- terminology (2)
6Type expressions
- basic types -- int, char, float, ...
- array -- int arSIZE
- function -- void f(int)
- pointer -- int , char,
- void (f)(int)
- reference -- int, char
- class, union, struct -- user-defined
7- operators -- , - ,.., lt , lt ,.., , ! ,..,
, - indexing -- o e
- application -- o(...)
- access -- o.m(...)
- dereference -- p-gtm(...)
- in/decrement -- o, o--
- conditional -- b?ee2
Assignment var expression modifying
-- . -, ...
C -- expressions (2)
8Control
- conditional -- if (b) S1 else S2
- selection -- switch(n) case n1 S1 break
... default ... - iteration -- while (b) S
- looping -- for( int i 1 i lt MAX i ) S
- jumps -- return, break, continue, goto
9ADT in C style
struct ctr int n void ctr_init(ctr c)
c.n 0 void ctr_add(ctr c, int i 1)
c.n c.n i int ctr_val(ctr c) return
c.n Usage ctr c ctr_init(c)
ctr_add(c,1) cout ltlt ctr_val(c) ctr p
new ctr ctr_init(p) ctr_add(p)
10ADT in C
class ctr public ctr() n 0
constructor ctr() cout ltlt "bye"
destructor
void add( int i 1) n n i int val(
) return n private int n
Usage ctr c c.add(1) cout ltlt c.val()
ctr p new ctr() c-gtadd(1) cout ltlt c-gtval()
11Inheritance
class A
ancestor
public A() n 0 void add( int i ) n
n i virtual int val() return n
protected
private would deny access to D int n
class D public A
descendant public D()
A() int val() return n \ 2
12Techniques
- templates -- templateltclassTgt class C ...
- overloading -- void read(int) voi read(float)
- friends -- to bypass protection
- type conversions -- by class constructors or type
operators - type coercion -- by explicit casts (is dangerous)
- smart pointers -- by overloading de-reference
13 class ctr
C techniques
public ctr(int i 0) n i ctr(const
char s "0") n itoa(s) void
operator() n n 1 int operator()()
return n operator char() return atoi(n)
private int n Usage ctr c
c cout ltlt (char) c ltlt " is " ltlt c()
14The language C
- design principles -- efficiency
- terminology -- object, type
- syntax -- object, reference, pointer
- objects -- abstract data types
- inheritance -- virtual functions
- techniques -- operators, templates, overloading