Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 5 1.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Operator Overloading Fundamentals
Class and Objects.
Chapter 14 More About Classes. CS SJAllan Chapter 14 2 Instance and Static Members Instance variable:  A member variable in a class  Each object.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
. Plab – Tirgul 8 I/O streams Example: string class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Starting Out with C++, 3 rd Edition 1 Chapter 14: more about classes Static Members If a member variable is declared static, all objects of that class.
Classes Separating interface from implementation
More About Classes. C++: Classes & Objects -2 2 Instance and Static Members Each instance of a class has its own copies of the class’s instance (member)
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
C++ Review (3) Structs, Classes, Data Abstraction.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
CS Object Oriented Programming Using C++
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
1 CS161 Introduction to Computer Science Topic #16.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
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.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
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,
CS410 – Software Engineering Lecture #12: C++ Basics V
Yan Shi CS/SE 2630 Lecture Notes
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.
Pointers and Dynamic Arrays
Strings: C-strings vs. Strings as Objects
Chapter 14: More About Classes.
Submission Example May 14, 2018
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
14.4 Copy Constructors.
group work #hifiTeam
Chapter 14: More About Classes.
Strings: C-strings vs. Strings as Objects
Advanced Program Design with C++
COP 3330 Object-oriented Programming in C++
Chapter 14 – More About Classes
Presentation transcript:

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 5 1

Quick Look 2 Overloading

Copy Constructor 3 Copy constructor called whenever new object created and initialized with another object’s data Like all other constructors, but accepts a reference parameter of its own type If copy constructor not explicitly defined, one is automatically provided that performs memberwise copy

Copy Constructors 4 Required to use reference Shouldn’t need to change the parameter – so make const PersonInfo::PersonInfo(const PersonInfo &Obj) { Name= new char[strlen(Obj.Name) + 1]; strcpy(Name, Obj.Name); Age = Obj.Age; }

Operator Overloading 5 The standard operators in C++ can be redefined for use with objects Helps to make operations more intuitive string s1, s2; s1 = “Hello”; s2 = “World”; s1 += “ “ + s2; char s1[50], s2[50]; strcpy(s1, “Hello”); strcpy(s2, “World”); strcat(s1, “ “); strcat(s1, s2); C-Strings String Objects

operator=(…) 6 Will be called with statements such as: person2 = person1; OR person2.operator=(person1); Parameter r – declared as reference prevents invocation of copy constructor for copy of object Parameter declared constant – shouldn’t be any changes to r in this function void operator=(const PersonInfo& r)

PersonInfo 7 class PersonInfo { private: char* name; int age; public: //Other member functions void operator=(const PersonInfo &right) { delete [] Name; name = new char[strlen(right.name) + 1]; strcpy(name, right.name); age = right.age; } };

Aside: The this pointer 8 this is a special built-in pointer available to any member function contains the address of the object that called the member function passed as a hidden argument to all non-static member functions

operator=(…) 9 Multiple operators in one statement Person3 = Person2 = Person1; First Second If operator=() returns void, above won’t work To do this, operator=() must return type PersonInfo

PersonInfo 10 class PersonInfo { private: char* name; int age; public: //Other member functions PersonInfo operator=(const PersonInfo &right) { delete [] Name; name = new char[strlen(right.name) + 1]; strcpy(name, right.name); age = right.age; return *this; } };

General Issues of Operator Overloading 11 You can completely alter the intuitive meaning of an operator (can make = mean +)…. NOT a good idea!! You cannot change the number of operands needed by an operator. Cannot overload the following operators:..* ?: :: sizeof

Class FeetInches 12 #ifdef FEETINCHES_H #define FEETINCHES_H class FeetInches { private: int feet; int inches; void simplify(); public: FeetInches(int f = 0, int i = 0); void setFeet(int f); void setInches(int i); int getFeet(); int getInches(); FeetInches operator+(const FeetInches&); FeetInches operator-(const FeetInches&); }; #endif FeetInches.h

Class FeetInches 13 #include “FeetInches.h” #include using namespace std; FeetInches::FeetInches(int f = 0, int i = 0) { feet = f; inches = i; simplify(); } void FeetInches::setFeet(int f) {feet = f;} void FeetInches::setInches(int i) { inches = i; simplify(); } int FeetInches::getFeet() {return feet;} int FeetInches::getInches() {return inches;} FeetInches.cpp

Class FeetInches 14 FeetInches FeetInches::operator+(const FeetInches& r) { FeetInches temp; temp.inches = inches + r.inches; temp.feet = feet + r.feet; temp.simplify() return temp; } FeetInches FeetInches::operator-(const FeetInches& r) { FeetInches temp; temp.inches = inches – r.inches; temp.feet = feet –r.feet; temp.simplify(); return temp; } FeetInches.cpp

Class FeetInches 15 //Ensures feet/inches in simplest terms //no inches >= 12 void FeetInches::simplify(void) { if (inches >= 12) { feet += (inches / 12); // Integer division inches = inches % 12; } else if (inches < 0) { feet -= ((abs(inches) / 12) + 1); inches = 12 - (abs(inches) % 12); } FeetInches.cpp

Prefix ++ operator 16 FeetInches FeetInches::operator++() { ++inches; simplify(); return *this; }

Postfix ++ operator 17 FeetInches FeetInches::operator++(int) { FeetInches temp(feet, inches); inches++; simplify(); return temp; }

Overloading Relational Operators 18 int FeetInches::operator>(const FeetInches &Right) { if (Feet > Right.Feet) return 1; else if (Feet == Right.Feet &&Inches > Right.Inches) return 1; else return 0; }

Object Conversion 19 May provide a special operator function to convert a class object to any other type. No Return Type Specified – should always return a double FeetInches::operator double() { double temp = feet; temp += (inches / 12.0); return temp; }

Overloading > 20 FeetInches x; //other code //Good cout << x.getFeet() << “ feet”; cout << x.getInches() << “ inches”; //Better cout << x; cin >> x; //that’s better also

21 ostream& operator<< (ostream& strm, const FeetInches& obj); istream& operator>> (istream& strm, FeetInches& obj); - >> and << are part of the istream and ostream classes - returns a stream object to allow for chaining -cout << x << endl; Overloading >

22 ostream& operator<< (ostream& strm, const FeetInches& obj) { strm << obj.feet << “ feet, “ << obj.inches << “ inches”; return strm; } Overloading >

23 istream& operator>> (istream& strm, FeetInches& obj) { cout << “Feet: “; strm >> obj.feet; cout << “Inches: “; strm >> obj.inches; obj.simplify(); return strm; } Overloading >

Creating a String Class 24 Great example to demonstrate operator overloading MyString class defines an ADT for handing strings Memory management is handled “behind the scenes” Use operators for intuitive string manipulation Note: These slides only contain the interface. The implementation is on a handout.

MyString 25 #ifndef MYSTRING_H #define MYSTRING_H class MyString; //Forward Declaration ostream& operator<<(ostream&, const MyString&); istream& operator>>(istream&, MyString&); class MyString { private: char* str; int len; public: MyString(); MyString(MyString& right); MyString(char* sptr); ~MyString(); int length(); const char* getValue();

MyString 26 // overloaded operators MyString operator+=(MyString &); char *operator+=(const char *); MyString operator=(MyString &); char *operator=(const char *); int operator==(MyString &); int operator==(const char *); int operator!=(MyString &); int operator!=(const char *); int operator>(MyString &); int operator>(const char *); int operator<(const char *); int operator<(MyString &); int operator>=(MyString &); int operator<=(const char *); friend ostream &operator<<(ostream &, MyString &); friend istream &operator>>(istream &, MyString &); }; #endif