School Work

An Introduction to C and Object Oriented Programming

Description
OOP
Categories
Published
of 191
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  An Introduction toC++ andObject Oriented Programming Abstract The aim of the notes is to provide an introduction to the C++ programming language andobject oriented programming. It is assumed that you know one programming language mod-erately well. Author:  Ian D Chivers Email:  ian.chivers@kcl.ac.uk  Version : 6.1 Date:  August 1999© Ian D Chivers. Permission to copy all or part of this work is granted, provided that thecopies are not made or distributed for resale (except a nominal copy fee may be charged),and provided that the Author, Copyright, & No Warranty sections are retained verbatim andare displayed conspicuously. If anyone needs other permissions that aren't covered by theabove, please contact the author.No Warranty: this work is provided on an as is basis. The author provides no warrantywhatsoever, either express or implied, regarding the work, including warranties with respectto its merchantability or fitness for any particular purpose. All comments welcome.  Table of Contents 1 Overview...............................................................................................................14 1.1 Aims............................................................................................................................141.2 Assumptions ...............................................................................................................141.3 Additional Material and Recommended Sources ......................................................141.4 Compilers and Standards............................................................................................15 1.4.1 Contents of the standard...................................................................................................16 1.5 Old and New...............................................................................................................171.6 Coda............................................................................................................................171.7 Course Details ............................................................................................................181.8 Problems.....................................................................................................................18 2 An Introduction to Programming Languages and Object Oriented Pro-gramming...................................................................................................................20 2.1 Fortran 66, 1966.........................................................................................................202.2 Pascal, 1975, ANSI & BSI 1982, ISO 1983, Extended Pascal 1991?......................202.3 Fortran 77, 1978.........................................................................................................212.4 C, K&R 1978, Standard 1989....................................................................................212.5 Modula 2, 1982, Standard 1996?...............................................................................212.6 Ada, ISO 8652: 1987 .................................................................................................212.7 C++, 1986, Standard November 1997.......................................................................212.8 Oberon 2, Late 1980's, early 1990's...........................................................................222.9 Fortran 90, 1991.........................................................................................................222.10 Eiffel, 1988.................................................................................................................222.11 Ada, ISO 8652: 1995 .................................................................................................222.12 Java.............................................................................................................................232.13 Visual Basic................................................................................................................232.14 Language Comparison................................................................................................232.15 Language Features......................................................................................................25 2.15.1 Independent Compilation.................................................................................................252.15.2 Separate Compilation.......................................................................................................252.15.3 Concrete Data Types........................................................................................................252.15.4 Abstract Data Types.........................................................................................................252.15.5 Dynamic arrays.................................................................................................................252.15.6 Numeric and General Polymorphism...............................................................................252.15.7 Modules ...........................................................................................................................262.15.8 Pointers and References...................................................................................................262.15.9 Procedure Variables .........................................................................................................262.15.10 Inheritance........................................................................................................................262.15.11 Dynamic Binding .............................................................................................................262.15.12 Operator Overloading.......................................................................................................262.15.13 Threads/Multitasking........................................................................................................262.15.14 Exception Handling..........................................................................................................27 2.16 Some Important Milestones in Program Language Development ............................27 2.16.1 Structured Programming ..................................................................................................272.16.2 Stepwise Refinement........................................................................................................272.16.3 Data Structuring, Concrete vs Abstract Data Types........................................................272.16.4 Information Hiding – Modules ........................................................................................27 2.17 Terminology of Object Oriented Programming.........................................................272.18 Parallel Developments................................................................................................27 2.18.1 Parallel Fortran – Fortran 95, Fortran 2000, SMP, MPI, HPF .......................................282.18.2 Parallel C++......................................................................................................................28 2.19 Object Oriented Programming...................................................................................282.20 Object Oriented Languages........................................................................................29 2.20.1 Simula – 1967 ..................................................................................................................292.20.2 Smalltalk – 1978 ..............................................................................................................292.20.3 C++...................................................................................................................................292.20.4 Eiffel.................................................................................................................................292.20.5 Oberon 2...........................................................................................................................292.20.6 Ada 95 ..............................................................................................................................302.20.7 Java...................................................................................................................................30 2.21 Other Languages.........................................................................................................31 Contents 3  2.21.1 Fortran 90.........................................................................................................................312.21.2 Modula 2...........................................................................................................................31 2.22 The OO Approach......................................................................................................31 2.22.1 Meyer's Approach ............................................................................................................312.22.2 Rumbaugh et al ................................................................................................................312.22.3 Practical Steps ..................................................................................................................32 2.23 Simple Example..........................................................................................................322.24 Other Developments...................................................................................................33 2.24.1 Development Environments.............................................................................................332.24.2 Graphical Development Tools .........................................................................................342.24.3 Software Components ......................................................................................................342.24.3.1 COM, OLE, ActiveX.................................................................................................342.24.3.2 JavaBeans...................................................................................................................35 2.25 Coda............................................................................................................................352.26 Bibliography...............................................................................................................362.27 Problems.....................................................................................................................41 3 An Introduction to C++......................................................................................44 3.1 Hello World – Old C style.........................................................................................443.2 Hello World – New standard C++ style....................................................................443.3 Simple text i/o using C style arrays of char..............................................................453.4 Simple text i/o using C++ style strings .....................................................................463.5 Simple numeric i/o.....................................................................................................463.6 Some C++ Rules and Terminology ...........................................................................473.7 Good Programming Guidelines..................................................................................483.8 C++ for C Programmers.............................................................................................48 3.8.1 Macros..............................................................................................................................483.8.2 Malloc...............................................................................................................................483.8.3 Pointers.............................................................................................................................483.8.4 Arrays and C style strings................................................................................................48 3.9 C++ Character Set......................................................................................................483.10 Summary.....................................................................................................................493.11 Key Concepts..............................................................................................................49 3.11.1 Basic structure of a C++ program ...................................................................................493.11.2 Indentation........................................................................................................................493.11.3 Data Types........................................................................................................................49 3.12 Bibliography...............................................................................................................503.13 Problems.....................................................................................................................50 4 Arithmetic and Expressions in C++..................................................................54 4.1 Basic numeric types ...................................................................................................544.2 Integer Numeric Type ................................................................................................54 4.2.1 Variations on a theme - signed, unsigned........................................................................54 4.3 Real Numeric Type ....................................................................................................554.4 Numeric Type Conversion Rules...............................................................................554.5 Complex?....................................................................................................................564.6 const............................................................................................................................564.7 Character Data as a form of Integer Data..................................................................564.8 Operators and Expression Evaluation........................................................................57 4.8.1 Expression Evaluation......................................................................................................574.8.2 Sequence Points................................................................................................................574.8.3 Lvalue and Rvalue............................................................................................................574.8.4 Operators, Precedence and Associativity.........................................................................574.8.4.1 :: [scope resolution] class_name :: member..............................................................594.8.4.2 :: [global] :: name......................................................................................................594.8.4.3 . [member selection] object.member.........................................................................594.8.4.4 -> [member selection] pointer -> member................................................................594.8.4.5 [] [subscripting] pointer [expr]..................................................................................594.8.4.6 () [function call] expr (expr_list)...............................................................................594.8.4.7 () [value construction] type(expr_list).......................................................................594.8.4.8 ++ [post increment] lvalue ++...................................................................................594.8.4.9 — [post decrement] lvalue —...................................................................................594.8.4.10 sizeof [size of object] sizeof expr .............................................................................594.8.4.11 sizeof [size of type] sizeof (type)..............................................................................59 4 Contents  4.8.4.12 ++ [pre increment] ++ lvalue....................................................................................594.8.4.13 — [pre decrement] — lvalue.....................................................................................604.8.4.14 ~ [complement] ~ expr..............................................................................................604.8.4.15 ! [not] ! expr ..............................................................................................................604.8.4.16 - [unary minus] - expr................................................................................................604.8.4.17 + [unary plus] + expr.................................................................................................604.8.4.18 & [address of] & expr................................................................................................604.8.4.19 * [dereference] * expr................................................................................................604.8.4.20 new [create] new type................................................................................................604.8.4.21 delete [destroy] delete pointer...................................................................................604.8.4.22 delete[] [destroy array] delete [] pointer...................................................................604.8.4.23 () [cast] (type) expr....................................................................................................604.8.4.24 .* [member selection] object.* pointer_to_member..................................................604.8.4.25 ->* [member selection] pointer -> * pointer_to_member ........................................604.8.4.26 * [multiply] expr * expr ............................................................................................604.8.4.27 / [divide] expr / expt..................................................................................................604.8.4.28 % [modulo or remainder] expr % expr.....................................................................614.8.4.29 + [plus] expr + expr...................................................................................................614.8.4.30 - [minus] expr - expr..................................................................................................614.8.4.31 << [shift left] expr << expr.......................................................................................614.8.4.32 >> [shift right] expr >> expr.....................................................................................614.8.4.33 < [less than] expr < expr ...........................................................................................614.8.4.34 <= [less than or equal] expr <= expr ........................................................................614.8.4.35 > [greater than] expr > expr......................................................................................614.8.4.36 >= [greater than or equal] expr >= expr ...................................................................614.8.4.37 == [equal] expr == expr............................................................................................614.8.4.38 != [not equal] expr != expr........................................................................................614.8.4.39 & [bitwise AND] expr & expr ..................................................................................614.8.4.40 ^ [bitwise exclusive OR] expr ^ expr........................................................................614.8.4.41 | [bitwise inclusive OR] expr | expr ..........................................................................614.8.4.42 && [logical AND] expr && expr.............................................................................614.8.4.43 || [logical inclusive OR] expr || expr .........................................................................614.8.4.44 ?: [conditional expression] expr ? expr : expr ..........................................................624.8.4.45 = [conventional assignment] lvalue = expr...............................................................624.8.4.46 *= [multiply and assign] lvalue *= expr...................................................................624.8.4.47 /= [divide and assign] lvalue /= expr ........................................................................624.8.4.48 %= [modulo and assign] lvalue %= expr..................................................................624.8.4.49 += [add and assign] lvalue += expr ..........................................................................624.8.4.50 -= [subtract and assign] lvalue -= expr.....................................................................624.8.4.51 <<= [shift left and assign] lvalue <<= expr..............................................................624.8.4.52 >>= [shift right and assign] lvalue >>= expr............................................................624.8.4.53 &= [AND and assign] lvalue &= expr......................................................................624.8.4.54 |= [inclusive OR and assign] lvalue |= expr..............................................................624.8.4.55 ^= [exclusive OR and assign] lvalue ^= expr...........................................................624.8.4.56 throw [throw exception] throw expr..........................................................................624.8.4.57 , [comma] expr , expr................................................................................................62 4.9 Expression Examples..................................................................................................624.10 Summary.....................................................................................................................644.11 Key Concepts..............................................................................................................65 4.11.1 Numeric Data Types ........................................................................................................654.11.1.1 Integer ........................................................................................................................654.11.1.2 Real ............................................................................................................................654.11.1.3 Complex – supported via <complex>.......................................................................654.11.1.4 Character Data as Numeric Data...............................................................................654.11.2 Constants – use the const attribute ..................................................................................654.11.3 Operators – 45 but effectively 57 with variants..............................................................654.11.4 Expressions – the order of expression evaluation is often undefined.............................65 4.12 Problems.....................................................................................................................65 5 Strings and other Data Types............................................................................68 5.1 Character Data or Strings...........................................................................................68 5.1.1 C Style strings..................................................................................................................685.1.1.1 Example 1..................................................................................................................685.1.1.2 Example 2..................................................................................................................685.1.1.3 strcpy(s1,s2)...............................................................................................................69 Contents 5
Search
Tags
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks