1 c++ reference parameters math 130 b smith: 40 to 45 min. rate:3. important discussion on reference...

17
1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: Subtle but critical!: Give two reasons a function would use pass by reference? Answer: 1. To provide a called function direct access to the passed variable. 2. To optimize speed of program. With call-by-reference, the copy constructor will not be called with each function call.

Upload: sammy-greaver

Post on 15-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

1

C++ Reference Parameters

Math 130

B Smith:

40 to 45 min. Rate:3. Important discussion on reference parameters!

B Smith:

40 to 45 min. Rate:3. Important discussion on reference parameters!

B Smith:

Subtle but critical!:

Give two reasons a function would use pass by reference?

 

Answer:

1. To provide a called function direct access to the passed variable.

2. To optimize speed of program. With call-by-reference, the copy constructor will not be called with each function call.

B Smith:

Subtle but critical!:

Give two reasons a function would use pass by reference?

 

Answer:

1. To provide a called function direct access to the passed variable.

2. To optimize speed of program. With call-by-reference, the copy constructor will not be called with each function call.

Page 2: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

2

Overview

• Reference Parameters

• Common Errors

• Rules of References

• Use of const

• References as Function Arguments

B Smith:

Modify to show learning objects

B Smith:

Modify to show learning objects

Page 3: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

3

Reference Parameters

• In function arguments

C and C++ use call-by-value primarily How would you define call-by-reference ?

• In C, call-by-reference requires that we call functions using parameters of data type...? pointer

• C++ introduces another means for passing function arguments via call-by-reference

• This new parameter type in C++ is called a reference parameter

Page 4: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

4

Call-By-Reference in C++

void getInput(double& receiver)

{

cout << “enter input number: \n”:

cin >> receiver;

}

The & can also be placed with the parameter name

void getInput(double &receiver);

Page 5: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

5

Reference Parameters

• A reference variable (&) is like a pointer that is automatically dereferenced

• Reference variables provide us an alias for a previously declared variable

Page 6: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

6

Passing by Value and by Reference#include <iostream>using namespace std; void f(int, int&);

int main(){ int m=22; int n=44; cout << "m= " << m << endl; cout << "n= " << n << endl; f(m,n); cout << "m= " << m << endl; cout << "n= " << n << endl; }

void f(int x, int& y){ x = x+1000; y = y*1000;}

Page 7: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

7

swap with pointers //Demonstrates passing by reference #include <iostream> void swap(int *x, int *y); int main() { int x = 5, y = 10; cout << "Main. Before swap, x: " << x << " y: " << y << "\n"; swap(&x, &y); cout << "Main. After swap, x: " << x << " y: " << y << "\n"; return 0; } void swap (int *px, int *py) { int temp; cout << "Swap. Before swap, *px: " << *px << " *py: " << *py << "\n"; temp = *px; *px = *py; *py = temp; cout << "Swap. After swap, *px: " << *px << " *py: " << *py << "\n"; }

Page 8: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

8

swap with references //Demonstrates passing by reference // using references! #include <iostream> void swap(int& x, int& y); int main() { int x = 5, y = 10; cout << "Main. Before swap, x: " << x << " y: " << y << "\n"; swap(x,y); cout << "Main. After swap, x: " << x << " y: " << y << "\n"; return 0; } void swap (int& rx, int& ry) { int temp; cout << "Swap. Before swap, rx: " << rx << " ry: " << ry << "\n"; temp = rx; rx = ry; ry = temp; cout << "Swap. After swap, rx: " << rx << " ry: " << ry << "\n"; }

Page 9: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

9

What will be the output?

#include <iostream>int main ( ){ float total = 20.5 ; float& sum = total ; cout <<"sum = "<< sum << endl ; sum = 18.6 ; cout << "total = “ << total << endl ; return 0 ;}

declare and initialize total

declare another name for total

this changes the value in total

Page 10: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

10

Common Errors with References

• The references should be of the same data type as the variable to which it refers.

• What is the output of the following program segment? (If it even compiles!)

#include <iostream>using namespace std;int main(){ int num = 10; float& numref = num; numref = 23.6; cout <<"The value of num is " << num << endl; <<"The value of numref is "<< numref << endl; return 0;}

this does not equate numref to num

Page 11: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

11

Reassignment of References

int main(){ int intOne; int &aRef = intOne;

intOne = 5;

int intTwo = 8; aRef = intTwo; return 0;}

aRef: 8intTwo:

5intOne: 58

The reinitialization of the

reference variable failed!

B Smith:

just discussed in previous slide. This slide is only useful to the extent that it shows that you’re unable to dereference.

B Smith:

just discussed in previous slide. This slide is only useful to the extent that it shows that you’re unable to dereference.

Page 12: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

13

Keyword const• To help avoid this type of confusion, C++ allows you to

explictly prevent changing the value of the referenced object

• Using const designates aRef as read-onlyint main(){ int intOne; const int &aRef = intOne;

intOne = 5;

int intTwo = 8; aRef = intTwo; //the compiler will catch return 0;}

try C++ Source File

Page 13: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

14

const: Multiple Uses

• Data objects qualified by const cannot be modified after they have been initialized

• Functions qualified with const can not modify the member variable’s data:

int main(){ int intOne; const int &aRef = intOne;

intOne = 5;

int intTwo = 8; aRef = intTwo; //compiler catches return 0;}

class Cat

{

public:

Cat(int initialAge);

int GetAge() const;

private:

int itsAge;

};

. . .

void Cat::GetAge() const

{ return itsAge;

}

void Cat::GetAge() const {return itsAge;}

Page 14: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

15

const – It’s Good Software Engineering

• Use const wherever possible in your programs It reduces the likelihood of unintentional modification It communicates to other programmers your intentions

to restrict modification to data members

Page 15: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

16

constant call-by-reference parameter

int isLarger(BankAccount account1, BankAccount account2)

//Returns true if the balance in acct1 is gtr than that

//in acct2. Otherwise returns false.

{

return(account1.getBalance() > account2.getBalance());

}

int isLarger(const BankAccount& account1,

const BankAccount& account2)

//Returns true if the balance in acct1 is gtr than that

//in acct2. Otherwise returns false.

{

return(account1.getBalance() > account2.getBalance());

}

Page 16: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

17

Rules of Reference

• Use them to create an alias to an object

• When a reference is created, it must be initialized pointers can be initialized anytime

• Do not try to reassign a reference but pointers can point to another object at anytime

• Use const to help prevent bugs in your code

• Don’t confuse the “address of” operator with the reference operator

Page 17: 1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important

21

Summary• Reference Parameters

• Common Errors

• Rules of References• References as Function Arguments