Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.

Similar presentations


Presentation on theme: "OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3."— Presentation transcript:

1 OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3

2 OOP Spring 2007 – Recitation 32 Today: Operator overloading Friend This

3 OOP Spring 2007 – Recitation 33 Operator Overloading

4 OOP Spring 2007 – Recitation 34 The “Old” Matrix Consider class Matrix that implements a 2D matrix of integers. It supports all the usual operations – arithmetics, printing, indexing, assigning. Its use looks like: Matrix m1, m2, m3; m3.copy(m1); m3.add(m2); m3.print(); cout << m1.index(5, 5);

5 OOP Spring 2007 – Recitation 35 The “New” Matrix It’s much more natural to use standard notation: +, *, <<, = … These are called operators, and in C++ the programmer can define new meanings for them – this is called operator overloading. Now the usage is more natural: Matrix m1, m2, m3; m3 = m1 + m2; cout << m3; cout << m1(5, 5);

6 OOP Spring 2007 – Recitation 36 Syntax An overloaded operator is simply a function with funny name and calling syntax. It can implement any behavior we want ( + can subtract, etc.) Name for the function that implements + is operator+() and it can be called either by m1 + m2; or by operator+(m1, m2); Basically, the compiler replaces the former form by the latter.

7 OOP Spring 2007 – Recitation 37 Two Forms of An Operator Just as any function, an operator can be defined as a non-member function, or as a class method. If a compiler sees m1 + m2; it looks for operator+ in m1 ’s class and writes m1.operator+(m2); and for non-member operator+ and writes operator+(m1, m2); If both exist, overloading resolution is used.

8 OOP Spring 2007 – Recitation 38 Binary and Unary A binary operator takes two arguments (such as +, -, < ). A unary operator takes one argument (such as ++, ! ).

9 OOP Spring 2007 – Recitation 39 Overloadable Binary These binary operators can be overloaded: +-*/% &|^ =+=-=*=/=%=<<=>>=&=|=^= <<=>>===!= []() ->*->, <<>>&&||

10 OOP Spring 2007 – Recitation 310 Overloadable Unary These unary operators can be overloaded: ~!-+ &*newnew[]deletedelete[] ++ (prefix)++ (postfix)-- (prefix)-- (postfix)

11 OOP Spring 2007 – Recitation 311 Non-overloadable These operators cannot be overloaded: ::..*sizeoftypeid?:

12 OOP Spring 2007 – Recitation 312 Binary Looks and Calling A binary operator can be defined by either –a non-static member function taking one argument of any type, –or a non-member function taking two arguments of any type. Return type can be anything. For a binary operator@, a@b can be interpreted either as a.operator@(b) or as operator@(a, b).

13 OOP Spring 2007 – Recitation 313 Unary Looks and Calling A unary operator can be defined by either –a non-static member function taking no arguments, –or a non-member function taking one argument of any type. Return type can be anything. For a unary operator@, @a can be interpreted either as a.operator@() or as operator@(a).

14 OOP Spring 2007 – Recitation 314 Must-be Members These operators must be non-static member functions of a class: – operator= – operator[] – operator() – operator-> This is to ensure that their first operand is lvalue. All the rest can be either member or non- member.

15 OOP Spring 2007 – Recitation 315 Friends

16 OOP Spring 2007 – Recitation 316 Non-member Private Access Sometimes we need to “break” the encapsulation of a class – allow a non- member function access to private parts. For example, operator+() should not be a member function, but it might need access to implementation. C++ allows the class to declare some function as its friend, thus allowing access to private parts.

17 OOP Spring 2007 – Recitation 317 Friend Function Adding keyword friend before function name in a class definition implies that this function is friend of the class rather than member. The function still needs to be defined. class Complex { friend Complex operator+(const Complex&, const Complex&); }; operator+() above is not a member function of Complex (we can’t call c.operator+() for Complex c ), but it can access private parts of Complex.

18 OOP Spring 2007 – Recitation 318 Friend Class A class can declare another class’s method as friend : class A { friend void B::stop(); }; class B { void stop(); }; If all methods of one class need to be friend s of another, the whole class can be declared friend : class A { friend class B; };

19 OOP Spring 2007 – Recitation 319 Technicalities friend ship is not transitive. A friend of a friend is not (necessarily) a friend. It doesn’t matter if the friend declaration is made in the public or private parts (public is better). If the friend function is itself a method of another class, it doesn’t matter if it is in the public or private parts. Don’t declare everything friend just because you can.

20 OOP Spring 2007 – Recitation 320 This

21 OOP Spring 2007 – Recitation 321 The pointer this Class Date day month year set() ge() d1.set(10,3,2000) d2.set(5,4,2007) void Date::set(int d, int m, int y) { day = d; month = m; year = y; }

22 OOP Spring 2007 – Recitation 322 The pointer this How the compiler can know which fields to update in set() ? d1’s or d2’s ? Answer: The compiler does a “translation” of the code to a c-like code. It adds as the beginning of the parameter list a pointer to the object, named this

23 OOP Spring 2007 – Recitation 323 The pointer this void Date::set(Date* const this, int d, int m, int y) { this->day = d; this->month = m; this->year = y; } d1.set(10,3,2000)  Date::set(&d1,10,3,2000)

24 OOP Spring 2007 – Recitation 324 The pointer this We can use this explicitly Sometimes we must use it for example: when a member function should return the object it is working on (as in operators overloading)


Download ppt "OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3."

Similar presentations


Ads by Google