324-lecture19 - Principles of Programming Languages...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Principles of Programming Languages Principles of Programming Languages Lecture 19 Lecture 19 University of Toronto Wael Aboulsaadat Wael Aboulsaadat wael@cs.toronto.edu wael@cs.toronto.edu http://portal.utoronto.ca/ 1 User User-defined Operators defined Operators Operators such as +,-,*, are defined for the language types Some languages enable the programmer to add new University of Toronto semantics for existing operators Enhances the writeability of the program but makes readability slightly harder 2 User User-defined Operators defined Operators class Cube { // C++ public: Cube :: Cube( float inx, float iny, float inz); Cube operator+ ( const Cube & rhs); float Cube :: getX(); float Cube :: getY(); float Cube :: getZ(); private : float x; float y; oat University of Toronto float z; }; Cube :: Cube( float inx, float iny, float inz) { x = inx; y = iny; z = inz; } Cube Cube ::operator+ ( const Cube & rhs) { float newx; if (x > rhs.x) newx = x else newx = rhs.x; return Cube(newx,newy,newz); } 3 int main () { Cube Compaq = Cube(33.0,17.0,3.0); Cube Powerbook = Cube(39.0,16.0,1.8); Cube Combo = Compaq + Powerbook; } User User-defined Operators defined Operators class Car: # Python def __init__ ( self, Brand,EngineSerial,carclr): self .Brand = Brand self .Serial = EngineSerial self .carclr = carclr def __eq__ ( self ,rhs): return self .Serial == rhs.Serial University of Toronto if __name__ == "__main__": car1 = Car("Honda",111,"white") car2 = Car("Honda",111,"red") if car1 == car2: print "they are equal" else: print "they are not equal" 4 Syntax: X = <expression> X := <expression> X <- <expression> Assignment Statement Assignment Statement University of Toronto Semantics Evaluate right hand side first, the result is assigned to left hand side Make left hand side and right hand side equal With operator overloading, assignment gets a little bit more complicated 5 Assuming = operator is implemented for class Person Assignment Statement Assignment Statement X = new Person(John) Phoneslst Y = X Phoneslst University of Toronto 6 John John X = new Person(John) Phoneslst Y = X Phoneslst John John Shallow Copy Deep Copy With operator overloading, assignment gets a little bit more complicated Deep copy is very powerful but very expensive. Assignment Statement Assignment Statement University of Toronto E.g. a data structure with 1Mn nodes X = Y // means creating another 1Mn nodes 7 Syntax: X,Y = Y,X Semantics Assignment Statement Variations Assignment Statement Variations University of Toronto Swap X and Y Equivalent to temp = X X = Y Y = temp E.g....
View Full Document

This document was uploaded on 08/02/2011.

Page1 / 29

324-lecture19 - Principles of Programming Languages...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online