1 of 74

CSE 374 Programming concepts and tools

Summer 2024 Instructor: Audrey Seo

2 of 74

Announcements

Grades posted on Canvas:

  • HW through HW3
  • Exercises through Ex12
  • Week 1-6 participation grades

HW6 is out -- implementing T9

  • We’ll take a look at the homework at the end of class

This is week 7! Just 3 more weeks :)

  • We will be meeting the week of finals by the way, at the regular time
    • If you have a conflict, please let us know

3 of 74

Last week

This week/Today

Monday: Testing

Wednesday: Variable Types and Storage

Friday: Memory Architecture

Today: Intro to C++

  • Differences from C
  • Intro
  • Hello World in C++
  • Pointers vs. References (Revenge of the &)
  • HW6: T9 Implementation Overview

Wednesday: C++ Classes

Friday: C++ Class Details

4 of 74

C++ Intro

5 of 74

Why C++?

  • Originally created as an extension to C to add object-oriented programming (OOP)
  • OOP can be a powerful tool for abstraction
    • And C doesn’t have OOP
    • Abstractions make it easier to organize code
  • Still pretty old: first released in 1985
  • Provides more programming assistance
    • C’s standard library is very lightweight
    • C++’s standard template library (STL) provides generic implementations of:
      • popular data structures
      • algorithms
      • iterators
      • etc.
  • Like C: C++ doesn’t do a lot of hand-holding

6 of 74

How to Think About C++

6

Set of styles and ways to use C++

Set of styles and ways to use C

Good styles and robust engineering practices

7 of 74

Or…

7

In the hands of a disciplined programmer, C++ is a powerful tool

But if you’re not so disciplined about how you use C++…

8 of 74

Hello World in C

  • Compile with gcc:

  • You should be able to describe in detail everything in this code

8

#include <stdio.h> // for printf()

#include <stdlib.h> // for EXIT_SUCCESS

int main(int argc, char** argv) {

printf("Hello, World!\n");

return EXIT_SUCCESS;

}

helloworld.c

gcc -Wall -g -std=c11 -o hello helloworld.c

9 of 74

Hello World in C++

Looks simple enough…

  • Compile with g++ instead of gcc:

*.cpp is also a common file extension for C++ files, but we’ll use .cc in this class

9

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

g++ -Wall -g -std=c++17 -o helloworld helloworld.cc

helloworld.cc

10 of 74

Hello World in C vs. C++

10

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

#include <stdio.h> // for printf()

#include <stdlib.h> // for EXIT_SUCCESS

int main(int argc, char** argv) {

printf("Hello, World!\n");

return EXIT_SUCCESS;

}

helloworld.c

11 of 74

Example: Hello World in C++

12 of 74

Hello World in C++

iostream is part of the C++ standard library

  • Note: you don’t write “.h” when you include C++ standard library headers
    • But you do for local headers (e.g. #include "ll.h")
  • iostream declares stream object instances in the “std” namespace
    • e.g. std::cin, std::cout, std::cerr

12

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

13 of 74

Hello World in C++

cstdlib is the C standard library’s stdlib.h

  • Nearly all C standard library functions are available to you
    • For C header stdlib.h, you should #include <cstdlib>
  • We include it here for EXIT_SUCCESS, as usual

13

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

14 of 74

Hello World in C++

std::cout is the “cout” object instance declared by iostream, living within the “stdnamespace

  • C++’s name for stdout, std::cout is an object of class ostream
    • output stream
  • Used to format and write output to the console
  • The entire standard library is in the namespace std

14

helloworld.cc

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

15 of 74

Hello World in C++

C++ has a stronger distinction between objects and primitive types

  • These include the familiar ones from C:�char, short, int, long, float, double, etc.
  • C++ also defines bool as a primitive type 🎉🎉🎉
    • Use it!

15

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

16 of 74

Hello World in C++

<<” is an operator defined by the C++ language

  • Defined in C as well: usually it bit-shifts integers (in C/C++)
  • C++ allows classes and functions to overload operators!
    • Here, the ostream class overloads “<<
    • i.e. it defines different member functions (methods) that are invoked when an ostream is the left-hand side of the << operator

16

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

17 of 74

Hello World in C++

ostream has many different methods to handle <<

  • The functions differ in the type of the right-hand side (RHS) of <<
  • e.g. if you do std::cout << "foo"; , then C++ invokes cout’s function to handle << with RHS char*

17

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

ostream object

still a char*

18 of 74

Hello World in C++

The ostream class’ member functions that handle << return a reference to themselves

  • When std::cout << "Hello, World!"; is evaluated:
    • A member function of the std::cout object is invoked
    • It buffers the string "Hello, World!" for the console
    • And it returns a reference to std::cout

18

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

This is equivalent to:

std::cout << “Hello,world!”;

std:cout << std::endl;

19 of 74

Hello World in C++

Next, another member function on std::cout is invoked to handle << with RHS std::endl

  • std::endl is a pointer to a “manipulator” function
    • This manipulator function writes newline ('\n') to the ostream it is invoked on and then flushes the ostream’s buffer
    • This enforces that something is printed to the console at this point
  • If you need to print a '\n', you should probably use std::endl

19

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

20 of 74

Wow…

You should be surprised and scared at this point

  • C++ makes it easy to hide a significant amount of complexity
    • It’s powerful, but really dangerous
    • Once you mix everything together (templates, operator overloading, method overloading, multiple inheritance), it can get really hard to know what’s actually happening!

20

#include <iostream>

#include <cstdlib>

int main(int argc, char** argv) {

std::cout << "Hello, World!" << std::endl;

return EXIT_SUCCESS;

}

helloworld.cc

21 of 74

Questions?

22 of 74

Let’s Refine It a Bit

C++’s standard library has a std::string class

  • Include the string header to use it

22

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello, World!");

cout << hello << endl;

return EXIT_SUCCESS;

}

helloworld2.cc

23 of 74

Let’s Refine It a Bit

The using keyword introduces a namespace (or part of) into the current region

  • using namespace std; imports all names from std::
    • Linter will complain, but we will ignore for this class
  • using std::cout; imports only std::cout (used as cout)

23

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello, World!");

cout << hello << endl;

return EXIT_SUCCESS;

}

helloworld2.cc

24 of 74

Let’s Refine It a Bit

Benefits of

  • We can now refer to std::string as string, std::cout as cout, and std::endl as endl

24

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello, World!");

cout << hello << endl;

return EXIT_SUCCESS;

}

helloworld2.cc

using namespace std;

25 of 74

Let’s Refine It a Bit

Here we are instantiating a std::string object on the stack (an ordinary local variable)

  • Passing the C string "Hello, World!" to its constructor method
  • hello is deallocated (and its destructor invoked) when main returns

25

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello, World!");

cout << hello << endl;

return EXIT_SUCCESS;

}

helloworld2.cc

26 of 74

Let’s Refine It a Bit

The C++ string library also overloads the << operator

  • Defines a function (not an object method) that is invoked when the LHS is ostream and the RHS is std::string

26

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello, World!");

cout << hello << endl;

return EXIT_SUCCESS;

}

helloworld2.cc

27 of 74

String Concatenation

The string class overloads the “+” operator

  • Creates and returns a new string that is the concatenation of the LHS and RHS

27

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello");

hello = hello + ", World!";

cout << hello << endl;

return EXIT_SUCCESS;

}

concat.cc

28 of 74

String Assignment

The string class overloads the “=” operator

  • Copies the RHS and replaces the string’s contents with it

28

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

#include <string> // for string

using namespace std;

int main(int argc, char** argv) {

string hello("Hello");

hello = hello + ", World!";

cout << hello << endl;

return EXIT_SUCCESS;

}

concat.cc

29 of 74

String Manipulation

This statement is complex!

  • First “+” creates a string that is the concatenation of hello’s current contents and ", World!"
  • Then “=” creates a copy of the concatenation to store in hello
  • Without the syntactic sugar:
    • hello.operator=(hello.operator+(", World!"));

29

int main(int argc, char** argv) {

string hello("Hello");

hello = hello + ", World!";

cout << hello << endl;

return EXIT_SUCCESS;

}

concat.cc

hello.operator=(hello.operator+(", World!"));

Operators are just member functions

30 of 74

C and C++

C is (roughly) a subset of C++

  • You can still use printf – but bad style in ordinary C++ code
    • E.g Use std::cerr instead of fprintf(stderr, …)
  • Can mix C and C++ idioms if needed to work with existing code, but avoid mixing if you can
    • Use C++(17)

30

#include <cstdio> // for printf

#include <cstdlib> // for EXIT_SUCCESS

int main(int argc, char** argv) {

printf("Hello from C!\n");

return EXIT_SUCCESS;

}

helloworld3.cc

31 of 74

Reading

std::cin is an object instance of class istream

  • Supports the >> operator for “extraction”
    • Can be used in conditionals ! (std::cin>>num) is true if successful
  • Has a getline() method and methods to detect and clear errors

31

#include <iostream> // for cout, endl

#include <cstdlib> // for EXIT_SUCCESS

using namespace std;

int main(int argc, char** argv) {

int num;

cout << "Type a number: ";

cin >> num;

cout << "You typed: " << num << endl;

return EXIT_SUCCESS;

}

echonum.cc

32 of 74

Demo: echonum.cc

33 of 74

Questions?

34 of 74

C++ References

35 of 74

Review: Pointer

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++

35

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.

36 of 74

Review: Pointer

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++

36

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…a4

37 of 74

Review: Pointer

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++

37

int main(int argc, char** argv) {

int x = 5, y = 10;

int* z = &x;

*z += 1; // sets x to 6

x += 1;

z = &y;

*z += 1;

return EXIT_SUCCESS;

}

pointer.cc

x

6

y

10

z

0x7fff…a4

38 of 74

Review: Pointer

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++

38

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…a4

39 of 74

Review: Pointer

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++

39

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…a0

40 of 74

Review: Pointer

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++

40

int main(int argc, char** argv) {

int x = 5, y = 10;

int* z = &x;

*z += 1; // sets x to 5 + 1 = 6

x += 1; // sets x (and *z) to 6 + 1 = 7

z = &y;

*z += 1; // sets y to 10 + 1 = 11

return EXIT_SUCCESS;

}

pointer.cc

x

7

y

11

z

0x7fff…a0

41 of 74

References

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

41

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

When we use '&' in a type declaration, it is a reference.

&var is still “address of var”

42 of 74

References

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

42

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

43 of 74

References

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

43

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;

z = y;

z += 1;

return EXIT_SUCCESS;

}

reference.cc

x, z

6

y

10

44 of 74

References

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

44

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

45 of 74

References

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

45

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; Normal assignment

z += 1;

return EXIT_SUCCESS;

}

reference.cc

x, z

7

y

10

There is no way to rebind a reference to refer to a different object. Because there is no way to rebind a reference, references must be initialized.

46 of 74

References

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

46

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

47 of 74

References

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

47

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; // sets z (and x) to 11

return EXIT_SUCCESS;

}

reference.cc

x, z

11

y

10

48 of 74

Some Symbols Have Multiple Meanings

& and * are used as both an operator in an expression and as part of a declaration. The context in which a symbol is used determines what the symbol means:

int i = 42;

int& r = i; // & follows a type and is part of a declaration; r is a reference

int* p; // * follows a type and is part of a declaration; p is a pointer

p = &i; // & is used in an expression as the address-of operator

*p = i; // * is used in an expression as the dereference operator

int& r2 = *p; // & is part of the declaration; * is the dereference operator

In declarations, & and * are used to form compound types. In expressions, these same

symbols are used to denote an operator.

49 of 74

Questions?

50 of 74

Pass-By-Reference

C++ allows you to use real 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!

50

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

51 of 74

Pass-By-Reference

C++ allows you to use real 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!

51

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

Parameters are attached to variables provided by caller

52 of 74

Pass-By-Reference

C++ allows you to use real 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!

52

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

5

(main) b

(swap) y

10

(swap) tmp

53 of 74

Pass-By-Reference

C++ allows you to use real 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!

53

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

5

(main) b

(swap) y

10

(swap) tmp

5

54 of 74

Pass-By-Reference

C++ allows you to use real 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!

54

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

10

(main) b

(swap) y

10

(swap) tmp

5

55 of 74

Pass-By-Reference

C++ allows you to use real 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!

55

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

10

(main) b

(swap) y

5

(swap) tmp

5

56 of 74

Pass-By-Reference

C++ allows you to use real 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!

56

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

10

(main) b

5

57 of 74

Best Practices

Programmers accustomed to programming in C often use pointer parameters to access objects outside a function. In C++, programmers generally use reference parameters instead.

58 of 74

Questions?

59 of 74

What will happen when we run this?

  1. Outputs "(1,2,3)"
  2. Outputs "(3,2,3)"
  3. Compiler error about arguments to foo (in main)
  4. Compiler error about body of foo

59

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

60 of 74

61 of 74

62 of 74

Poll Question Explained

62

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

a, c

1

b

2

63 of 74

Poll Question Explained

63

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

(main) a, c

(foo) x

1

b

2

y

z

1

b

2

64 of 74

Poll Question Explained

64

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

(main) a, c

(foo) x

1

b

2

y

z

2

b

2

65 of 74

Poll Question Explained

65

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

(main) a, c

(foo) x

3

b

2

y

z

2

b

2

66 of 74

Poll Question Explained

66

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

(main) a, c

(foo) x

3

b

2

y

z

2

b

2

67 of 74

Poll Question Explained

67

void foo(int& x, int* y, int z) {

z = *y;

x += 2;

y = &x;

}

int main(int argc, char** argv) {

int a = 1;

int b = 2;

int& c = a;

foo(a, &b, c);

std::cout << "(" << a << ", " << b

<< ", " << c << ")" << std::endl;

return EXIT_SUCCESS;

}

a, c

3

b

2

b

2

68 of 74

Aside: C++ Primer

It’s hard to learn the “why is it done this way” from reference docs, and even harder to learn from random stuff on the web

69 of 74

HW6: T9 Implementation

70 of 74

Last week on CSE 374…

You were writing tests for T9!

T9 uses a data structure called tries

  • Compactly stores sets of strings
  • Pretty fast string lookup
  • Great for autocompletion

Now, you’ll be implementing T9 to create a whole autocomplete command line application

71 of 74

What you’ll need to do for HW6*

What you’ll do:

  • Implement the trie data structure
  • Write your own implementation of the T9 library functions in t9_lib.c
    • You can add your own helpers too!
    • But you will need to implement all of the functions
  • You can and should use your HW5 tests to help test your application
    • Feel free to add more tests, especially as you fix bugs (regression tests!)
  • Create your own Makefile (feel free to riff off of the HW5 Makefile)

Requirements:

  • Edit the files t9_priv.h, t9_lib.c, and t9_tests.c
  • Use good code style (code quality is a part of your grade)
  • Must be memory error free (use valgrind!)
  • Library functions should return NULL when an error occurs when opening/reading a file

Total Points: 65

  • Autograded: 58
  • Manually Graded: 7

*This is just the gist, more information on the website (which is the ground source of truth for homework specs)!

72 of 74

The T9 Application

73 of 74

74 of 74

Ex15 due Wednesday, HW6 due Sunday!

Ex15 is due before the beginning of the next lecture

HW6 due Sunday 11.59pm!