This, friend and operators. this exists in every non-static method exists in every non-static method a pointer to the oblect, for which the method was.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Operator Overloading Fundamentals
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Plab – Exercise 5 C++: references, operator overloading, friends.
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
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:
Ch 7. Operator Overloading Timothy Budd. Ch 7. Operator Overloading2 Introduction Almost all operators in C++ can be overloaded with new meanings. Operators.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
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.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
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:
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©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.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
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
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Lecture 9: Operator Overloading
Operator overloading Conversions friend inline
The dirty secrets of objects
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Overview of C++ Overloading
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Java Programming Language
Operator Overloading; String and Array Objects
Presentation transcript:

this, friend and operators

this exists in every non-static method exists in every non-static method a pointer to the oblect, for which the method was called a pointer to the oblect, for which the method was called used for returning reference or pointer to the object for which the method was called used for returning reference or pointer to the object for which the method was called does not occupy memory does not occupy memory does not need to be declared does not need to be declared

this person & person::timeTravel(int years) { age += years; age += years; return *this; return *this;} person Boss(„John”, „Kovalsky”, 40); ( Boss.timeTravel(+20) ).timeTravel(-30)

friend by declaring a „friendly” code we allow accessing protected and private members of the class by declaring a „friendly” code we allow accessing protected and private members of the class

friend class A { friend C::theMethodNoArg(); friend C::theMethodNoArg(); // specific method (if overloaded, then with specific args) // specific method (if overloaded, then with specific args) friend class B; friend class B; // concerns whole declaration of B // concerns whole declaration of B // declarations inside B also can access // declarations inside B also can access // non-public A members // non-public A members friend fun(); friend fun(); // specific function (if overloaded, then with specific args) // specific function (if overloaded, then with specific args)}

friend friendship is not inheritive friendship is not inheritive friendship is not transtive friendship is not transtive friendship is declared in any section of a class (even private) friendship is declared in any section of a class (even private)

friend class M { friend void f() … { … } }; is equivalent to class M { friend void f(); }; void f() { … }

Operators What is an operator? What is an operator? a = b.val >> 4 + c * d[ e++ ]

Operators operator defines operations performed on its arguments and the type of the resulting expression – similarly to functions. operator defines operations performed on its arguments and the type of the resulting expression – similarly to functions. in C++ we may define operators to be used with objects (at least one argument of the operator should be an object). in C++ we may define operators to be used with objects (at least one argument of the operator should be an object).

Operators Most operators are overloadable Most operators are overloadable Overloading may improve performance – similarly to functions Overloading may improve performance – similarly to functions There is no obligation, and usually no need either, to define a lot of operators. Instead of operator that is used rarely it is advised to declare a method or a function There is no obligation, and usually no need either, to define a lot of operators. Instead of operator that is used rarely it is advised to declare a method or a function

Overloadable operators () [] -> ! ~ * & new delete (typ) // unary ->* * / % + - > > >= >= == != &^|&&|| = += -= *= /= %= &= ^= |= >= // assignment

Operators Not overloadable operators: Not overloadable operators:..* ::?:sizeof operators to be declared exclusively as a class members: operators to be declared exclusively as a class members: ()[]->newdelete(typ)= predefined operarors (redefinable): predefined operarors (redefinable): =&

Associativity assignment and unary operators are of right associativity assignment and unary operators are of right associativity a=b=c is equivalent to a=(b=c) remaining ones are of right associativity remaining ones are of right associativity a+b+c is equivalent to (a+b)+c

Order of evaluation order of evaluation is undefined, except for: order of evaluation is undefined, except for:,||&&,||&& „, ”, „ || ”, „ && ” evaluated in the left to right order „, ”, „ || ”, „ && ” evaluated in the left to right order right arguments of operators „ || ”, „ && ” are evaluated only if necessary. right arguments of operators „ || ”, „ && ” are evaluated only if necessary.

Operators vs. methods different syntax for declaring, defining an callin different syntax for declaring, defining an callin no default arguments allowed (exception: function call operator) no default arguments allowed (exception: function call operator) there is a fixed number of arguments for specific operator (exception: function call operator) there is a fixed number of arguments for specific operator (exception: function call operator) there is a traditional semantics (meaning) of operators there is a traditional semantics (meaning) of operators

Operators If You need += then define it, If You need += then define it, defining + and = does not suffice If You need ++ then define it, If You need ++ then define it, defining + and = does not suffice Mind the traditional meaning of operators Mind the traditional meaning of operators let the unary & return the address let the unary & return the address Be cosistent Be cosistent ++i; i++; i+=1; i=i+1; // should result in the same change of i ++i; i++; i+=1; i=i+1; // should result in the same change of i

Operators as methods object of the class is the first argument of the operator object of the class is the first argument of the operator we declare all arguments, but the first one we declare all arguments, but the first one declaring declaring int point::operator<=(point); calling calling point point1, point2; int i= point1 <= point2; int j= point1.operator<=(point2);

Operators as functions at least one argument of the operator should be an object at least one argument of the operator should be an object all the arguments are operator function parameters all the arguments are operator function parameters there is no this there is no this declaring declaring int operator<=(point, point); calling calling point point1, point2; int i = point1 <= point2; int j= operator<=(point1, point2);

Convention for ++ and -- operator++() and operator--() are prefix ones operator++() and operator--() are prefix ones ++i--i postfix operators should be drfined using one additional unnamed int argument: postfix operators should be drfined using one additional unnamed int argument: operator++(int); operator--(int); i++i--

Convention for ++ and -- class X { public: X& operator++(); // prefix ++a X operator++(int); // postfix a++ }; class Y { public: }; Y& operator++(Y&); // prefix ++b Y operator++(Y&, int); // postfix b++

Convention for ++ and -- void f(X a, Y b) { ++a; // a.operator++(); a++; // a.operator++(0); ++b; // operator++(b); b++; // operator++(b, 0); a.operator++(); // explicit call: like ++a; a.operator++(0); // explicit call: like a++; operator++(b); // explicit call: like ++b; operator++(b, 0); // explicit call: like b++; }

Operators class X // members with this pointer avaliable { X* operator&();// prefix, unary, & X operator&(X);// two args X operator++(int);// suffix //X operator&(X, X);// error! 3 args //X operator/();// error! unary / ??? }; // standalone functions, no this, often friends of some class X operator-(X);// prefix, unary, - X operator-(X, X);// two args X operator--(X&, int);// suffix (post-decrementation) // X operator-(X, X, X);// error! 3 args // X operator/();// error! 0-argument operator! ;)

Assignment operator Define it as a nonstatic class member Define it as a nonstatic class member T& T::operator=(const T &) No initialization list, it’s not a constructor !!! No initialization list, it’s not a constructor !!!

Assignment operator person& person::operator=(const person &o) { if (this == &o) // Tom = Tom; if (this == &o) // Tom = Tom; return *this; return *this; delete [] name; delete [] name; name=new char[strlen(o.name) + 1]; name=new char[strlen(o.name) + 1]; strcpy(name, o.name); strcpy(name, o.name); delete [] lastName; delete [] lastName; lastName=new char[strlen(o.lastName) + 1]; lastName=new char[strlen(o.lastName) + 1]; strcpy(lastName, o.lastName); strcpy(lastName, o.lastName); age=o.age; age=o.age; return *this;// returning an object (as a reference) return *this;// returning an object (as a reference)}

Assignment operator why it returns a reference? why it returns a reference? we’d like to do: o1=o2=o3; we’d like to do: o1=o2=o3; person& operator=(const person&); 1. assignment to: o2 from: o3 2. assignment to: o1 from: o2 person operator=(const person&); 1. assignment to: o2 from: o3 2. copy constructor (temporary object t1 created) 3. assignment to: o1 from: t1 4. copy constructor (temporary object t2 created) 5. destructor t2 6. destructor t1

Assignment operator why it returns a reference? why it returns a reference? we’d like to do: o1=o2=o3; we’d like to do: o1=o2=o3; void operator=(const person&); void operator=(const person&); we may do: o2=o3; we may do: o2=o3; we cannot do: o1=o2=o3; we cannot do: o1=o2=o3;

Default (compiler generated) assignment operator if we do not define an assignment operator, then compiler generates one, that copies objects field by field. if we do not define an assignment operator, then compiler generates one, that copies objects field by field. it does not work for const fields it does not work for const fields it does not work for reference fields it does not work for reference fields it simply copies the pointer fields it simply copies the pointer fields

Stream operators Input Input friend istream & operator >>(istream &, T &); Output Output friend ostream &operator <<(ostream &, const T &);

Stream operators class point { int x, y; int x, y;public:… friend istream &operator >>(istream & s, point & p); friend istream &operator >>(istream & s, point & p); friend ostream &operator <<(ostream & s, const point &); friend ostream &operator <<(ostream & s, const point &);} point p, p2; cin>>p;cin>>p>>p2;cout<<p2<<p;

Stream operators istream & operator >>(istream & s, point & p) { s >> p.x >> p.y; s >> p.x >> p.y; return s; return s;} ostream &operator <<(ostream & s, const point & p) { s << p.x << p.y; s << p.x << p.y; return s; return s;}

Member access operator -> T* operator->(); T t; T t; t->m is interpreted as: t->m is interpreted as: ( t.operator-> )->m we may use object as if it was pointer we may use object as if it was pointer if operator-> returns no pointer, then if operator-> returns no pointer, then we still may use it: a=t.operator->(); we still may use it: a=t.operator->(); we cannot do: a=t->; it’s a syntax error we cannot do: a=t->; it’s a syntax error

Function call operator result T::operator()(args); Any number of arguments, overloadable Any number of arguments, overloadable Exception: function call operator may have default arguments Exception: function call operator may have default arguments We may use object as if it was a function We may use object as if it was a function T ob; ob(); we use it we use it when there is no appropriate operator, that we needed when there is no appropriate operator, that we needed there is a dominant method for the class (it now has no name, but is easier to call – for exaple class counter and method of incrementing) there is a dominant method for the class (it now has no name, but is easier to call – for exaple class counter and method of incrementing)

Index operator result T::operator[](index); Any type of result and index allowed, common sense implies integer index Any type of result and index allowed, common sense implies integer index We may use object as if it was an array We may use object as if it was an array

Conversion operator a.k.a. confersion function / method, a.k.a. confersion function / method, it converts object of its own class to other class it converts object of its own class to other class T::operator X(); Conversion operator of class T to the X type: Conversion operator of class T to the X type: no retrn value specified, no retrn value specified, no argument(s) specified, no argument(s) specified, declared as a class method declared as a class method

Conversion operator for example.: for example.: person::operator int(); conversion does not have to be explicit: conversion does not have to be explicit: person o; int i=o+5; In expressions In expressions for the specific object conversion is performed only if it is necessary, for the specific object conversion is performed only if it is necessary, no more than one conversion per single argument no more than one conversion per single argument no ambiguity allowed no ambiguity allowed

Conversion operator for example for classes A, B and C, conversion operators B::operator A() and C::operator B(); are defined for example for classes A, B and C, conversion operators B::operator A() and C::operator B(); are defined int f(A a); A a; B b; C c; f(a); // ok.. f(b); // f(A(b)) // f(c); // there is no operator C::operator A() // compiler cannot interpret this as f(A(B(c))) // (2 conversions of single argument)

Conversion operator For converting objects of one type into other type we could use a constructor, however: For converting objects of one type into other type we could use a constructor, however: this way we could not convert to fundamental type (like: int, it’s not a class), this way we could not convert to fundamental type (like: int, it’s not a class), this way we could not convert to already defined class without redefining it. this way we could not convert to already defined class without redefining it.

Example class counter { int cnt; int cnt; public: public: counter(int i=0):cnt(i){}; counter(int i=0):cnt(i){}; int operator()(int =1); // increment by arg. or by 1 int operator()(int =1); // increment by arg. or by 1 operator int(); // convert to int operator int(); // convert to int operator double(); // convert to double operator double(); // convert to double counter &operator+=(int); counter &operator+=(int); friend counter operator+(int, counter); // int+counter, friend friend counter operator+(int, counter); // int+counter, friend counter operator+(int); // counter+int counter operator+(int); // counter+int int operator++();// ++counter int operator++();// ++counter int operator++(int);// counter++ int operator++(int);// counter++};

Example inline int counter::operator ()(int i) { return cnt+=i; return cnt+=i;} inline counter::operator int() { return cnt; return cnt;} inline counter::operator double() { return double(cnt); return double(cnt);}

Example counter & counter::operator +=(int i) { cnt+=i; // operator()(i); (*this)(i) cnt+=i; // operator()(i); (*this)(i) return *this; return *this;} counter operator+(int i, counter l) { l+=i; // friend was not necessary l+=i; // friend was not necessary return l; return l;}

Example counter counter::operator +(int i) { counter l=*this; counter l=*this; l+=i; l+=i; return l; return l;} int counter::operator ++() { cnt+=1; cnt+=1; return cnt; return cnt;} int counter::operator ++(int) { int ret=cnt; int ret=cnt; cnt+=1; cnt+=1; return ret; return ret;}

Example class complex { double re, im; double re, im;public: complex(double re, double im):re(re), im(im) {}; complex(double re, double im):re(re), im(im) {}; complex & operator = (const complex &c); complex & operator = (const complex &c); complex & operator += (const complex &c); complex & operator += (const complex &c); complex operator + (const complex &); complex operator + (const complex &); friend complex & operator -= (complex &, const complex &); friend complex & operator -= (complex &, const complex &); friend complex operator - (const complex &, const complex &); friend complex operator - (const complex &, const complex &); friend istream & operator >>(istream &, complex &); friend istream & operator >>(istream &, complex &); friend ostream & operator <<(ostream &, const complex &); friend ostream & operator <<(ostream &, const complex &);};

Example complex & complex::operator = (const complex &c) { re=c.re; re=c.re; im=c.im; im=c.im; return *this; return *this; }

Example complex & complex::operator += (const complex &c) { re+=c.re; re+=c.re; im+=c.im; im+=c.im; return *this; return *this; } inline complex complex::operator + (const complex & c) { return complex(re+c.re, im+c.im); // complex(double, double) return complex(re+c.re, im+c.im); // complex(double, double)}

Example inline complex & operator-=(complex & c1, const complex & c2) //friend { c1.re-=c2.re; c1.re-=c2.re; c1.im-=c2.im; c1.im-=c2.im; return c1; return c1;} complex operator-(const complex & c1, const complex & c2) //friend { complex c=c1; complex c=c1; c-=c2; c-=c2; return c; return c;}

Example istream & operator >>(istream & s, complex & c) { s >> c.re >> c.im; s >> c.re >> c.im; return s; return s;} ostream &operator <<(ostream & s, const complex & c) { s << c.re << c.im; s << c.re << c.im; return s; return s;}