Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 202 Computer Science II Lab Fall 2009 September 10.

Similar presentations


Presentation on theme: "CS 202 Computer Science II Lab Fall 2009 September 10."— Presentation transcript:

1 CS 202 Computer Science II Lab Fall 2009 September 10

2 Today Topics Reusing programs (making header files) Script mounting your USB Copy-Paste in vi

3 Reusing Programs Why reuse? Copy-paste the code – Advantages: NONE – Disadvantage: a lot!! Using header files Object-oriented programming

4 Header file example… Create headerExample directory – *do not delete files Create basicMath.h file : 1./* basicMath.h */ 2. 3.int add (int a, int b); 4.int mul (int a, int b);

5 .Header file example.. Create basicMath.cpp file : 1./* basicMath.cpp */ 2.int add (int a, int b) { 3. return a+b; 4.} 5.int mul (int a, int b) { 6. return a*b; 7.}

6 ..Header file example. Create main.cpp file : 1.#include 2.#include "basicMath.h" 3.using namespace std; 4.int main(int argc, char ** argv) { 5.int a, b; 6.cout << " Name: Your-Name " << endl; 7.cout << " Section: Your-Section-No " << endl; 8.if (argc > 2) { 9.a = atoi(argv[1]); 10.b = atoi(argv[2]); 11.} else { 12.a = 1; 13. b = 2; 14.} 15.cout << a << " + "<< b << " = " << add(a, b) << endl; 16.cout << a << " * "<< b << " = " << mul(a, b) << endl; 17.return 0; 18.}

7 …Header file example – g++ -c main.cpp – g++ -c basicMath.cpp – Or: – g++ -c *.cpp – g++ -o main main.o basicMath.o –./main 12 5 > output.out

8 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 – lpr output.out

9 Script Script [file name] … exit Default file name: typescript

10 mounting your USB df -Tto see list of mounted devices mount /dev/fd0 /mnt/floppy mount /dev/cdrom /mnt/cdrom mount /dev/sda1 /mnt/usbdisk

11 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

12 Questions?


Download ppt "CS 202 Computer Science II Lab Fall 2009 September 10."

Similar presentations


Ads by Google