ProcedureBased Programming main func 1 func 2 first
Procedure-Based Programming main() func 1() func 2() . . . first function invoked at execution func 3() func 4() The functions within a program communicate through values that they receive and through values that they return.
C Procedural programming language designed by D. Ritchie to implement the UNIX operating system (1972). Provides low-level memory access – very efficient! Provides language constructs that map efficiently to machine instructions Requires minimal run-time support. Data structures such as arrays or structs allow too easy access and modification. Compromise of data integrity decreases program quality and software productivity.
Object-Oriented Programming A programming paradigm based on two principles: Information hiding – keep the interface separate from the implementation. Users care about the interface and operations of a new data type. Only the implementer cares about the implementation details. Encapsulation – keep the data and the functions that manipulate them in one place. Program with a black box! The two principles are implemented in C++ through the “class” facility. New data types can be created and manipulated as easily as built-in types.
C++ A superset of C (except for minor details) better in type checking and expressing modularity data items + functions (operators) = programs retains C’s efficiency dealing with hardware (bits, bytes, words etc. ) public interface + private implementation Class Members public: Data Functions External Program Units private: Data Functions
Example – Exchange Sort 8 3 6 2 3 8 6 2 2 8 6 3 2 6 8 3 2 3 8 6 2 3 6 8
C++ Code #include <iostream. h> // interchange the values of the two integer variables x and y void Swap(int &x, int &y) { int temp = x; // store original value of x x = y; y = temp; // replace x by y // assign y the original value of x } // sort the n element integer array a in ascending order. void Exchange. Sort(int a[ ], int n) { int i, j; /* implement n-1 passes. locate correct values in a[0], . . . , a[n-2]. */ for (i=0; i<n-1; i++) // put minimum of a[i+1]. . . a[n-1] in a[i] for (j=i+1; j<n; j++) // exchange if a[i]>a[j] if (a[i] > a[j]) Swap(a[i], a[j]); } // step through the list and print each value. void Print. List(int a[ ], int n) { for (int i=0; i<n; i++) cout << a[i] << " "; cout << endl; } void main() { int list[15] = {38, 58, 13, 15, 51, 27, 10, 19, 12, 86, 49, 67, 84, 60, 25}; cout << "Original List n"; Print. List(list, 15); Exchange. Sort(list, 15); cout << endl << "Sorted List" << endl; Print. List(list, 15); }
Header and Source Files A header file declares each function, object, and data type that is part of the public interface // File add. h #ifndef ADD_H #define ADD_H int add(int, int); #endif ifndef tests whether ADD_H has been defined previously. If not, define it. This guarantees that the header file is processed only once in case multiple source files include it. Its implementation (definition) lies in a source file. // File add. cpp #include "add. h" int add(int a, int b) { return a + b; } The complier will check the declaration and definition for consistency.
The User Code // File triple. cpp #include "add. h" int triple(int x) { return add(x, x)); } When the definition of add changes in add. cpp, triple. c needs not be modified, unless the declaration of add changes in add. h. So triple does not need to know the implementation details of the add function. It reduces the maintenance burden.
Compile & Execute -- Makefile and make $ cat Makefile triple. exe: triple. o add. o g++ -o triple. exe triple. o add. o triple. o: triple. cpp add. h g++ -c triple. cpp add. o: add. cpp add. h g++ -c add. cpp $ make generate files add. o, triple. o, and triple. exe $ triple according to their dependencies. triple. exe depends on triple. o and add. o, and is generated by running the g++ complier on them. triple. o depends on triple. cpp and add. h. add. o depends on add. cpp and add. h. Note “g++ -c” instead of “g++ -o” is used to generate them.
Unix File System (1969) bin dev junk etc tmp you mike usr paul unix mary junk temp for more see http: //en. wikipedia. org/wiki/UNIX boot junk data The Unix Programming Environment by B. W. Kernighan and R. Pike, Prentice-Hall, Inc. , 1984. ISBN 0 -13 -937681 -X
The Directory Hierarchy / /bin /dev /etc/passwd /lib /tmp /usr/adm /usr/bin /usr/include /usr/lib /usr/man /usr/src /usr/you/bin root of the file system essential programs in executable form device files system miscellany password file essential libraries, etc. temporary files; cleaned at system restart user file system administration user binaries header files for C programs, e. g. math. h libraries for C, FORTRAN on-line manual source code for utilities and libraries your login directory your personal programs
Your Home Directory All files of yours start with /usr/you. $ ls junk file name /usr/you/junk $ pwd /usr/you $ mkdir recipes $ cd recipes $ pwd /usr/you/recipes $ mkdir pie cookie $ ls pie cookie $ cd. . $ pwd /usr/you print working directory return to the parent directory
Tree Structure /usr/you recipes junk pie apple cookie crust choc. chip
Command du Tells how much disc space (in bytes) is consumed by the files in a directory, including all its subdirectories. /usr/you recipes junk pie apple cookie crust choc. chip $ pwd /usr/you/ $ du 6. /recipes/pie 4. /recipes/cookies 11. /recipes 13. $ current directory
Print out All the Files Use the option -a, for “all”. /usr/you recipes junk pie apple cookie crust choc. chip $ du -a 2. /recipes/pie/apple 3. /recipes/pie/crust 6. /recipes/pie 3. /recipes/cookie/choc. chip 4. /recipes/cookie 11. /recipes 1. /junk 13. $
Use of Regular Expressions A convenient means of referring to files/directories as long as ambiguities would not arise. r* r+ r? ? zero or more characters beginning with r one or more characters beginning with r r followed by any single character /usr/you recipes junk pie apple cookie crust choc. chip $ pwd /usr/you $ cd r* $ pwd /usr/you/recipes $ cd *ie: Ambiguous $ cd c*ie $ cd. . /p* $ pwd /usr/you/recipes/pie $ cat *e [contents of the file apple]
Looking up a Command $ man cd BASH_BUILTINS(1) N�NA�AM�ME�E bash, : , . , [, alias, bg, bind, break, builtin, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf, pushd, pwd, readonly, return, set, shift, shopt, source, suspend, test, times, trap, typeset, ulimit, umask, unalias, unset, wait - bash built-in commands, see b�ba�as�sh�h(1) B�BA�AS�SH�H B�BU�UI�IL�LT�TI�IN�N C�CO�OM�MM�MA�AN�ND�DS�S Unless otherwise noted, each builtin command documented in this section as accepting options preceded by -�- accepts -�--�- to signify the end of the options. : �: [_�a_�r_�g_�u_�m_�e_�n_�t_�s] No effect; the command does nothing beyond expanding _�a_�r_�g_�u_�m_�e_�n_�t_�s and performing any specified redirections. A zero exit code is returned.
Permissions Every file has a set of permissions associated with it, which determines who can do what with the file. The -l option of ls prints the permissions. $ ls -l /etc/passwd -rw-r--r-- 1 root 5115 Aug 30 10: 40 /etc/passwd Owned by root, 5115 bytes long, last modified on August 30 at 10: 30 am, and has one link. -rw-r--r-ordinary file represented by 644 (i. e. 110100100) the rest of the users can only read. people in adm group can read but not write or execute. root may read or write but not execute
Change Permissions Change the permissions on junk to “rw-rw-rw-” (i. e. 110110110). $ chmod 666 junk Allow everyone to execute command: $ chmod +x command Turn off write permission for everyone $ chmod -w file
Remove a File or Directory /usr/you recipes junk pie apple cookie crust choc. chip $ pwd /usr/you $ cd recipes/pie $ pwd /usr/you/recipes/pie $ ls apple crust $ rm apple crust $ ls execute two $ cd. . ; pwd commands /usr/you/recipes sequentially $ rmdir pie $ rmdir cookies rmdir: `cookies': Directory not empty
Copy and Move Files /usr/you junk nojunk recipes copyofjunk choc. chip cookie choc. chip $ pwd /usr/you $ cp junk recipes/copyofjunk $ cd recipes $ mv cookie/choc. chip. . $ cd. . $ ls junk recipes choc. chip $ mv junk nojunk
Editors You can use vi or troff. But most people prefer emacs. (http: //www. delorie. com/gnu/docs/emacs_toc. html) Start emacs $ emacs & or $ emacs -nw Basic commands within the emacs window C-x C-s C-x C-f C-s Esc-x replace-string Esc-% C-v Esc-v C-x C-c C-x C-k write to a file save to the current file load a file search a string replace all occurrences of a string query replace roll one page forward roll one page backward exit the Emacs window kill a buffer
- Slides: 22