procedure-based programming
DESCRIPTION
Procedure-Based Programming. main(). first function invoked at execution. func1(). func2(). func3(). func4(). The functions within a program communicate through values that they receive and through values that they return. Procedural programming language designed by D. Ritchie - PowerPoint PPT PresentationTRANSCRIPT
Procedure-Based Programming
main()
func1() func2() func3()
func4()
The functions within a program communicate through values
that they receive and through values that they return.
first function invoked at execution
...
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
The two principles are implemented in C++ through the “class” facility.
Only the implementer cares about the implementation details.
Users care about the interface and operations of a new data type.
A programming paradigm based on two principles:
Information hiding – keep the interface separate from the implementation.
Encapsulation – keep the data and the functions that manipulate them in one place.
Program with a black box!
New data types can be created and manipulated as easily as built-in types.
C++
External Program UnitsClass Members
public: Data Functions
private: Data Functions
data items + functions (operators) = programs
A superset of C (except for minor details)
better in type checking and expressing modularity
public interface + private implementation
retains C’s efficiency dealing with hardware (bits, bytes, words etc.)
8 3 26 3 8 6 2
2 8 6 3 2 6 8 3
32 8 6 2 63 8
Example – Exchange Sort
C++ Code #include <iostream.h>
// interchange the values of the two integer variables x and yvoid Swap(int &x, int &y){ int temp = x; // store original value of x
x = y; // replace x by y y = temp; // assign y the original value of x}
// sort the n element integer array a in ascending order.void ExchangeSort(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 PrintList(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"; PrintList(list, 15); ExchangeSort(list,15); cout << endl << "Sorted List" << endl; PrintList(list, 15);}
Header and Source Files
// File add.h#ifndef ADD_H #define ADD_H
int add(int, int);
#endif
A header file declares each function, object, and data type that is part of the public interface
// File add.cpp#include "add.h"
int add(int a, int b) { return a + b; }
Its implementation (definition) lies in a source file.
The complier will check the declaration and definition for consistency.
ifndef tests whether ADD_H has been definedpreviously.
If not, define it. This guarantees that the headerfile is processed only once in case multiple source files include it.
The User Code
// File triple.cpp#include "add.h"
int triple(int x) { return add(x, 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 Makefiletriple.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$ triple
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.
generate files add.o, triple.o, and triple.exeaccording to their dependencies.
Unix File System (1969)
bin dev etc tmp usr unix boot
you mike paul mary
junk junk temp junk data
The Unix Programming Environmentby B. W. Kernighan and R. Pike, Prentice-Hall, Inc., 1984. ISBN 0-13-937681-Xfor more see http://en.wikipedia.org/wiki/UNIX
The Directory Hierarchy
/ root of the file system/bin essential programs in executable form/dev device files /etc system miscellany/etc/passwd password file /lib essential libraries, etc./tmp temporary files; cleaned at system restart/usr user file system/usr/adm system administration/usr/bin user binaries/usr/include header files for C programs, e.g. math.h/usr/lib libraries for C, FORTRAN/usr/man on-line manual/usr/src source code for utilities and libraries /usr/you your login directory/usr/you/bin your personal programs
Your Home DirectoryAll 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
return to the parent directory
print working directory
Tree Structure
/usr/you
junk recipes
pie cookie
apple crust choc.chip
Command duTells how much disc space (in bytes) is consumed by the files in a directory, including all its subdirectories.
$ pwd/usr/you/$ du 6 ./recipes/pie7 ./recipes/cookies11 ./recipes13 .$
current directory
/usr/you
junk recipes
pie cookie
apple crust choc.chip
Print out All the Files
Use the option -a, for “all”.
/usr/you
junk recipes
pie cookie
apple crust choc.chip
$ du -a 2 ./recipes/pie/apple3 ./recipes/pie/crust6 ./recipes/pie7 ./recipes/cookie/choc.chip4 ./recipes/cookie11 ./recipes12 ./junk13 .$
Use of Regular Expressions
/usr/you
junk recipes
pie cookie
apple crust choc.chip
$ pwd/usr/you$ cd r*$ pwd /usr/you/recipes$ cd *ie*ie: Ambiguous$ cd c*ie$ cd ../p*$ pwd /usr/you/recipes/pie$ cat *e [contents of the file apple]
A convenient means of referring to files/directories as long as ambiguities would not arise.
r* zero or more characters beginning with rr+ one or more characters beginning with rr? r followed by any single character ? any single character
Looking up a Command$ man cd
BASH_BUILTINS(1) BASH_BUILTINS(1)
NNAAMMEE 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, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit, umask, unalias, unset, wait - bash built-in commands, see bbaasshh(1)
BBAASSHH BBUUIILLTTIINN CCOOMMMMAANNDDSS 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.
PermissionsEvery 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 30at 10:30am, and has one link.
-rw-r--r--
ordinaryfile root may read or write
but not execute
people in adm group can read but not write or execute.
the rest of the users can only read. represented by 644 (i.e. 110100100)
Change Permissions
$ chmod 666 junk
Change the permissions on junk to “rw-rw-rw-” (i.e. 110110110).
$ chmod +x command
Allow everyone to execute command:
Turn off write permission for everyone
$ chmod -w file
Remove a File or Directory
/usr/you
junk recipes
pie cookie
apple crust choc.chip
$ pwd/usr/you$ cd recipes/pie$ pwd/usr/you/recipes/pie$ ls apple crust$ rm apple crust$ ls$ cd ..; pwd/usr/you/recipes$ rmdir pie$ rmdir cookiesrmdir: `cookies': Directory not empty
execute two commandssequentially
Copy and Move Files
/usr/you
junk recipes
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
copyofjunk
choc.chip nojunk
EditorsYou can use vi or troff.But most people prefer emacs.(http://www.delorie.com/gnu/docs/emacs/emacs_toc.html)
$ emacsStart emacs
$ emacs -nw or
Basic commands within the emacs windowC-x C-w write to a fileC-x C-s save to the current fileC-x C-f load a fileC-s search a stringEsc-x replace-string replace all occurrences of a stringEsc-% query replaceC-v roll one page forwardEsc-v roll one page backwardC-x C-c exit the Emacs windowC-x C-k kill a buffer
$ emacs &