Docsity

CSE333 Autumn 2018: Lecture Notes on References, Const, and Classes in C++, Slides of Object Oriented Programming

Partial preview of the text

Download CSE333 Autumn 2018: Lecture Notes on References, Const, and Classes in C++ and more Slides Object Oriented Programming in PDF only on Docsity!

C++ References, Const, Classes

CSE 333 Autumn 2018

Instructor: Hal Perkins

Teaching Assistants:

Tarkan Al-Kazily Renshu Gu Travis McGaha

Harshita Neti Thai Pham Forrest Timour

Soumya Vasisht Yifan Xu

Administrivia v Yet another exercise released today, due Friday v Sections this week: C++ classes, references + Makefiles! § Don’t miss!! – you’ll need to create Makefiles soon, and this is the only

time we’ll talk about them in class

v More office hours added on Wed. and Thur. afternoons v Homework 2 due next Thursday (7/19) § Note: libhw1.a (yours or ours) needs to be in correct directory

(hw1/)

§ Use Ctrl-D to exit searchshell; must free all allocated memory § Test on directory of small self-made files § Valgrind takes a long time on the full test_tree. Try using enron docs

only or other small test data directory

Pointers Reminder v A pointer is a variable containing an address § Modifying the pointer doesn’t modify what it points to, but you can access/modify what it points to by dereferencing § These work the same in C and C++ int main (int argc, char** argv) { int x = 5 , y = 10 ; int* z = &x; *z += 1 ; x += 1 ; z = &y; *z += 1 ; return EXIT_SUCCESS; }

pointer.cc

x 5

y 10

z

Note: Arrow points

to next instruction.

Pointers Reminder v A pointer is a variable containing an address § Modifying the pointer doesn’t modify what it points to, but you can access/modify what it points to by dereferencing § These work the same in C and C++ int main (int argc, char** argv) { int x = 5 , y = 10 ; int* z = &x; *z += 1 ; x += 1 ; z = &y; *z += 1 ; return EXIT_SUCCESS; }

pointer.cc

x 5

y 10

z 0x7fff…a

Note: Arrow points

to next instruction.

Pointers Reminder v A pointer is a variable containing an address § Modifying the pointer doesn’t modify what it points to, but you can access/modify what it points to by dereferencing § These work the same in C and C++ int main (int argc, char** argv) { int x = 5 , y = 10 ; int* z = &x; *z += 1 ; // sets x to 6 x += 1 ; // sets x (and *z) to 7 z = &y; *z += 1 ; return EXIT_SUCCESS; }

pointer.cc

x 7

y 10

z 0x7fff…a

Note: Arrow points

to next instruction.

Pointers Reminder v A pointer is a variable containing an address § Modifying the pointer doesn’t modify what it points to, but you can access/modify what it points to by dereferencing § These work the same in C and C++ int main (int argc, char** argv) { int x = 5 , y = 10 ; int* z = &x; *z += 1 ; // sets x to 6 x += 1 ; // sets x (and *z) to 7 z = &y; // sets z to the address of y *z += 1 ; return EXIT_SUCCESS; }

pointer.cc

x 7

y 10

z 0x7fff…a^0

Note: Arrow points

to next instruction.

References v A reference is an alias for another variable § Alias : another name that is bound to the aliased variable

  • Mutating a reference is mutating the aliased variable § Introduced in C++ as part of the language int main (int argc, char** argv) { int x = 5 , y = 10 ; int & z = x; z += 1 ; x += 1 ; z = y; z += 1 ; return EXIT_SUCCESS; }

reference.cc

x 5

y 10

Note: Arrow points

to next instruction.

References v A reference is an alias for another variable § Alias : another name that is bound to the aliased variable

  • Mutating a reference is mutating the aliased variable § Introduced in C++ as part of the language int main (int argc, char** argv) { int x = 5 , y = 10 ; int& z = x; // binds the name "z" to x z += 1 ; x += 1 ; z = y; z += 1 ; return EXIT_SUCCESS; }

reference.cc

x , z 5

y 10

Note: Arrow points

to next instruction.

References v A reference is an alias for another variable § Alias : another name that is bound to the aliased variable

  • Mutating a reference is mutating the aliased variable § Introduced in C++ as part of the language int main (int argc, char** argv) { int x = 5 , y = 10 ; int& z = x; // binds the name "z" to x z += 1 ; // sets z (and x) to 6 x += 1 ; // sets x (and z) to 7 z = y; z += 1 ; return EXIT_SUCCESS; }

reference.cc

x , z 7

y 10

Note: Arrow points

to next instruction.

References v A reference is an alias for another variable § Alias : another name that is bound to the aliased variable

  • Mutating a reference is mutating the aliased variable § Introduced in C++ as part of the language int main (int argc, char** argv) { int x = 5 , y = 10 ; int& z = x; // binds the name "z" to x z += 1 ; // sets z (and x) to 6 x += 1 ; // sets x (and z) to 7 z = y; // sets z (and x) to the value of y z += 1 ; return EXIT_SUCCESS; }

reference.cc

x , z 10

y 10

Note: Arrow points

to next instruction.

Pass-By-Reference v C++ allows you to truly pass-by- reference § Client passes in an argument with normal syntax

  • Function uses reference parameters with normal syntax
  • Modifying a reference parameter modifies the caller’s argument! void swap (int & x, int & y) { int tmp = x; x = y; y = tmp; } int main (int argc, char** argv) { int a = 5 , b = 10 ; swap (a, b); cout << "a: " << a << "; b: " << b << endl; return EXIT_SUCCESS; }

passbyreference.cc

(main) a 5

(main) b 10

Note: Arrow points

to next instruction.

Pass-By-Reference v C++ allows you to truly pass-by- reference § Client passes in an argument with normal syntax

  • Function uses reference parameters with normal syntax
  • Modifying a reference parameter modifies the caller’s argument! void swap (int& x, int& y) { int tmp = x; x = y; y = tmp; } int main (int argc, char** argv) { int a = 5 , b = 10 ; swap (a, b); cout << "a: " << a << "; b: " << b << endl; return EXIT_SUCCESS; }

passbyreference.cc

(main) a

(swap) x

(main) b

(swap) y

Note: Arrow points

to next instruction.

(swap) tmp

Pass-By-Reference v C++ allows you to truly pass-by- reference § Client passes in an argument with normal syntax

  • Function uses reference parameters with normal syntax
  • Modifying a reference parameter modifies the caller’s argument! void swap (int& x, int& y) { int tmp = x; x = y; y = tmp; } int main (int argc, char** argv) { int a = 5 , b = 10 ; swap (a, b); cout << "a: " << a << "; b: " << b << endl; return EXIT_SUCCESS; }

passbyreference.cc

(main) a

(swap) x

(main) b

(swap) y

Note: Arrow points

to next instruction.

(swap) tmp 5

Pass-By-Reference v C++ allows you to truly pass-by- reference § Client passes in an argument with normal syntax

  • Function uses reference parameters with normal syntax
  • Modifying a reference parameter modifies the caller’s argument! void swap (int& x, int& y) { int tmp = x; x = y; y = tmp; } int main (int argc, char** argv) { int a = 5 , b = 10 ; swap (a, b); cout << "a: " << a << "; b: " << b << endl; return EXIT_SUCCESS; }

passbyreference.cc

(main) a

(swap) x

(main) b

(swap) y

Note: Arrow points

to next instruction.

(swap) tmp 5