principles of object-oriented software development the language c++
Post on 19-Jan-2016
215 Views
Preview:
TRANSCRIPT
Principles of Object-Oriented Software Development
The language C++
The language C++
Introduction Terminology Expressions Control Objects Inheritance Techniques Summary
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++
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
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)
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
• 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)
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
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);
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();
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; } };
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
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();
The language C++
• design principles -- efficiency
• terminology -- object, type
• syntax -- object, reference, pointer
• objects -- abstract data types
• inheritance -- virtual functions
• techniques -- operators, templates, overloading
top related