Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Similar documents
Chapter 11: More About Classes and Object-Oriented Programming

Make Classes Useful Again

Overloading Operators in C++

Evolution of Programming Languages

A <Basic> C++ Course

A <Basic> C++ Course

Lab Instructor : Jean Lai

Where do we go from here?

More Advanced Class Concepts

Object-Oriented Design (OOD) and C++

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Module Operator Overloading and Type Conversion. Table of Contents

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

UNIT- 3 Introduction to C++

Fast Introduction to Object Oriented Programming and C++

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

7.1 Optional Parameters

Ch 8. Operator Overloading, Friends, and References

CS 247: Software Engineering Principles. ADT Design

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

OBJECT ORIENTED PROGRAMMING USING C++

ADTs & Classes. An introduction

Function Overloading

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Operator overloading: extra examples

Operation Overloading.

Introduction to C++ (Extensions to C)

Introduction to C++ Systems Programming

Operator overloading

Chapter 18 - C++ Operator Overloading

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Operator Overloading in C++ Systems Programming

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Due Date: See Blackboard

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

11.2. Overloading Operators

G52CPP C++ Programming Lecture 17

Chapter 2. Procedural Programming

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

III. Classes (Chap. 3)

Roxana Dumitrescu. C++ in Financial Mathematics

A First Program - Greeting.cpp

Lecture 23: Pointer Arithmetic

PIC 10A Objects/Classes

Object-Oriented Principles and Practice / C++

Object-Oriented Programming

Cpt S 122 Data Structures. Introduction to C++ Part II

Ch. 12: Operator Overloading

Interview Questions of C++

CS2141 Software Development using C/C++ C++ Basics

Computer Science II CSci 1200 Lecture 18 Operators and Friends

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

IS 0020 Program Design and Software Tools

W3101: Programming Languages C++ Ramana Isukapalli

pointers & references

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Review of the C Programming Language for Principles of Operating Systems

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Overloading & Polymorphism

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

CS11 Intro C++ Spring 2018 Lecture 5

Implementing Abstract Data Types (ADT) using Classes

Circle all of the following which would make sense as the function prototype.

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Use the dot operator to access a member of a specific object.

Introduction to C++ Introduction to C++ 1

Chapter 3. Numeric Types, Expressions, and Output

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Instantiation of Template class

Operator overloading. Instructor: Bakhyt Bakiyev

Review of Important Topics in CS1600. Functions Arrays C-strings

Recharge (int, int, int); //constructor declared void disply();

CS304 Object Oriented Programming Final Term

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Implementing an ADT with a Class

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Short Notes of CS201

by Pearson Education, Inc. All Rights Reserved.

Abstract Data Types. Development and Implementation. Well-defined representations that allow objects to be created and used in an intuitive manner

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

C++ Programming: From Problem Analysis to Program Design, Third Edition

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CS201 - Introduction to Programming Glossary By

Generic Programming: Overloading and Templates

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Transcription:

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 argument s value is made and passed to the called function. Changes to the copy do not affect the original variable s value in the caller.

Reference Parameters pass-by-reference The caller passes the address of its data. The caller gives the called function the ability to access the caller s data directly and to modify it if the called function chooses to do so.

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that the parameter is passed by reference. All operations performed on the alias (i.e. the reference) are actually performed on the original variable. The alias is simply another name for the original variable.

Reference Parameters "pass-by-value in C" example: #include <stdio.h> void swap(int first, int second); // prototype int main() { int x = 5; int y = 6; } printf("before: x = %d y = %d\n", x, y); swap(x, y); //were the integers swapped? printf("after: x = %d y = %d\n", x, y); return 0; void swap(int first, int second) { int temp; temp = first; first = second; second = temp; } Output: Before: x = 5 y = 6 After: x = 5 y = 6 "simulating pass-by-reference in C" example: #include <stdio.h> void swap(int *first, int *second); // prototype int main() { int x = 5; int y = 6; } printf("before: x = %d y = %d\n", x, y); swap(&x, &y); //were the integers swapped? printf("after: x = %d y = %d\n", x, y); return 0; void swap(int *first, int *second) { int temp; temp = *first; *first = *second; *second = temp; } Output: Before: x = 5 y = 6 After: x = 6 y = 5

Reference Parameters in C++ C++ has true reference true "pass-by-reference". Use the & operator to denote a reference parameter, e.g. void swap(int &a, int &b); or void swap(int &, int &);

Reference Parameters in C++ "pass-by-value in C++" example: #include <iostream> using namespace std; void swap(int first, int second); // prototype int main() { int x = 5; int y = 6; } cout << "\nbefore: x = " << x << " y = "<< y; swap(x, y); //were the integers swapped? cout << "\nafter: x = " << x << " y = " << y << endl; return 0; void swap(int first, int second) { int temp; temp = first; first = second; second = temp; } Output: Before: x = 5 y = 6 After: x = 5 y = 6 "pass-by-reference in C++" example: #include <iostream> using namespace std; void swap(int &first, int &second); // prototype int main() { int x = 5; int y = 6; } cout << "\nbefore: x = " << x << " y = " << y; swap(x, y); //were the integers swapped? cout << "\nafter: x = " << x << " y = " << y << endl; return 0; void swap(int &first, int &second) { int temp; temp = first; first = second; second = temp; } Output: Before: x = 5 y = 6 After: x = 6 y = 5

Reference Parameters Pass-by-reference is easier and more natural for some programmers than simulating pass-by-reference with pointers. Some programmers prefer using pointers, because * in the parameter list and the & in the call, make it explicitly clear that the called function will alter the caller s data. With call-by-reference, the parameter list in the function call is identical to the parameter list for call-by-value.

Function Overloading A class may have several methods that have the same name. This is referred to as function overloading. When a function name is overloaded, the implementation that is actually used is the one whose formal parameters match the actual arguments being passed by the caller in both number of arguments and type of argument. Therefore, it is mandatory that each implementation has distinguishable parameters.

Operator Overloading (Restrictions) Most operators can be overloaded. The ones that cannot are. (member access),.* (member access through pointer to member), :: (scope resolution),?: (ternary conditional), and sizeof

Operator Overloading (Restrictions) New operators such as **, <>, or & cannot be created. The overloads of operators &&,, and, (comma) lose their special properties: shortcircuit evaluation and sequencing. The overload of operator -> must either return a raw pointer or return an object (by reference or by value), for which operator -> is in turn overloaded. It is not possible to change the precedence, grouping, or number of operands of operators.

Operator Overloading Operator overloading is actually part of the function overloading mechanism. The name of the function for an overloaded operator is operator followed by the operator symbol, e.g., Operator Function name + operator+ - operator- * operator* <= operator<= -> operator->

Operator Overloading Operator overloading is restricted to existing operators. Thus it is not legal to try to overload ** **operator** Operators can be overloaded as instance member functions, or as friend functions

Operator Overloading The operator functions work "almost" exactly like "regular" functions. They can even be invoked using their operator+ or operatornames. Keeping this fact in mind will remove much of the mystery from how they work and how they must be implemented. The "almost" qualifier above reflects necessity to remember that almost all C operators take either one or two operands. Thus an operator function has at most two parameters.

Operator Overloading The C addition operator takes two operands: a + b Therefore the operator+ function will have two parameters: the first will represent the left side operand and the second the right side operand. The C logical not operator takes one operand:!value Therefore the operator! function will have one parameter and it will represent the right side operand.

Operator Overloading Operator overloading must preserve the "aryness" (unary or binary) nature of the operator. Thus, the! operator could be overloaded to compute the length of a single vector but could not be used to compute a dot product. The operators &, *, +, - have both binary and unary versions that may be overloaded separately. It is not possible to change the precedence or associativity of an overloaded operator.

Operator Overloading Most operator functions return a value that replaces the operator and its operand(s) in an expression. However, that is not mandatory. For example a side effect operator such as ++ may not need to return a value.

Overloading Operators as Instance Members A binary operator that is overloaded as an instance member needs only one parameter, which represents the operand on the right: class OpClass { private: int x; public: OpClass operator+(opclass right); };

Overloading Operators as Instance Members The left operand of the overloaded binary operator is the calling object The implicit left parameter is accessed through the this pointer OpClass OpClass::operator+(OpClass r) { OpClass sum; sum.x = this->x + r.x; return sum; }

Invoking an Overloaded Operator Operator can be invoked as a member function: OpClass a, b, s; s = a.operator+(b); It can also be invoked in the more conventional manner: OpClass a, b, s; s = a + b;

Overloading Assignment Overloading the assignment operator solves problems with object assignment when an object contains pointer to dynamic memory. Assignment operator is most naturally overloaded as an instance member function It needs to return a value of the assigned object to allow cascaded assignments such as a = b = c;

Overloading Assignment Assignment overloaded as a member function: class CpClass { int *p; public: CpClass(int v=0) { p = new int; *p = v; ~CpClass(){delete p;} CpClass operator=(cpclass); };

Overloading Assignment Implementation returns a value: CpClass CpClass::operator=(CpClass r) { *p = *r.p; return *this; }; Invoking the assignment operator: CpClass a, x(45); a.operator=(x); // either of these a = x; // lines can be used

Overloading << and >> The overloaded operators << and >> can be used to print and to read numeric and character string values to stdout and stderr and from stdin, respectively. True to form, they can be further overloaded to print and read a complete object, such as a Rational object.

Overloading << and >> Given the following class declaration for class Complex: #ifndef COMPLEX_H #define COMPLEX_H class Complex{ public: Complex( ); Complex(double realinit, double imaginaryinit); void setcomplex(double realinit, double imaginaryinit); Complex add(const Complex &rhs) const; Complex subtract(const Complex &rhs) const; double magnitute( ); void printcomplex( ); private: double real; double imaginary; }; #endif

Operator Overloading We can overload the following operators to add, subtract, multiply, and two objects: Complex operator+ (const Complex &rhs) const; Rational operator- (const Complex &rhs) const; Rational operator* (const Complex &rhs) const;

Overloading + Complex::operator+ (const Complex &rhs) const { Complex sum; sum.real = real + rhs.real; sum.imaginary = imaginary + rhs.imaginary; return sum; }

Overloading << and >> Given: Complex c; We want to be able to do something like cout << c; or cin >> c; where r1 is a Rational object. Since the left hand side of >> or << is not a Complex, we must use the friend function form. So in complex.h we include: friend ostream &operator<<(ostream &out, const Complex &c);

Overloading << and >> We provide the implementation in either rational.cpp or inline in the class definition. ostream &operator<<(ostream &out, const Complex &c) { char op = '+'; if (imaginary < 0) op = '-'; out << "(" << real << " " << op << " " << fabs(imaginary) << "i)"; } return(out);

Overloading << and >> Note that our new overload just uses the built in overload of << to output each component of the Complex object. This is the key to cascading application of the << operator

Overloading << and >> The implementation of >> is similar istream &operator>>(istream &in, const Complex &c) { in >> c.real>> c.imaginary; return in; }

Overloading Types of Operators ++, -- operators overloaded differently for prefix vs. postfix notation Overloaded relational operators should return a bool value Overloaded stream operators >>, << must return istream, ostream objects and take istream, ostream objects as parameters

Overloaded [] Operator Can be used to create classes that behave like arrays, providing bounds-checking on subscripts Overloaded [] returns a reference to object, not an object itself

Type Conversion Operators Conversion Operators are member functions that tell the compiler how to convert an object of the class type to a value of another type The conversion information provided by the conversion operators is automatically used by the compiler in assignments, initializations, and parameter passing

Syntax of Conversion Operators Conversion operator must be a member function of the class you are converting from The name of the operator is the name of the type you are converting to The operator does not specify a return type

Conversion Operator Example To convert from a class IntVal to an integer: class IntVal { public: IntVal(int a = 0) { x = a; } operator int() { return x; } private: int x; };

Conversion Operator Example Automatic conversion during assignment: IntVal obj(15); int i; i = obj; cout << i; // prints 15

Convert Constructors Convert constructors are constructors that take a single parameter of a type other than the class in which they are defined class CCClass { int x; public: CCClass() //default CCClass(int a, int b); CCClass(int a); //convert CCClass(string s); //convert };

Example of a Convert Constructor The C++ string class has a convert constructor that converts from C-strings: class string { public: string(char *); //convert };

Uses of Convert Constructors They are automatically invoked by the compiler to create an object from the value passed as parameter: string s("hello"); //convert C-string CCClass obj(24); //convert int The compiler allows convert constructors to be invoked with assignment-like notation: string s = "hello"; //convert C-string CCClass obj = 24; //convert int

Uses of Convert Constructors Convert constructors allow functions that take the class type as parameter to take parameters of other types: void myfun(string s); // needs string // object myfun("hello"); // accepts C-string void myfun(ccclass c); myfun(34); // accepts int