Sei sulla pagina 1di 8

PZ01DX - Overview of C and C++

Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 1.5 Section 6.5 Appendix A.2 Appendix A.3

PZ01DX

Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C overview
C developed in 1972 by Dennis Ritchie and Ken Thompson at AT&T Bell Telephone Laboratories Developed as language to implement UNIX on a DEC PDP11. UNIX was a then small operating system to compete with the large Multics of MIT and GE. C is more of an environment than a simple language: The C language The C preprocessor (#include, #if, ...) The C interface assumptions (.h include files) The C library (Built-in functions like printf, malloc, ...

PZ01DX

Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C program structure
C program is a sequence of procedures and global declarations Each procedure contains local declarations, imperative statements, which can call other procedures Most data are integer data. This allows full flexibility since almost everything is an integer.

More about C data storage later in semester.


Today C and to some extent C++ have become the dominant language used to develop systems software (operating systems, compilers, utility programs, video games, etc.)

PZ01DX

Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> const int maxsize=9; Figure A.5 in text main() {int a[maxsize]; int j,k; while( (k=convert(getchar())) != 0) { for(j=0;j<k;j++)a[j]=convert(getchar()); for(j=0; j<k; j++) printf("%d ", a[j]); printf("; SUM= %d\n", addition(a,k)); while(getchar() != '\n'); }} /* Function convert subprogram */ int convert(char ch) {return ch-'0';} /* Function addition subprogram */ int addition(v, n) int v[ ], n; {int sum,j; sum=0; for(j=0; j<n; j++) sum=sum+v[j]; return sum; }
PZ01DX Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> C language const int maxsize=9; C preprocessor main() C interface assumptions {int a[maxsize]; C library int j,k; while( (k=convert(getchar())) != 0) { for(j=0;j<k;j++)a[j]=convert(getchar()); for(j=0; j<k; j++) printf("%d ", a[j]); printf("; SUM= %d\n", addition(a,k)); while(getchar() != '\n'); }} /* Function convert subprogram */ int convert(char ch) {return ch-'0';} /* Function addition subprogram */ int addition(v, n) int v[ ], n; {int sum,j; sum=0; for(j=0; j<n; j++) sum=sum+v[j]; return sum; }
PZ01DX Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C++ overview
Developed by Bjarne Stroustrup at AT&T in 1986 as an extension to C. C++ includes essentially all of C (both good and bad features) Improves on C by adding strong typing Goal was to keep efficiency of C execution, while adding new features C++ adds concept of a class (borrowed from Simula, which was a simulation language developed from Algol). Data was defined local to a class Functions (methods) could access this local data Implemented concept of inheritance of classes

PZ01DX

Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C++ example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <stream.h> Figure A.7 in text class DataConvert { protected: int convert(char ch) {return ch-'0';}}; class DataStore: DataConvert{ public: int initial(char a) {ci=0; return size = convert(a);}; void save(char a) {store[ci++]=convert(a);}; int setprint() { ci=0; return size;}; int printval() { return store[ci++];}; Classes int sum() Methods {int arrsum; arrsum=0; for(ci=0;ci<size;ci++)arrsum=arrsum+store[ci]; return arrsum;} private: const int maxsize=9; int size; // Size of array int ci; // Current index into array int store[maxsize];}; // end class DataStore
PZ01DX Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

C++ example (continued)


21 22 23 24 25 26 27 28 main() {int j,k; DataStore x; while((k=x.initial(cin.get()))!=0) {for(j=0;j<k;j++)x.save(cin.get()); for(j=x.setprint();j>0;j--)cout<<x.printval(); cout << "; SUM=" << x.sum() << endl; while(cin.get()!='\n');}}

PZ01DX

Programming Language design and Implementation -4th Edition CopyrightPrentice Hall, 2000

Potrebbero piacerti anche