cs 202 computer science ii lab
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
CS 202Computer Science II Lab
Fall 2009September 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 :
1. /* basicMath.h */2. 3. int add (int a, int b);
4. int mul (int a, int b);
.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. }
..Header file example.• Create main.cpp file :1. #include <iostream>
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. }
…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
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
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
Questions?