1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Binary Search Trees II Morse Code.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Object-Oriented Programming (OOP) Lecture No. 24.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Chapter 14: More About Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 14: More About Classes.
Overloading the << operator
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Inheritance: Polymorphism and Virtual Functions
Introduction to Programming
COMS 261 Computer Science I
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
COMS 261 Computer Science I
COP 3330 Object-oriented Programming in C++
COMS 261 Computer Science I
Inheritance: Polymorphism and Virtual Functions
CS 144 Advanced C++ Programming February 21 Class Meeting
Class: Special Topics Overloading (methods) Copy Constructors
Overloading the << operator
Presentation transcript:

1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29

2 Announcements

3 Review Classes –User defined data types –Constructors Of all types –Copy Constructor –Assignment operator

4 Outline Classes –Assignment operator

Assignment Operator It would be nice to assign one VEC object to another –v1 = v2; To do this we must overload the assignment operator (=) to define a function when a VEC object is on both the lhs and the rhs

Assignment Operator Should there be any arguments? –Yes, the VEC we wish to assign –Avoid making a copy, use call by reference Should the assignment operator change the RHS? –No, make it a const reference –void operator=(const VEC& v); Run CodeWarrior vec03

Assignment Operator The assignment operator for this class does not behave the way the primitive data types do –They allow chaining A = B = C; –What happens if we try to chain the VEC assignment operator? –VEC& operator=(const VEC& v);

Assignment Operator VEC& operator=(const VEC& v); Which VEC reference will we return? –Can not be v, as v is a const reference to a VEC, not a reference to a VEC! –It must be the VEC corresponding to the lhs The object that invoked the assignment operator, the operator= member function Which raises the question

Assignment Operator How do we access the object that invokes any member function? –So far we have accessed the data members of the object, but not the object itself Constructors Accessors Facilitators VEC::VEC (float xval, float yval) { x = xval; y = yval; } How do we access the object that contains x and y?

Assignment Operator Likewise The invoking object within a member function is referred to as this –That is correct, this Run CodeWarrior vec04 void VEC::setX (float xval) { x = xval; } Run CodeWarrior vec03

Outputting Vectors The print member function of the VEC class is rather lame –It always inserts the stream into cout –Modify print() so that it takes a parameter of the output stream we wish to insert the vector VEC v(1.2f, 3.4f); v.print(cout); Run CodeWarrior vec05

Outputting Vectors The real power of C++ now come into play –We can use the print function with any output stream!! For example, a file Run CodeWarrior vec06

Outputting Vectors The print member function of the VEC class is still rather lame –A different sequence of statements to output an object of the VEC class –How can we make vectors act like the primitive types? int a = 5; cout << a << endl; VEC v(1.2f, 3.4f); v.print(cout);

Outputting Vectors What do we need to do to allow this in our programs? –Overload the output stream insertion operator (<<) ostream object on the lhs VEC on the rhs VEC v(1.2f, 3.4f); cout << v << endl;

Outputting Vectors The insertion operator is global –It is not implemented as a member function of any class Place its prototype outside the class defination, but inside the class header file, vec.h Run CodeWarrior vec07 void operator<<(ostream& strm, VEC& v);