1 Reference Variables Chapter 8 Page 383 - 408. 2 Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.

Slides:



Advertisements
Similar presentations
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Parameter Passing Mechanisms Reference Parameters.
1 Derived Classes and Inheritance Chapter Objectives You will be able to: Create derived classes in C#. Understand polymorphism. Write polymorphic.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
11 Introduction to Object Oriented Programming (Continued) Cats II.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Binary Search Trees II Morse Code.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
ITEC 320 C++ Examples.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
1 CS161 Introduction to Computer Science Topic #10.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
11 Introduction to Object Oriented Programming (Continued) Cats.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 5 Function Basics
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
group work #hifiTeam
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers & Functions.
Functions Pass By Value Pass by Reference
C++ Pointers and Strings
Pointers and Pointer-Based Strings
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
C++ Pointers and Strings
Presentation transcript:

1 Reference Variables Chapter 8 Page

2 Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively an alias for the variable to which it refers. Used exactly like the the variable to which it refers. No * & after type in a declaration makes it a reference.

3 Reference Variables You can declare reference variables See pages 340 – 344 Must be initialized where declared. Another name for an existing variable. Not very useful! Function parameters can be references. Reference parameter is an alias for the argument specified in the function call. Not a copy! Very useful! "Call by reference"

4 Reference Variables as Function Parameters Example: void swap(int& a, int& b) When a function parameter is a reference, the function has direct access to the variable passed by the caller. As if the caller had passed a pointer. But you don't dereference the parameter. Use the parameter as if were the caller's variable.

5 Swap Example Let's implement a "swap" function using reference parameters Create a new empty project in Visual Studio. Add new item main.cpp

6 Reference Example: Swap #include using namespace std; void swap(int& a, int& b) { int temp = a; a = b; b = temp; }

7 Arguments for Reference Parameters Write the call to a function with reference parameters as if you were passing the values of the variables that you want to swap. No “&” Note: You can't tell by looking at the call whether it is passing values or references. You have to look at the function signature.

8 Reference Example: Swap int main() { int x = 1; int y = 2; cout << "Before call to swap: " << endl; cout << "x = " << x << " y = " << y << endl; swap (x, y); cout << "After call to swap: " << endl; cout << "x = " << x << " y = " << y << endl; cin.get(); return 0; }

9 Program swap Running

10 Things to Notice Reference parameters are initialized on the function call. Function acts on caller's variables. as with pointers No dereferencing operator Compare * for pointers Reference variable cannot be made to refer to a different memory location. Consider: a = b;

11 References to Objects Download project Circle_Demo Downloads/2016_01_29/ Expanded and improved version of our original Circle program. Extract All Open project in Visual Studio

12 Circle.h #pragma once #include using namespace std; class Circle { private: string name; double radius; public: Circle(string Name, double Radius); string Name() const { return name; }; double Radius() const { return radius; }; void Display() const; double Area() const; };

13 Circle.cpp #include "Circle.h" #include Circle::Circle(string name_, double radius_) { name = name_; radius = radius_; }

14 Circle.cpp double Circle::Area() const { return * radius * radius; } void Circle::Display() const { cout << name << " is a Circle of radius " << radius << std::endl; }

15 Circle_Test.cpp #include #include "Circle.h" Circle* Create_Circle() { string name; double radius; cout << "Name? "; getline(cin, name); cout << "Radius? "; cin >> radius; cin.get(); // Clear keyboard input buffer return new Circle(name, radius); }

16 Circle_Test.cpp int main() { Circle* c1 = Create_Circle(); double c1_area = c1->Area(); cout Name() << " is " << c1_area << endl; cin.get(); // Hold window open cin.get(); return 0; } Build and run.

17 Program Running

Comparing Circles Let’s add a method to class Circle to permit users to compare Circles. Pass Circle to compare by reference In Circle.h: bool Is_Greater_Than(const Circle& other) const; 18

19 Comparing Circles In Circle.cpp bool Circle::Is_Greater_Than(const Circle& other) const { return this->radius > other.radius; }

20 Comparing Circles int main() { Circle* c1 = Create_Circle(); double c1_area = c1->Area(); cout Name() << " is " << c1_area << endl; Circle* c2 = Create_Circle(); double c2_area = c2->Area(); cout Name() << " is " << c2_area << endl;

21 Comparing Circles if (c1->Is_Greater_Than(*c2)) { cout Name() << " is greater than " Name() << endl; } else if (c2->Is_Greater_Than(*c1)) { cout Name() << " is greater than " Name() << endl; } else { cout Name() Name() << " are the same size\n"; }

22 Program Running

23 Summary References are good! Use them as function parameters instead of pointers in C++. Make reference parameters const whenever possible. Also the function itself!