Presentation is loading. Please wait.

Presentation is loading. Please wait.

PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ01DX - Overview of C and C++ Programming Language.

Similar presentations


Presentation on theme: "PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ01DX - Overview of C and C++ Programming Language."— Presentation transcript:

1 PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 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

2 PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 2 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 PDP- 11. 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,...

3 PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 3 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. Today, C and to some extent C++ have become the dominant languages used to develop systems software (operating systems, compilers, utility programs, video games, etc.)

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

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

6 PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 6 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

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

8 PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 8 C++ example (continued) 21 main() 22 {int j,k; 23 DataStore x; 24 while((k=x.initial(cin.get()))!=0) 25 {for(j=0;j<k;j++)x.save(cin.get()); 26 for(j=x.setprint();j>0;j--)cout<<x.printval(); 27 cout << "; SUM=" << x.sum() << endl; 28 while(cin.get()!='\n');}}


Download ppt "PZ01DX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ01DX - Overview of C and C++ Programming Language."

Similar presentations


Ads by Google