lecture7slides

lecture7slides - Topics for Monday, October 22nd 1. Project...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Topics for Monday, October 22 nd 1. Project #3 questions 2. Function trivia challenge 3. Variable promotion 4. Using header files for prototypes 5. Inline functions 6. Enum 7. Arrays, arrays, arrays Briefly: When writing programs, try to solve them a little bit at a time. Once you get one component working, you can worry less about its bugs. After creating lots of little components, put them together and write the “glue” code.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Quick demos What’s wrong with this function? void I_hate_proj_3_this_much() { int a; cin >> a; return(a); } What does the following prog do? int snot(void) { int ahh; for (ahh=0;ahh<5;ahh++) { if (2 == ahh) return(ahh); cout << “ahh”; } cout << “choo!”; } main() { cout << snot(); }
Background image of page 2
What’s wrong with this function? void mucus(int gallons) { int gallons; cin >> gallons; cout << “gurgle “ << gallons; } What’s wrong with this program? float icecream(double dip); float icecream(int dip) { cout << “you get “ << dip << “scoops”; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Variable promotion in C/C++ What happens in the following program? void messy(double room) { cout << room; } void main() { int dorm = 5; messy(dorm); } How about this one? void dirty(char mind) { cout << mind; } void main() { int hair = 5; dirty(hair); }
Background image of page 4
Promotion Continued You may pass a lower-precision variable to a higher-precision parameter without troubles: For example, the compiler will allow without complaint: float to double short to float int to long char to long char to short etc… Be careful when you pass a higher-precision variable to a lower-precision parameter. This can result in bugs (warnings): double to float double to int long to char int to char Be careful when you pass a signed variable to an unsigned parameter too (and visa versa)! unsigned int to int
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
More promotion examples What happens with: void foo(int a) { cout << a; } void main(void) { float a = 3.14159; foo(a); } How about this: void foo(char a) { cout << a // ?? } void main(void) { int a = 255; foo(a); }
Background image of page 6
Writing multi-file programs: Place prototypes in one header file for each cpp file. Include a header file in your C++ file if you intend to call its functions. Add all cpp and h files to your
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

lecture7slides - Topics for Monday, October 22nd 1. Project...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online