C++ -- is much more than a better C

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

slide: The language C++


Keywords:

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

slide: C++ -- terminology (1)


Some basic terminology

name -- denotes an object, a function, set of functions, enumerator, type, class member, template, a value or a label

object -- region of storage


slide: C++ -- terminology (2)


Type expressions


slide: C++ -- expressions (1)


Expressions

  • operators -- + , - ,.., < , <= ,.., == , != ,.., && , ||
  • indexing -- o[ e ]
  • application -- o(...)
  • access -- o.m(...)
  • dereference -- p->m(...)
  • in/decrement -- o++, o--
  • conditional -- b?e1:e2

Assignment

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

slide: C++ -- expressions (2)


Control


slide: C++ -- control


ADT in C style


  struct ctr { int n; }
  
  void ctr_init(ctr& c) { c.n = 0; }
  void ctr_add(ctr& c, int i) { c.n = c.n + i; }
  int ctr_val(ctr& c) { return c.n; }
  

Usage


  ctr c; ctr_init(c); ctr_add(c,1); 
  ctr* p = new ctr; ctr_init(*p); ctr_add(*p);  
  

slide: C++ -- objects (1)


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); ...
  

slide: C++ -- objects (2)


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

slide: C++ -- inheritance


Techniques


slide: C++ -- techniques (1)



  class ctr { 
C++
public: ctr(int i = 0, char* x = "ctr") { n = i; strcpy(s,x); } ctr& operator++(int) { n++; return *this; } int operator()() { return n; } operator int() { return n; } operator char*() { return s; } private: int n; char s[64]; };

Usage


  ctr c; cout << (char*) c++ << "=" << c();
  

slide: C++ - techniques (2)


The language C++

C



slide: C++ -- summary