Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Lecture notes for CSE333 Autumn 2018, covering the topics of references, const, and classes in C++. explanations, examples, and exercises. Students are encouraged to read the sections in C++ Primer on class constructors, copy constructors, assignment operators, and destructors before the next class.

What you will learn

  • How does const work in C++?
  • What is the difference between a pointer and a reference in C++?
  • How do you define and use classes in C++?
  • What is a reference in C++?
  • What are the benefits of using references instead of pointers in C++?

Typology: Slides

2021/2022

Uploaded on 09/12/2022

shashwat_pr43
shashwat_pr43 🇺🇸

4.5

(15)

233 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE333, Autumn 2018L11: References, Const, Classes
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

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