73_pdfsam_cs2022_lectures - printf( "%d %d\n”, A, B...

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

View Full Document Right Arrow Icon
Const, register Constant variables Register: variable in register (faster access) Just a recommendation to compiler though int main() { const float PI=3.142; ... } int main() { register int i; ... for(i=0; i<10;i++) ... }
Background image of page 1

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

View Full DocumentRight Arrow Icon
C++: Non object-oriented features C++ Standard in 1997 Call by reference const call by reference Type overloading Behavior of function depends on inputs Default parameter values
Background image of page 2
Recall: swap This swap() function works void swap ( int * a, int * b ) { int temp = * a; * a = * b; * b = temp; } void main() { int A = 1, B = 2; swap( A, B); printf( "%d %d\n”, A, B ); // prints “2 1” }
Background image of page 3

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

View Full DocumentRight Arrow Icon
Call by reference in C++ This swap() function works: no addresses void swap ( int a, int b ) { int temp = a; a = b; b = temp; } void main() { int A = 1, B = 2; swap(A, B);
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: printf( "%d %d\n”, A, B ); // prints “2 1” } Using const . .. & Calling by value: Creates copy of data A large data type you don't want to copy int compare ( int a, int b ){ if (a==b) return 0; return (a>b)? 1 : -1; } struct Student { int id, year, phone_number, birthdate . ..; char name[30], mothers_name[30], fathers_name[30] . ..; char CS2022_grade, CS1110_grade, . ..; }; Const prevents copying data Can avoid copying data if variable unchanged Compiler will complain int compare (const int &a, const int &b ){ a++ ; if (a==b) return 0; return (a>b)? 1 : -1; } int compare ( const int &a, const int &b ){ if (a==b) return 0; return (a>b)? 1 : -1; }...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

73_pdfsam_cs2022_lectures - printf( "%d %d\n”, A, B...

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

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