lecture17

lecture17 - Monday De m r 6 ce be Fill out class e...

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

View Full Document Right Arrow Icon
Monday, December 6 th Fill out class evaluation forms Ice cream (mmmmmm, Ice Cream) Finish Operator Overloading Start Review: ASCII Pointers Classes Constructors and Destructors The final exam is this FILL IN THE BLANK. Review session on FILL IN THE BLANK!
Background image of page 1

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

View Full Document Right Arrow Icon
Characters and ASCII Every symbol on the keyboard: letter , digit period space , etc. is represented by a numeric value in memory ! ‘A’ = 65, ‘B’ = 66, ..., ‘Z’ = 90 ‘a’ = 97, ‘b’ = 98, . .., ‘z’ = 122 ‘ ‘ = 32 (space), ‘!’ = 33, ‘,’ = 44, etc. .. ‘0’ = 48, ‘1’ = 49, ‘2’ = 50,. .. ‘9’ = 57
Background image of page 2
main() { char c1 = ‘C’ ; // note: c NOT C char c2 = 67 ; cout << c1 << endl; cout << c2 << endl; } Characters and ASCII c1 67 When using char variables in C++, you may use symbolic characters or ASCII (numeric) values. c2 C C ‘A’ = 65 ‘B’ = 66 ‘C’ = 67 ... ‘Z’ = 90 When you use cout to print a char variable , converts the number back to a symbol for display on the screen.
Background image of page 3

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

View Full Document Right Arrow Icon
Characters and ASCII void main(void) { char ch; cin >> ch; cout << ch << endl; cout<< static_cast<int>( ch ) ++ch; // treat it just like a number! cout << ch << endl; } Enter a char: d ch 100 d You can print out the ASCII value of a char variable instead of printing its symbol using a static_cast . 101 e
Background image of page 4
What’s the Difference? void main(void) { int i cout >> “Enter an int: “; cin >> i; cout << i << endl; char c; cout >> “Enter a char: “; cin >> c; cout << static_cast<int>(c) << endl; cout << c; } If you use cin with an int variable , C++ converts the symbol you type into its integer equivalent. i c Enter an int: 9 9 9 Enter a char: 9 57 If you use cin with a char variable , it converts the symbol you type into its ASCII value, not its integer value. 9
Background image of page 5

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

View Full Document Right Arrow Icon
num2 Characters and ASCII void main(void) { char c; cout<<"Enter uppercase letter: "; cin >> c; int num = c - 65; cout << c << " is letter # " << num; cout << endl; int num2 = c - 'A'; cout << c << " is letter # " << num2; cout << endl; if ('A' == 65) cout << "A is 65!\n"; if ('0' == 48) cout << "0 is 48!\n"; } c num Enter uppercase letter: F 70 5 F is letter 5 num2 = c - 65; F is letter 5 5 A is 65 0 is 48
Background image of page 6
main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs p1 = &a; p2 = &b; // whats the diff between if (*p1 == *p2) { do something; }// and if (p1 == p2) { do something; } } 00000000 00000001 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 a 1 b 2 p1 p2 6420 6424 *p1 == *p2 and p1 == p2
Background image of page 7

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

View Full Document Right Arrow Icon
*p1 == *p2 and p1 == p2 main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs p1 = &a; p2 = &b; // whats the difference between if (*p1 == *p2) { do something; } // and if (p1 == p2) { do something; } } 00000000 00000001 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 a 1 b 2 p1 p2 6420 6424 “Compare the value pointed to by p1 to the value pointed to by p2.” if ( 1 == 2 )
Background image of page 8
main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs p1 = &a; p2 = &b; // whats the diff between if (*p1 == *p2) { do something; } // and if (p1 == p2) { do something; } } 00000000 00000001 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 a 1 b 2 p1 p2 6420 6424 “Compare the value of variable p1 to the value of variable p2.” if ( 6420 == ) *p1 == *p2 and p1 == p2
Background image of page 9

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

View Full Document Right Arrow Icon
void set(int *pa) // like a reference! { *pa = 5; } main() { int x = 1; set(&x); cout << x; } Using Pointers Instead of References 00000000 00000001 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 x 1 pa 9240 Store a value of 5 in the variable where pa points (at location 9240).
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 40

lecture17 - Monday De m r 6 ce be Fill out class e...

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

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