120 likes | 195 Views
CS 202 Computer Science II Lab. Fall 2009 September 10. Today Topics. Reusing programs (making header files) Script mounting your USB Copy-Paste in vi. Reusing Programs. Why reuse? Copy-paste the code Advantages: NONE Disadvantage: a lot!! Using header files
E N D
CS 202Computer Science II Lab Fall 2009 September 10
Today Topics • Reusing programs (making header files) • Script • mounting your USB • Copy-Paste in vi
Reusing Programs • Why reuse? • Copy-paste the code • Advantages: NONE • Disadvantage: a lot!! • Using header files • Object-oriented programming
Header file example… • Create headerExample directory • *do not delete files • Create basicMath.h file : • /* basicMath.h*/ • int add (int a, int b); • intmul (int a, int b);
.Header file example.. • Create basicMath.cpp file : • /* basicMath.cpp */ • int add (int a, int b) { • return a+b; • } • intmul (int a, int b) { • return a*b; • }
..Header file example. • Create main.cpp file : • #include <iostream> • #include "basicMath.h" • using namespace std; • int main(int argc, char ** argv) { • int a, b; • cout << " Name: Your-Name " << endl; • cout << " Section: Your-Section-No " << endl; • if (argc > 2) { • a = atoi(argv[1]); • b = atoi(argv[2]); • } else { • a = 1; • b = 2; • } • cout << a << " + "<< b << " = " << add(a, b) << endl; • cout << a << " * "<< b << " = " << mul(a, b) << endl; • return 0; • }
…Header file example • g++ -c main.cpp • g++ -c basicMath.cpp • Or: • g++ -c *.cpp • g++ -o main main.obasicMath.o • ./main 12 5 > output.out
Exercise • After successfully running the program, add the following function to “basicMath.cpp”, and try to make it work by editing main.cpp & basicMath.h int min (int a, int b) { return (a < b) ? a : b; } • Testing Program • ./main • ./main > output.out • Print output.out • lproutput.out
Script Script [file name] … exit Default file name: typescript
mounting your USB • df -T to see list of mounted devices • mount /dev/fd0 /mnt/floppy • mount /dev/cdrom /mnt/cdrom • mount /dev/sda1 /mnt/usbdisk
Copy-Paste in vi • (n)yy (n) lines to buffer • y(n)w yanks (n) words to buffer • p puts yanked or deleted text after cursor • P puts yanked or deleted text before cursor