1 / 12

CS 202 Computer Science II Lab

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

Download Presentation

CS 202 Computer Science II Lab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 202Computer 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 : • /* basicMath.h*/ • int add (int a, int b); • intmul (int a, int b);

  5. .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; • }

  6. ..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; • }

  7. …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

  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 • lproutput.out

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

  10. 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

  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?

More Related