CPSC 427a: Object-Oriented Programming

Michael J. Fischer

Lecture 2
September 6, 2011

subsection in toc subsection in toc subsection in toc subsection in toc subsection in toc subsection in toc

C++ Language Design Goals

Why did C need a ++?

Chapter 2 of Exploring C++

  1. C was designed and constructed a long time ago (1971), as a language for writing Unix.
  2. The importance of data modeling was very poorly understood at that time.
  3. Data types were real, integer, character, and array, of various sizes and precisions.
  4. It was important for a C to be powerful and flexible, but not to have clean semantics.
  5. Nobody talked about portability and code re-use.

Today, we demand much more from a language.

C++ was Designed for Modeling Design goals for C++ (Bjarne Stroustrup)

  1. Provide classes (replacing structs) as a means to model data.
  2. Let a class encapsulate data, so that its implementation is hidden from a client program.
  3. Permit a C++ program to link to libraries from other languages, especially FORTRAN.
  4. Produce executable code that is as fast as C, unless run-time binding is necessary.
  5. Be fully compatible with C, so that C programs could be compiled under a C++ compiler and still work properly.

General properties of C++

C++ Extends C

Some Extensions in C++

Tools

Low-level: Command line tools

High-level: Integrated Development Environments (IDEs)

Recommended IDE’s The following are all open source and are installed on the Zoo. Geany Easy to use. Good for small projects. CodeBlocks Good general purpose IDE. Many advanced features, well-engineered, but not well-supported on Mac OS X. Eclipse/CDT Powerful, well-supported IDE, somewhat brittle, but getting better all the time.

Example

Generic Insertion Sort

Two implementations of simple insertion sort:

  1. C version: Written in object-oriented style to the extent possible in C.
  2. C++ version: Similar code but with C++ support

C version

See code demo 02-InsertionSortC.

C++ version

See code demo 02-InsertionSortCpp and following notes.