Download presentation
Presentation is loading. Please wait.
1
CS3101-2 Programming Languages – C++ Lecture 1
Matthew P. Johnson Columbia University Fall 2003
2
Agenda hw0 due last night Arrays, mult-D arrays
Char arrays, String objects Pointers & References hw1 assigned tonight 1/18/2019 CS3101-2, Lecture 2
3
Array review Arrays are complex (=non-primitive) data structures
Ordered, fixed-length seq of vars All elms same type Access by index: ar[4] Indices begin at 0 1/18/2019 CS3101-2, Lecture 2
4
Arrays Review: Example: Composite: multiple members
Homogeneous: mems of same type Contiguous: adjacent in memory Etym: root is contig-, ~ “contact” Example: int nums[3]; nums[0] = nums[1] = nums[2]; nums[3] ? 1/18/2019 CS3101-2, Lecture 2
5
C-style strings C also has For example: NB: ‘\0’ != ‘0’
neither a built-in string type Nor a string class Just char[]s For example: char s[3]; s[0] = ‘H’; s[1] = ‘i’; s[2] = ‘\0’; NB: ‘\0’ != ‘0’ Shortcut syntax: char s[] = “Hi”; //size inferred For non-dynamic C-style strings members (chars) are mutable char[]s (arrays) are immutable 1/18/2019 CS3101-2, Lecture 2
6
C-style strings So can’t: But can: Unsafe, though:
char s1[100] = “hi”; char s2 = “ there”; s1 = s2; s1 += s2; s3 = s1 + s2; But can: strcpy(s1, s2); s1 == “ there” strcat(s1, s2); s1 == “hi there” strcat overwrites ‘\0’ of s1 strcpy(s3, s1); s3 == “hi there” Unsafe, though: No space checking Also, can get length: strlen(s3) == 9 And compare: strcmp(s1, s2) < 0 s1 comes before s2 > 0 s1 comes after s2 == 0 s1 == s2 These functions live in <cstring> NB: <lib.h> in C <clib> in C++ 1/18/2019 CS3101-2, Lecture 2
7
C-style strings – strlen
Nothing built-in to a string/array telling its length, like s.length() in Java How to find out? For arrays in general: remember/be told For strings: look and see! i.e., look for ‘\0’ for (int i = 0; s[i] != ‘\0’; i++) ; return i; NB for C-style strings: Size is fixed Length (==distance from start to ‘\0’) varies 1/18/2019 CS3101-2, Lecture 2
8
Strings – strlen char s[] = “hello”; s[3] = ‘\0’; strlen(s) == … 2 h e
1/18/2019 CS3101-2, Lecture 2
9
Mistakes with strings char s[2];
s = “hi”; // error: arrs are immutable char s[2] = “hi”; //error: sizeof “hi” == 3 char s[3] = “hi”; //okay char t[6] = “there”; s = s + t; // error: trying to add arrays strcat(s, t); // error: writes too far strcat(s, t, sizeof(s)); // s == {‘t’,’h’,’e’} s[2] = ‘\0’; // okay strcat(s, t, sizeof(s)-1) // okay if s[2]==‘\0’ 1/18/2019 CS3101-2, Lecture 2
10
C-style strings strcat copies t over null char of s
strlen(strcat(s,t)) == strlen(s) + strlen(t) #bytes(strcat(s,t)) == strlen(s) + strlen(t) + 1 == #bytes(s) + #bytes(t) -1 Not automatically initialized (like arrays in gen.) char s[100]; s[10] == ? But extra space in static init set to ‘\0’: char s[100] = “hi”; {‘h’, ‘i’, 0, 0, …} char s[100] = “”; {0, 0, 0, …} char s[100] = {}; {0, 0, 0, …} 1/18/2019 CS3101-2, Lecture 2
11
String class Like C, Java, C++ has no built-in String type
I.e., String is not part of the C++ language Like C, char[]s can be used as (“C-style”) strings Like Java, the std libraries have a string class defined in <string> Lives in std namespace 1/18/2019 CS3101-2, Lecture 2
12
String class e.g. #include <string> #include <iostream>
using namespace std; int main() { //String instances declared like other vars: string school; //Can be set to string literals: school = “Columbia”; //Can be concatenated with + op: school = school + “ “ + “University”; //school += “ “ + “University”; cout << “We\’re at: “ << school << endl; } $ g++ school.cpp $ a.out We're at: Columbia University 1/18/2019 CS3101-2, Lecture 2
13
String operators NB: + is overloaded to apply to string objects
+= is overloaded too: school += “ “; school += “University”; same effect What if school += “ “ + “University”? Idea 1: Precedence problem? A: No. Assign ops have very low prec. Idea 2 str-lit + str-lit problem? A: Yes! + is overloaded for string objects not for string literals/char[]s/char*s g++ says: school.cpp:16: invalid operands `const char[2]' and `const char[11]' to binary `operator +' 1/18/2019 CS3101-2, Lecture 2
14
String objects String objects != char arrays
char[]s often cannot be treated like string objs But: string class designed so can often treat like char[]s char[] chars accessed quickly/unsafely with []s: char ca[] = “hi”; ca[0] = ‘H’; ca[-1] = … trouble string chars can be accessed the same way: string school = “Columbia”; school[0] = ‘N’l school[-1] = ‘Y’; still unsafe! the [] operator is overloaded! 1/18/2019 CS3101-2, Lecture 2
15
string class should be sure/check that the index is valid
assert(i >= 0 && i < 8); Lives in cassert If expr passed is false, program quits, with message: ca.cpp:13: failed assertion `-1 >= 0 && -1 < 8‘ Abort (core dumped) string chars can also be read by at member ftn: school.at(0); school.at(-1); throws an exception does automatic bounds checking informs caller if index is not valid if main ftn ignores, we Abort 1/18/2019 CS3101-2, Lecture 2
16
string.length() Remember: arrays do not “know” their length
where they’re defined, length is part of type (int[10]) passed as member-type ptrs (length is lost) array itself is just sequence of members char[]s do not know they’re type convention: last char is ‘\0’ string objects do know they’re length school.length() == 8 can return part of self: string.substr(start, length) school.substr(2,3) “lum” second param is not last char (or next-after-last char, as in J) it’s the desired length of substring if omitted, gives rest 1/18/2019 CS3101-2, Lecture 2
17
Comparing strings string class has a compare function similar to strcmp: string hi = "hi"; string hi2 = "hi"; char ca[] = "hi"; string hi3 = ca; string there = "there"; hi.compare(hi2) == strcmp(hi.c_str(), hi2.c_str()) == 0 hi.compare(there) == strcmp(hi.c_str(), there.c_str() < 0 The big improvement, though: overloaded operators (hi == hi2) == true (hi == there) == false (hi < there) == true 1/18/2019 CS3101-2, Lecture 2
18
Input with strings – io2.cpp
Can read in primitive types: cout << “Enter two numbers: “; //sep-ed by whitespace cin >> x >> y; cout << “They\’re product is “ << x*y << endl; String input slightly more complicated string s; cout << “Enter some text: “; //reads until whitespace cin >> s; //hi there cout << “You entered: “ << s << endl; //hi string s, t; cout << “Enter two words: “; cin >> s >> t; //hi there cout << “You entered: “ << s << t << endl; //hithere Alternative: getline(cin, s); cout << “You entered: “ << s << endl; takes whole line until carriage return 1/18/2019 CS3101-2, Lecture 2
19
String input – io3.cpp >> treats all whitespace as delimiters
getline treats only ‘\n’ as a delim (by default) combining can be problematic Consider: int i; cout << "Enter i: "; cin >> i; cout << "i == " << i << endl; string s; cout << "enter s: "; getline(cin, s); cout << "s == " << s << endl; $ g++ io3.cpp $ a.out Enter i: 10 i == 10 enter s: s == Q: Why? A: cin >> i doesn’t take ‘\n’, so getline does 1/18/2019 CS3101-2, Lecture 2
20
String input – io3.cpp Soln 1: read in the ‘\n’ separately, after >> and before getline: cin.ignore(); string s; cout << "enter s: "; But only reads in fixed number of chars (1 by default) – same problem for spaces Soln 2: call call ignore with params: cin.ignore(1000, ‘\n’); ignores up 1000 chars, up to/including ‘\n’ Soln 3: call getline twice: getline(cin, s); getline(cin, s); … NB for C++-style strings: Length varies Size varies automatically 1/18/2019 CS3101-2, Lecture 2
21
Converting bet. C-style, C++-style
C C++: Converted in assignment char cs[] = “hi; string s = “hi”; s = cs; s = “hi”; C++ C: Use string’s member ftn string s “hi”; char cs[] = s.c_str(); C-style strings can also be input with getline Should pass max num chars getline(cin, s, sizeof(s)); 1/18/2019 CS3101-2, Lecture 2
22
Non-pointer data types
Used several data types: ints ~ whole numbers floating pts ~ “decimal” (non-whole) numbers chars ~ letters Will (next time) use generic complex data types: Structs/classes ~ multiple-field records/objects In general: a data type ~ class of poss vals, corresponding to real-life concept (letter, number, customer record) Indy var means/refers to/denotes today’s date., # pages, etc. But not always 1/18/2019 CS3101-2, Lecture 2
23
Non-pointer vars Vars are distinct
Changing one does not affect another: int x = 10; int y = x; x++; y == 10 Vars live in sep places in memory I.e., mapping:{var-names}{vars} is injective, “one-to-one”: Distinct var-names always refer to distinct vars Draw picture with x, y, array 1/18/2019 CS3101-2, Lecture 2
24
Pointers Do not correspond to real-life concept
Indy var means … another var Somewhat like quotes in natural language “Snow is white,” v. “’Snow’ has four letters.” Corresponds to computer hardware concept, memory address Theoretically, very interesting self-reference Halting Problem: interp nums as TMs Godel’s Incompleteness Theorem: interp nums as sentences ~ “I am Lying,” “This sentence is unprovable.” Here: interp nums as var addresses See 3261/4236, Godel, Escher, Bach, Douglas Hofstadter 1/18/2019 CS3101-2, Lecture 2
25
Pointers Q: What is a pointer?
A: K&R: “a var that contains the address of a var.” For regular (“first-order”) vars, we have var name, and value: char c = 10; Remember: var declar/def/init does: obtains mem for data attaches the name c to that mem location writes 10 there 1/18/2019 CS3101-2, Lecture 2
26
Var creation After declar/def/init, can use that data with the identifier c. When we say c, it remembers where to look The data lives somewhere in memory. Don’t have to know where the data actually is—just remember c. Could we find out the location? (Why?) Turns out: yes Ptrs are vars that take on these locations as vals 1/18/2019 CS3101-2, Lecture 2
27
Pointers p = &c & op applied to a var (lvalue) evals to its address;
p is now set to the address of c We interp val of p as a memory location p now “points to” c & applies only to lvalues Not consts, literals, const exprs Draw picture of p & c in mem 1/18/2019 CS3101-2, Lecture 2
28
& and * Exists: inverse operator to &: *
the dereferencing or indirection op. Applied to a pointer evals to the pointer’s referent what it points to int x = 1, y = 2, z[10]; /* init */ int *ip; /* declar */ ip = &x; /* ip points to x */ y = *ip; /* y set to val of ip’s referent, 0*/ *ip = 0; /* ip’s referent, x, set to 0 */ ip = &z[0]; /* ip now points to z[0] After execution, x == 0, y == 1 1/18/2019 CS3101-2, Lecture 2
29
& and * & and * (as ptr ops) are inverses Examples
Cancel each other out Examples *&x “the value at the address of x” x Who is buried in Grant’s tomb? &*xp “the address of the value at the address xp” xp Where is the contents of Grant’s tomb? 1/18/2019 CS3101-2, Lecture 2
30
Pointer declars To declar ptr-to-type var, similar to type declar, plus *: int *ip; Notice: primitives, arrays, ptrs, ftns, can all be declared together: int x, *ip, y, z[10], myftn(); Interp 1: ip is an int-pointer: (int*) ip combined declar can be confusing: int* ip, x; ip is int*, x is int Interp 2: dereferenced ip is an int: int (*ip) more readable: int *ip, x; ip is int*, x is int 1/18/2019 CS3101-2, Lecture 2
31
Dereferenced ptrs Dereferenced ptr gives the actual lvalue, not simply a passive value Can be used just as the corresponding var could be Both accessed and modified *ip = *ip + 1; *, &, other unaries have high precedence *ip += 1; 1/18/2019 CS3101-2, Lecture 2
32
Uninit-ed ptrs Do not dereference uninit-ed ptrs
if nec, init ptrs to NULL (==0) int *ip = NULL; int *ip = 0; then check for null before deref: if (ip != NULL) … if (ip) … 1/18/2019 CS3101-2, Lecture 2
33
Dereferenced ptrs What about ++? Unaries associate R-to-L But:
++*ip ++(*ip) But: *ip++ *(ip++) Need parenths ip++ is valid! “pointer arithmetic” 1/18/2019 CS3101-2, Lecture 2
34
Modifying ptrs vals Pointers can be modified, like other vars:
iq = ip; Example: int *ip, *iq, x; x = 5; ip = &x; *ip = 10; x == *ip == *iq == 10 1/18/2019 CS3101-2, Lecture 2
35
Uses of ptrs Limitation mentioned before: ftns have single return value Sometimes nice to have multiple return values e.g., some value, plus error/success. getchar() returns next char read, EOF (-1) if error okay, -1 isn’t a char consider a getint(): return next int read, or EOF (-1) if error problem: EOF == -1 is an int! Soln: let return value be error/success; write next int at location of a ptr param: int getint(int *); 1/18/2019 CS3101-2, Lecture 2
36
Passing by ref Remember: args passed-by-value by def.
Local var created, inited to val of arg Actual arg never changes Alternative method is pass-by-reference (not avail in C) Don’t pass the value, pass the location of the value Don’t send the webpage, send the URL Ptrs can simulate pass-by-ref 1/18/2019 CS3101-2, Lecture 2
37
Passing by ref: swap Swap two ints Naïve swap ftn:
void swap(int x, int y) { int temp = x; x = y; y = temp; } … int a = 5, b = 10; swap(a,b); Effect: nothing! x inited to val of a == 5, y inited to val of b == 10 x and y swapped a and b never change NB: couldn’t be otherwise Consider: swap(5,10); Can’t change literals 1/18/2019 CS3101-2, Lecture 2
38
Passing by ref: swap’ Soln 1: write a CPP macro, obviating real params
Soln 2: Don’t pass a and b’s vals, but ptrs to a and b void swap(int *px, int *py) { int temp = *px; *px = *py; *py = temp; } … int a = 5, b = 10; swap(&a,&b); NB: px, py are passed by value! These vals happen to be (interped as) refeences Draw picture! Soln 3: Arrays… 1/18/2019 CS3101-2, Lecture 2
39
Pointers & Arrays What do they have to do w/each other?
In Java, not much arrays are special sorts of objs, with many mems arrays, like all objs, are passed by reference two distinct ideas In C/C++: Ptrs and arrays turn out to be almost identical 1/18/2019 CS3101-2, Lecture 2
40
Pointers & Arrays Consider int a[10];
What happens? The id a is attached to a block of mem with room for 10 ints These ints accessed with a and a subscript: a[0], a[5] Important: the 10 ints are contiguous, in a row: a: a[0] gives an int &a[0] gives the address of that int 1/18/2019 CS3101-2, Lecture 2
41
Pointers & Arrays “Ordinary” ptrs can point to arrays: pa = &a[0];
Now can get other array elms” pa+1 points to next pa+2 pa++ pa-1 points to prev &a[0] 1/18/2019 CS3101-2, Lecture 2
42
Pointers & Arrays Q: How does it know they’re ints? A: pa is an int*
why ptrs need to know referent type Turns out: array name evals to ptr to 0th elm a == &a[0] & not applied array name 1/18/2019 CS3101-2, Lecture 2
43
Pointers & Arrays Also turns out:
a[i] (array index notation) evals to same as *(a+i) (ptr + offset notation) array[index] is really short-cut notation array[index] converted to *(array+index) in compilation Also, array[index] notation applicable to regular pointers: ip[20] *(ip+20) who knows? As usual, be careful! Q: What if 2[a]? A: 2[a] *(2+a) *(a+2) (by symmetry) a[2] 1/18/2019 CS3101-2, Lecture 2
44
Array/ptr example Consider a string-length ftn: int strlen(char *s) {
int n; for (n = 0; *s != ‘\0’; s++) n++; return n; } Now change to: int strlen(char s[]) { … } Effects? None! 1/18/2019 CS3101-2, Lecture 2
45
Arrs/ptrs as ftn args Can call: Same behavior:
strlen(“hi”), strlen(a), strlen(pa) Local var s set to pt to arg passed Same behavior: strlen(a+2), strlen(&a[2]) 1/18/2019 CS3101-2, Lecture 2
46
Passing by ref: swap’’ Soln 3: Pass an array containing a and b
void swap(int AandB[]) { int temp = AandB[0]; AandB[0] = AandB[1]; AandB[1] = temp; } … int AandB = {5, 10}; swap(AandB); NB: We pass one ptr (by value!), with the understanding that a and b are contiguous Draw picture 1/18/2019 CS3101-2, Lecture 2
47
Arrays v. pointers One important difference:
Defined-array values are immutable immutable = cannot be changed ~ “cannot mutate” Defined-array value = id used to define/create array Illegal: int a[10]; a++, a = … Restriction does not apply to: Array members Array params in a ftn (“formal params”) 1/18/2019 CS3101-2, Lecture 2
48
Pointer arithmetic Addition/subtraction with integers:
Ptr +/- n ptr to val distance n away Subtraction of two pointers: p – q distance bet. p and q p, q should point to mems of same array/data block or NULL No other legal ops (apart from ) 1/18/2019 CS3101-2, Lecture 2
49
Pointer arithmetic e.g. int strlen(char *s) { char *p = s;
while (*p != ‘\0’) p++; return p-s; } Loops until ‘\0’ Consider “Hi” ~ {‘H’,’i’,’\0’} Initly, p == s ‘H’ ‘H’ != ‘\0’ p++ /* p-s == 1 */ ‘i’ != ‘\0’ p++ /* p-s == 2 */ ‘\0’ == ‘\0’ return 2 1/18/2019 CS3101-2, Lecture 2
50
Pointer arithmetic Here: For other elm types:
p-as-number – s-as-number == p-s == 2 For other elm types: p-as-number – s-as-number == (p-s)*sizeof(type) Sizeof gives # in var/type: sizeof(int), sizeof(x) No effect here since sizeof(char) == 1 Show sizeof.c 1/18/2019 CS3101-2, Lecture 2
51
String literals “Hi” {‘H’, ‘i’, ‘\0’}
len-n str literal is a len-n+1 char[] Signal ‘\0’ indicates the end Same effect: char am[] = “hi”; /* am is immutable */ char am[] = {‘H’, ‘i’, ‘\0’}; Slightly different effect: char *pm = “hi”; /* pm is mutable */ pm: am: &am[0] 1/18/2019 CS3101-2, Lecture 2
52
Copying strings How to copy strings? s = t;?
No: simply makes s and t pt to same chars Want: copy actual chars, so indy, from source t to dest s For now: assume s pts to enough free space Copy strings as arrays: void strcpy(char s[], char t[]) { int i = 0; while ((s[i] = t[i]) != ‘\0’) i++; } Each time around, 1) Assign s char to t char; 2) check for \0 NB: Assign expr evals to char assigned 1/18/2019 CS3101-2, Lecture 2
53
Copying strings’ Copy strings as pointers:
void strcpy(char *s, char *t) { while ((*s = *t) != ‘\0’) { s++; t++; } Essentially the same as prev No index needed, but now inc both ptrs 1/18/2019 CS3101-2, Lecture 2
54
Copying strings’’ Mix & match: void strcpy(char s[], char *t) {
/* s arr, t ptr */ int i = 0; while ((*s = t[i]) != ‘\0’) { s++; /* s ptr */ i++; /* t arr */ } 1/18/2019 CS3101-2, Lecture 2
55
Copying strings’’’ Shorter version: void strcpy(char *s, char *t) {
while ((*s++ = *t++) != ‘\0’) ; } Each time around: *s set to *t, check for ‘\0’ But: unaries assoc R-to-L So, Q: why do we get *t instead of *(t+1) A: in *t++, ++ is eval-ed before * But postfix ++ *lval++ evals to simply lval After eval, lval is inc-ed So *s set to *t; both inc-ed 1/18/2019 CS3101-2, Lecture 2
56
Copying strings’’’’ Q: What is the value of the null char ‘\0’?
A: 0; any other char is != 0 Can take advantage of this in if Shortest (?) version: void strcpy(char *s, char *t) { while ((*s++ = *t++)) ; } Real strcpy lives in <cstring> 1/18/2019 CS3101-2, Lecture 2
57
Comparing strings Return <0 if s < t, 0 if s==t, >0 o.w.
int strcmp(char *s, char *t) { int i; for (i = 0; s[i] == t[i]; i++) if (s[i] == ‘\0’) return 0; return s[i] – t[i]; } NB: two poss outcomes: Get through all of s w/o discrepancy 0 Find discrepancy amt greater s[i] than t[i] 1/18/2019 CS3101-2, Lecture 2
58
Comparing strings’ With arrays: int strcmp(char *s, char *t) {
for(; *s == *t; s++, t++) if (*s == ‘\0’) return 0; return *s - *t; } Omits i but needs , to combine inc stmts Very rarely used 1/18/2019 CS3101-2, Lecture 2
59
Pointers & I/O – ptrio.cpp
A pointer is just a memory address (== number) We can look at the number itself: int x = 10; int *xp = &x; cout << "x == " << x << endl; cout << "xp == " << xp << endl; $ a.out x == 10 xp == 0xffbffa9c printed in hex by default What about char pointers? by default, printed as strings usually what we want to get address, reinterpret as generic pointer char s[] = "hi"; cout << "s == " << s << endl; cout << "s as int == " << reinterpret_cast<void*>(s) << endl; s == hi s as int == 0xffbffa90 1/18/2019 CS3101-2, Lecture 2
60
Multi-D arrays 2-d array is just: array of arrays
Why we say b[1][2] rather than b[1,2] In same way: given 2-d array type array of 2-d arrays == 3-d array a[2][6][34]… No fixed limit 1/18/2019 CS3101-2, Lecture 2
61
2-D arrays - TicTacToe Inner braces optional X O char board[3][3] = {
{‘X’,’X’,’O’}, {’X’,‘O’,’X’}, {’ ’,’X’,’X’} }; Inner braces optional X O b[0][0] b[0][1] b[0][2] b[1][0] b[1][1] b[1][2] b[2][0] b[2][1] b[2][2] 1000 1001 1002 1003 1004 1005 1006 1007 1008 b[0][0] b[0][1] b[0][2] b[1][0] b[1][1] b[1][2] b[2][0] b[2][1] b[2][2] 1/18/2019 CS3101-2, Lecture 2
62
Cmd-line args – params.cpp
Canonical main function: int main(int argc, char *argv[]) { … } argc: argument count argv: argument vector exec name and params are tokenized as mems of argv $ a.out here are some params argv[0] == a.out argv[1] == here argv[2] == are argv[3] == some argv[4] == params 1/18/2019 CS3101-2, Lecture 2
63
Cmd-line args – params2.cpp
Many programs take params of the form: -P<param> -Wall -Dmacro==def Parsing params of this form quite elegant while (argc-- >= 0 && argv[argc-][0] == ‘-’)) { char *val = argv[argc-]+2; switch (argv[argc-][1]) { case ‘D’: case ‘d’: cout << “macro def: “ << val << endl; break; case ‘W’: case ‘w’: cout << “warnings: “ << val << endl; default: print_usage(); } 1/18/2019 CS3101-2, Lecture 2
64
switch statement Replaces many if-elses Similar to those in C, J
Applies only to integral data types Upon entering case, “falls through” until break allows combine cases: case ‘D’: case ‘d’: If no match, executes optional default case 1/18/2019 CS3101-2, Lecture 2
65
Constant ptrs, ptrs to constants
Can make var const (immutable) with const: const double PI = 3.14; int ftn(const int param) { … } const int: const int X = 10; int const X = 10; ptr-to-const: const int *cip = &x; int const *cip = &x; const ptr: int *const IP = &x; const ptr-to-const: const int *const CIP; int const *const CIP; 1/18/2019 CS3101-2, Lecture 2
66
const & ptr examples – const.cpp
const int x = 10; int * ip = &x; //error/cancels const const int * cip = &x; //val is const, ptr is not (*cip)++; //not allowed cip++; //is allowed int y = 11; int * const IP = &y; //ptr is const, val is not (*IP)++; //is allowed IP++; //not allowed const int * const CIP = &x; (*CIP)++; //not allowed CIP++; //not allowed 1/18/2019 CS3101-2, Lecture 2
67
Consts & arrays – const2.cpp
Array of consts: const int nums[10] = {1,2,3}; const int *xp = num[2]; //must be ptr to const *xp = 10; // not allowed Const array? Array is automatically const 1/18/2019 CS3101-2, Lecture 2
68
References C++ introduces the concept of a reference Example:
Often replaces need for ptrs Example: int x = 10; int &y = x; y++; x == 11 y is an alias for x y refers to x more precisely, x, y refer to same entity in mem Reference vars behave like ptrs, obj vars in J: obj1 = obj2; obj1, obj2 refer to same thing 1/18/2019 CS3101-2, Lecture 2
69
Reference params Reference vars mostly used for params Example:
void square(double &num) { num *= num; } Param is used like regular var (double) Doesn’t req dereferencing But is alias to original argument 1/18/2019 CS3101-2, Lecture 2
70
Reference params: swap’’’
Soln 4 for swapping: references void swap(int &x, int &y) { int temp = x; x = y; y = temp; } … int a = 5, b = 10; swap(a,b); NB: Only difference from original (erroneous) swap ftn is &x, &y 1/18/2019 CS3101-2, Lecture 2
71
Benefits of references
simpler because of no dereferencing safer because more limited references must be init-ed when defined int &x; does not compile 2. references cannot be null 3. references are immutable int &x = y; x = z; resets x’s value (y’s value) to z’s value &x just gives address of referent (== &y) 1/18/2019 CS3101-2, Lecture 2
72
References & pointers Most compilers translate ref-code to ptr-code
just as array-code is translated to ptr-code same underlying concept The safety comes from the restrictions imposed on ref-code before translation References used very widely in overloaded operations Will write some next time 1/18/2019 CS3101-2, Lecture 2
73
References, ptrs, and things
int x = 4; //thing &x //address of thing int *xp = &x; //ptr to thing *xp = 10; //thing int &rx = x; //reference to thing rx = 10; //thing x = 11; //same thing: x == rx int *rxp = ℞ //ptr to thing *rxp = 20; /*same thing: *rxp == rx == x */ 1/18/2019 CS3101-2, Lecture 2
74
Refs & consts – consts2.cpp
What you’d expect, similar to arrays Reference to const: const int x = 10; const int &rx = x; //must be const rx = 10; //not allowed Const reference? References automatically const 1/18/2019 CS3101-2, Lecture 2
75
References & objects In Java, refs & objects tightly coupled
obj1 = obj2; obj1.changeMe(); obj2 changed too In C++, refs & objects independent int x = 10; int &rx = x; rx++; rx, x both changed string s1 = “hi”, s2 = “there”; s1 = s2; s1.append(“abc”); s1 changed but not s2 1/18/2019 CS3101-2, Lecture 2
76
Next time Q: How can an object be assigned (by value) to another?
A: With the copy-constructor Topic next time: Classes For next time: Do reading assigned on web Hw1 will be up tonight Done individually Sign in before you leave! 1/18/2019 CS3101-2, Lecture 2
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.