principles of object-oriented software development the language c++

14
Principles of Object- Oriented Software Development The language C++

Upload: ethel-allen

Post on 19-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Principles of Object-Oriented Software Development The language C++

Principles of Object-Oriented Software Development

The language C++

Page 2: Principles of Object-Oriented Software Development The language C++

The language C++

Introduction Terminology Expressions Control Objects Inheritance Techniques Summary

Page 3: Principles of Object-Oriented Software Development The language C++

C++ -- 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++

Page 4: Principles of Object-Oriented Software Development The language C++

Keywords:

• inline, new, delete, private, protected, public

C++ overview

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

Page 5: Principles of Object-Oriented Software Development The language C++

Some 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)

Page 6: Principles of Object-Oriented Software Development The language C++

Type expressions

• basic types -- int, char, float, ...

• array -- int ar[SIZE]

• function -- void f(int)

• pointer -- int* , char*,

• void (*f)(int)

• reference -- int&, char*&

• class, union, struct -- user-defined

Page 7: Principles of Object-Oriented Software Development The language C++

• operators -- + , - ,.., < , <= ,.., == , != ,.., && , ||

• indexing -- o[ e ]

• application -- o(...)

• access -- o.m(...)

• dereference -- p->m(...)

• in/decrement -- o++, o--

• conditional -- b?e:e2

Assignment

var = expression modifying -- +=. -=, ...

C++ -- expressions (2)

Page 8: Principles of Object-Oriented Software Development The language C++

Control

• conditional -- if (b) S1; else S2;

• selection -- switch(n) { case : n1: S1; break; ... default: ... }

• iteration -- while (b) S

• looping -- for( int i = 1; i <= MAX; i + + ) S

• jumps -- return, break, continue, goto

Page 9: Principles of Object-Oriented Software Development The language C++

ADT 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 << ctr_val(c); ctr* p = new ctr; ctr_init(*p); ctr_add(*p);

Page 10: Principles of Object-Oriented Software Development The language C++

ADT in C++

class ctr { public: ctr() { n = 0; } constructor ~ctr() { cout << "bye"; }; destructor void add( int i = 1) { n = n + i; } int val( ) { return n; } private: int n; }; Usage

ctr c; c.add(1); cout << c.val(); ctr* p = new ctr(); c->add(1); cout << c->val();

Page 11: Principles of Object-Oriented Software Development The language C++

Inheritance

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; } };

Page 12: Principles of Object-Oriented Software Development The language C++

Techniques

• templates -- template<classT> 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

Page 13: Principles of Object-Oriented Software Development The language C++

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 << (char*) c << " is " << c();

Page 14: Principles of Object-Oriented Software Development The language C++

The language C++

• design principles -- efficiency

• terminology -- object, type

• syntax -- object, reference, pointer

• objects -- abstract data types

• inheritance -- virtual functions

• techniques -- operators, templates, overloading