Unformatted text preview:

Lecture 9OutlinePoint Class 1Point Class 2Default Arguments 1Default Arguments 2Const Reference Parameters 1Const Reference Parameters 2Const Reference Parameters 3Function OverloadingOverloaded Operators 1Overloaded Operators 2Overloaded Operators 3Member Function OperatorsFree Function Operators 1Free Function Operators 2Operators << and >>Friend Function Operators 1Friend Function Operators 2Overloaded Operators 4Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 1Lecture 9Log into LinuxQuestions about last Friday's in-class exercise.Homework 5 posted; due on Friday. Extends the in-class exercise from last Friday.Submission system will accept Project 1. Homework 5 will go up on Wednesday evening.How many have started Project 1? If you have not started, you are already late!!Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 2OutlineExample class: pointDefault argumentsConst reference parametersFunction overloadingOverloaded operatorsAs a free functionAs a member functionAs a friend functionMonday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 3Example: Point ClassCopy files from csserver: /home/hwang/cs215/lecture09/*.*Examine pointv1.h and pointv1.cpp. Similar, but not the same as textbook.The point class models the location of a point on a Cartesian plane. The point class has two attributes:x – the x-coordinate of the pointy – the y-coordinate of the pointMonday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 4Example: Point ClassThe Point class operations are:Explicit-value constructor with default arguments to initialize the pointshift – a member function that receives (two) amounts to shift the point along the x and y axes.rotate90 – a member function to rotate the point by 90 degrees in a clockwise directionget_x, get_y – member functions that return the x and y coordinates, respectivelyDistance – a free function that receives two points and returns the distance between themMonday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 5Default ArgumentsThe explicit-value constructor's prototype has default arguments:point (double initial_x = 0.0, initial_y = 0.0);A default argument is a value that will be used for an argument when a function is called without one. It is specified only once in the prototype, not in the function definition.Both default arguments and missing arguments in a function call must be in the right-most positions.Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 6Default ArgumentsExamples from inclass9v1.cpp:point p1(-1.0, 0.8); // "Usual" callpoint p2(-1.0); // One argument; initial_y = 0.0.point p3; // Default construction; // Using 0.0 for both parametersAny constructor that may be called with no arguments can be used as a default constructor. By using default arguments, often need to write only one constructor to handle all initialization patterns.Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 7Const Reference ParametersRecall that function parameters are described as received or passed back in design specifications.Received parameters can be implemented using a C++ value parameter. But recall that the value parameter mechanism makes a copy of the actual argument.This can be very inefficient for large objects, so we would like to implement these received parameters as C++ reference parameters.Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 8Const Reference ParametersBut a reference parameter is an alias for the actual argument and changes to the parameter change the actual argument, which is not what we want to happen to a received parameter.To prevent any changes from happening in this case, the reference parameter is made const. E.g., the function that computes the distance between two points:double Distance (const point & p1, const point & p2);Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 9Const Reference ParametersThe rule is that all received only class object parameters (including system-provided object types like string) are implemented using const reference parameters.Note that this has always been the case for arrays, which are passed by reference automatically (but do not use the &). E.g.,int Search (const string names[], int numElements, const string & target);received-only string parameterMonday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 10Function OverloadingC++ allows function overloading. That is more than one function definition may have the same name. The compiler must be able to distinguish which function to use based on the arguments of a call, so at least one of the following must be true:The number of parameters is different.The type of at least one of the parameters is different.E.g., constructors are often overloadedMonday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 11Overloaded OperatorsOne of the goals in designing a new ADT is to make it as much like the built-in types as possible.The program in inclass9v1.cpp "works" but is very awkward to read/write. Ideally, we'd like to write the program in inclass9.cpp.C++ has operator functions, so we can define overloaded operator functions.Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 12Overloaded OperatorsWhen a binary operator is used, a binary function is called with the operands as arguments. These functions are named operator<op>, where <op> is an operator symbol. E.g., operator== is the function associated with ==.As with any C++ function, these function names can be overloaded to work with different parameter types. This allows any class to provide operators as its operations.Monday, January 31 CS 215 Fundamentals of Programming II - Lecture 9 13Overloaded OperatorsConsider the expression x <op> y. There are three ways to define the operator<op> function.As a member function. The equivalent function call is x.operator<op>(y). The left operand is the object whose member function is being called. The argument is the right operand. Direct access to attributes is automatic.As a free function. The equivalent function call is operator<op>(x, y). This function has no direct access to the argument objects' (private) attributes.As a friend function. The equivalent


View Full Document

UE CS 215 - LECTURE NOTES

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?