04b_cpp_overload

04b_cpp_overload - Data Structures - CSCI 102 CS102 C+...

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

View Full Document Right Arrow Icon
1 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 C++ Operator Overloading Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 1

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

View Full DocumentRight Arrow Icon
Polymorphism (Ch 13) Virtual functions Abstract classes Interfaces C Structs (Ch 10) Topics to cover 2 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Object-Oriented Programming C++ Classes (Ch 11) Constructors Destructors Member functions Exceptions (Ch 15) Namespaces (Ch 8) Operator Overloading (Ch 14) Class Composition & Inheritance (Ch 12)
Background image of page 2
They work on built-in types (e.g. int , string ) C++ has tons of built-in operators (e.g. +,-,<<, etc.) 3 Data Structures - CSCI 102 Copyright © William C. Cheng Operator Overloading They don’t work on your own classes! string a = "hello " ; string b = " world!" ; string c = a + b; //addition cout << c << endl; //print to console String is a class, so why does this work? Since C++ doesn’t know how to do this stuff by default, we have to tell it how
Background image of page 3

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

View Full DocumentRight Arrow Icon
You can only overload an operator that has at least one user defined type (i.e. you can’t overload " int + int ") Rules 4 Data Structures - CSCI 102 Copyright © William C. Cheng Operator Overloading You can only overload existing operators, you can’t create your own new ones All operators keep their normal associativity & precedence You can’t overload any of these :: . -> .* ?: sizeof
Background image of page 4
5 Data Structures - CSCI 102 Copyright © William C. Cheng The Point Class class Point { private : int x; int y; public : Point() { x= 0 ; y= 0 ; } Point( int newx, int newy) { x=newx; y=newy; } int getX() const { return x; } int getY() const { return y; } void setX( int newx) { x=newx; } void setY( int newy) { y=newy; } };
Background image of page 5

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

View Full DocumentRight Arrow Icon
You can implement all the operators in C++ as a function call somewhere behind the scenes Point p1( 10 , 10 ), p2( 50 , 50 ); Point p3 = p1 + p2; When you see something like this: 6 Data Structures - CSCI 102 Copyright © William C. Cheng The + Operator You should really picture it like this: It can be, but it doesn’t really need to be (no changes are made to p1 or p2) Should this function be a member of the Point class? Could we get access to the private data of "a" and "b" without being part of class "Point"? Point operator +( const Point &a, const Point &b)
Background image of page 6
What does it mean to tag a function as a friend of a class? 7 Data Structures - CSCI 102 Copyright © William C. Cheng The friend Keyword The function can still access to the private data of the class Most of the time it is, but it’s useful for operators that don’t modify class data That sounds like a horrible idea! friend
Background image of page 7

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

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

This note was uploaded on 02/17/2011 for the course CSI 102 taught by Professor Billcheng during the Spring '11 term at USC.

Page1 / 28

04b_cpp_overload - Data Structures - CSCI 102 CS102 C+...

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

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