{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

324-lecture19

# 324-lecture19 - Principles Principles of Programming...

This preview shows pages 1–11. Sign up to view the full content.

Principles of Programming Languages Lecture 19 University of Toronto Wael Aboulsaadat [email protected] http://portal.utoronto.ca/ 1

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

View Full Document
User -defined Operators defined Operators Operators such as +,-,*,… are defined for the language types Some languages enable the programmer to add new semantics for existing operators University of Toronto Enhances the writeability of the program but makes readability slightly harder 2
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; float z; University of Toronto }; 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; }

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

View Full Document
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 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

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

View Full Document
Assuming = operator is implemented for class Person Assignment Statement X = new Person(“John”) Phoneslst Y = X Phoneslst John John University of Toronto 6 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 University of Toronto E.g. a data structure with 1Mn nodes X = Y // means creating another 1Mn nodes 7

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

View Full Document
Syntax: X,Y = Y,X Semantics Assignment Statement Variations University of Toronto Swap X and Y Equivalent to temp = X X = Y Y = temp E.g. Python 8
Syntax: X,Y,Z = 10,20,30 Semantics Assignment Statement Variations University of Toronto Multiple assignment in one statement Left most term in right side is evaluated first E.g. X,Y,Z = 10,X + 2,Y + 3 // after evaluation X = 10, Y = 12, Z = 15 9

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

View Full Document
What rules exist for naming variables?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

324-lecture19 - Principles Principles of Programming...

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

View Full Document
Ask a homework question - tutors are online