Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Lesson 14 Classes, Continued CS1 Lesson More Classes1.
Operator Overloading Fundamentals
Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Case Study - Fractions Timothy Budd Oregon State University.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
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+
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Chapter 8 Operator Overloading, Friends, and References.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
CSE1002 – Problem Solving with Object Oriented Programming
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
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.
Pointer to an Object Can define a pointer to an object:
Chapter 14: More About Classes.
Overloading Operator MySting Example
Introduction to Classes
14.4 Copy Constructors.
Chapter 15: Overloading and Templates
Memberwise Assignment / Initialization
The dirty secrets of objects
Chapter 14: More About Classes.
Introduction to Classes
Inheritance Using work from:
Chapter 9 Classes: A Deeper Look, Part 1
Operator Overloading; String and Array Objects
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Memberwise Assignment 14.3

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-3 Memberwise Assignment Can use = to assign one object to another, or to initialize an object with an object’s data Copies member to member. e.g., instance2 = instance1; means: copy all member values from instance1 and assign to the corresponding member variables of instance2 Use at initialization: Rectangle r2 = r1;

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-4

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-5

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Copy Constructors 14.4

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-7 Copy Constructors Special constructor used when a newly created object is initialized to the data of another object of same class Default copy constructor copies field-to-field Default copy constructor works fine in many cases

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-8 Copy Constructors Problem: what if object contains a pointer? class SomeClass { public: SomeClass(int val = 0) {value=new int; *value = val;} int getVal(); void setVal(int); private: int *value; }

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-9 Copy Constructors What we get using memberwise copy with objects containing dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // also 13 object1 object2 value 13

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programmer-Defined Copy Constructor Allows us to solve problem with objects containing pointers: SomeClass::SomeClass(const SomeClass &obj) { value = new int; *value = *(obj.value); } Copy constructor takes a reference parameter to an object of the class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programmer-Defined Copy Constructor Each object now points to separate dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // still 5 object1 object2 value 135

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programmer-Defined Copy Constructor Since copy constructor has a reference to the object it is copying from, SomeClass::SomeClass(SomeClass &obj) it can modify that object. To prevent this from happening, make the object parameter const: SomeClass::SomeClass (const SomeClass &obj)

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14-13

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operator Overloading 14.5

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operator Overloading Operators such as =, +, and others can be redefined when used with objects of a class The name of the function for the overloaded operator is operator followed by the operator symbol, e.g., operator+ to overload the + operator, and operator= to overload the = operator Prototype for the overloaded operator goes in the declaration of the class that is overloading it Overloaded operator function definition goes with other member functions

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operator Overloading Prototype: void operator=(const SomeClass &rval) Operator is called via object on left side return type function name parameter for object on right side of operator

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Invoking an Overloaded Operator Operator can be invoked as a member function: object1.operator=(object2); It can also be used in more conventional manner: object1 = object2;

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Returning a Value Overloaded operator can return a value class Point2d { public: double operator-(const point2d &right) { return sqrt(pow((x-right.x),2) + pow((y-right.y),2)); }... private: int x, y; }; Point2d point1(2,2), point2(4,4); // Compute and display distance between 2 points. cout << point2 – point1 << endl; // displays

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Returning a Value Return type the same as the left operand supports notation like: object1 = object2 = object3; Function declared as follows: const SomeClass operator=(const someClass &rval) In function, include as last statement: return *this;

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The this Pointer this : predefined pointer available to a class’s member functions Always points to the instance (object) of the class whose function is being called Is passed as a hidden argument to all non- static member functions Can be used to access members that may be hidden by parameters with same name

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley this Pointer Example class SomeClass { private: int num; public: void setNum(int num) { this->num = num; }... };

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Notes on Overloaded Operators Can change meaning of an operator Cannot change the number of operands of the operator Only certain operators can be overloaded. Cannot overload the following operators: ?:..* :: sizeof

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overloading > Overloaded stream operators >>, << must return reference to istream, ostream objects and take istream, ostream objects as parameters Examples

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overloading Types of Operators ++, -- operators overloaded differently for prefix vs. postfix notation Overloaded relational operators should return a bool value 14-24

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Overloaded [] Operator Can create classes that behave like arrays, provide bounds-checking on subscripts Must consider constructor, destructor Overloaded [] returns a reference to object, not an object itself