OutlineC++ OverviewC++ GoalsComparison of C and C++ToolsExampleInsertion sortC versionC++ versionOutline C++ Overview ExampleCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 2September 6, 2011CPSC 427a 1/14Outline C++ Overview ExampleC++ OverviewC++ GoalsComparison of C and C++ToolsExampleInsertion sortC versionC++ versionCPSC 427a 2/14Outline C++ Overview ExampleC++ Language Design GoalsCPSC 427a 3/14Outline C++ Overview ExampleC++ GoalsWhy did C need a ++?Chapter 2 of Exploring C++1. C was designed and constructed a long time ago (1971), as alanguage for writing Unix.2. The importance of data modeling was very poorly understoodat that time.3. Data types were real, integer, character, and array, of varioussizes and precisions.4. It was important for a C to be powerful and flexible, but notto have clean semantics.5. Nobody talked about portability and code re-use.Today, we demand much more from a language.CPSC 427a 4/14Outline C++ Overview ExampleC++ GoalsC++ was Designed for ModelingDesign 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 ishidden from a client program.3. Permit a C++ program to link to libraries from otherlanguages, especially FORTRAN.4. Produce executable code that is as fast as C, unless run-timebinding is necessary.5. Be fully compatible with C, so that C programs could becompiled under a C++ compiler and still work properly.CPSC 427a 5/14Outline C++ Overview ExampleC++ GoalsGeneral properties of C++IWidely used in the real world.IClose to the machine and capable of producing efficient code.IGives a programmer fine control over the use of resources.ISupports the object-oriented programming paradigm.ISupports modularity and component isolation.ISupports correctness through privacy, modularity, and use ofexceptions.ISupports reusabale code through derivation and templates.CPSC 427a 6/14Outline C++ Overview ExampleComparison of C and C++C++ Extends CIC++ grew out of C.IGoals were to improve support for modularity, portability, andcode reusability.IMost C programs will compile and run under C++.IC++ replaces several problematic C constructs with saferversions.IAlthough most old C constructs will still work in C++, severalshould not be used in new code where better alternatives exist.Example: Use Boolean constants true and false instead of1 and 0.CPSC 427a 7/14Outline C++ Overview ExampleComparison of C and C++Some Extensions in C++IComments // (now in C99)IExecutable declarations (now in C99)IType bool (now in C99)IEnumeration constants are not synonyms for integersIReference typesIDefinable type conversions and operator extensionsIFunctions with multiple methodsIClasses with private parts; class derivation.IClass templatesIAn exception handler.CPSC 427a 8/14Outline C++ Overview ExampleToolsToolsLow-level: Command line toolsIA text editor such as emacs or vi.IThe compiler suite: g++.IProject management: makeHigh-level: Integrated Development Environments (IDEs)IIntegrate various low-level toolsIFacilitate development cycleCPSC 427a 9/14Outline C++ Overview ExampleToolsRecommended IDE’sThe 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 onMac OS X.Eclipse/CDT Powerful, well-supported IDE, somewhat brittle, butgetting better all the time.CPSC 427a 10/14Outline C++ Overview ExampleExampleCPSC 427a 11/14Outline C++ Overview ExampleInsertion sortGeneric Insertion SortTwo implementations of simple insertion sort:1. C version: Written in object-oriented style to the extentpossible in C.2. C++ version: Similar code but with C++ supportCPSC 427a 12/14Outline C++ Overview ExampleC versionC versionSee code demo 02-InsertionSortC.CPSC 427a 13/14Outline C++ Overview ExampleC++ versionC++ versionSee code demo 02-InsertionSortCpp and following notes.CPSC 427a
View Full Document